I'm trying to make a histogram out of a DICOM file, but for the life of me I still can't figure out why I'm getting a negative value for an index. I've transposed the image but the index is still negative and I'm not sure what I am doing wrong. The values should be all correct for the file size, header, depth, and width, and the program I'm trying to process this in is MATLAB.
clear
fpointer=fopen('PIG_CT','r');
fseek(fpointer,980,'bof');
img=zeros(512,512);
img(:)=fread(fpointer,(512*512),'short');
img=transpose(img);
depth = 16;
width = depth/64;
fmax = max(max(img));
fmin = min(min(img));
hist64 = zeros(64,1);
for i = 1:512
for j = 1:512
rho = img(i,j);
b64 = floor(rho/width+1)+1;
hist64(b64,1)= hist64(b64,1)+1;
end
end
bar(hist64)
ERROR: Attempted to access hist64(-4094,1); index must be a positive integer or logical.
The equation that I am also using with this is:
Bin Width = (Image Depth)/(# of Bins)
img
ashort
? Looks like when you add 1 when you assignb64
you get an overflow. Try converting todouble
. – zeeMonkeezb64
when it fails? – zeeMonkeezimg = (img - fmin) / (fmax - fmin);
? – zeeMonkeez