Problem with cumsum

Hi guys,
I am trying to write a code to correct the background for an image. I used the formula given in the paper "Methods of Digital Video Microscopy for Colloidal Studies" by Crocker and Grier.
The formula is
Ib(x,y)=(constant)*[?I(x+i,y+j)] %the summation goes from i,j= -w to +w; w is predefined. 'I' is the original image and 'Ib' the background corrected image.
I thought of using "cumsum" since we need to sum up the components of the rows and columns of a matrix basically to decrease the computation time. However I am getting an error and I am clueless about what I am doing wrong.
The code is given below
I=imread('*.tif');
I=rgb2gray(I);
sizeim=size(I); %computes size of image
Iback=zeros(sizeim(1),sizeim(2)); %Iback same size as I but with zeros
B=1/((2*w+1)^2); %constant used for division
w=5;
for i=w+1:sizeim(1)-w
for j=w+1:sizeim(2)-w
temp=cumsum(cumsum(I(i-w:i+w,j-w:j+w),1),2);
Iback(i,j)=temp(2*w+1,2*w+1)/B;
end
end
I get the error message "??? Undefined function or method 'cumsum' for input arguments of type 'uint8'"
Can anyone tell me what I am doing wrong? Your help is truly appreciated.
NS

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 18일

2 개 추천

CUMSUM does not work for data that isn't single or double. You can write your own function to do this, or cast the data to single before passing to CUMSUM. Note however, that you will have to figure out what to do with overflow! The cumulative sum in uint8 may easily go over 255.
intmax('uint8')
You could also convert your image to have a datatype which has a larger range...

댓글 수: 8

Matt Fig
Matt Fig 2011년 5월 18일
NS says,
Thanks Matt. What do you mean by data being single or double?
Matt Fig
Matt Fig 2011년 5월 18일
I mean the data type. You might want to read the help for CLASS. There are several data types in MATLAB, single, double, int32, uint32, int16, uint16, etc.
NS
NS 2011년 5월 18일
Thanks Matt. I am a little more confused than I was before. Since my image is a 2D matrix, cumsum should work right?
Also, when I add in "I=double(I);" before the 'for loop', my code runs just fine. But I am not able to see my image at all. It is just a white background. How do I deal with this?
Sean de Wolski
Sean de Wolski 2011년 5월 18일
The image appears to be all white because the range is assumed to be [0 1] for class double images. Thus everything 1 or greater appears white. Since it's not in the range [0 1], you have to tell matlab to show the whole thing.
imshow(I,[]);
NS
NS 2011년 5월 18일
It worked. Thanks a lot Matt and Sean. :)
Matt Fig
Matt Fig 2011년 5월 18일
To convert to double on [0 1], you need to keep the proportions.
I = double(I)/255;
And you image being 2D has nothing to do with the datatype! Don't just be confused, read the help for the CLASS function and read about datatypes in MATLAB.
Sean de Wolski
Sean de Wolski 2011년 5월 18일
I don't usually bother with the conversion from 0-1. Are there any actual advantages to it?
Matt Fig
Matt Fig 2011년 5월 18일
Perhaps not, I am not into image processing too much. I only think about the math of it that way. ;-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

질문:

NS
2011년 5월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by