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)
I'm not familiar with MATLAB, but it looks like you're just reading the file from a specific seek point. Using a DICOM toolkit would ensure you get the actual pixel data attribute entry hand handle any encoding you're likely to run into.
Also, check if your DICOM reader is applying the rescale slope and intercept. Typically this will convert a CT image to Hounsfield units, which have negative values (though -4094 seems a bit much given air is -1000).