0
votes

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)

1
Is img a short? Looks like when you add 1 when you assign b64 you get an overflow. Try converting to double.zeeMonkeez
Still no luck - I now get the error "In an assignment A(:) = B, the number of elements in A and B must be the same." I am pretty sure I am calling the max/min functions wrong, I know I need to use them, I just don't know where.Genevieve
What's the value of b64 when it fails?zeeMonkeez
The value is -4094, which can't be right because it's a negative number. I thought that the transpose function would fix that, but it doesn't look like it has done anything.Genevieve
Something like img = (img - fmin) / (fmax - fmin);?zeeMonkeez

1 Answers

1
votes

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).