"W W" wrote in message <l2j80j$kui$1@newscl01ah.mathworks.com>...
> Hi, all. I'm a newbie in MATLAB.
> I have a binary image. I want to find the pixel value =1 and label it as the current pixel.
> Then, I want to sum its 8neighbor pixel values. If the summation of the 8neighbor pixel values of the current pixel = 1, then mark that current pixel with marker. Some part of a binary image as follow:
>
> 0 0 0 0 0
> 0 1 0 0 0
> 0 0 1 1 0
> 0 0 0 0 1
> 0 0 0 0 0
>
>
> I tried the following MATLAB code but it has some problems (error at > Sums = sum(currentPix, nOffsets)). How can I do?
>
> Sums = 0;
> S = size(BW,1);
> nOffsets = [S, S+1, 1, S+1, S, S1, 1, S1]'; % 8neighbors offsets
> BW_Out = BW;
>
> for row=1:S
> for col=1:S
> if BW(row,col),
> break;
> end
> end
>
> idx = sub2ind(size(BW),row,col);
> neighbors = bsxfun(@plus, idx, nOffsets);
> currentPix = find(BW==1); %if found 1, define it as current pixel
>
> while ~isempty(currentPix)
>
> % new current pixel list is set of neighbors of current list.
> currentPix = bsxfun(@plus, currentPix, nOffsets);
> currentPix = currentPix(:);
> Sums = sum(currentPix, nOffsets); %*** error this line
>
> if (Sums==1) % if the sum of 8neighbor values = 1, mark ROI
> plot(currentPix,'r*','LineWidth',1);
> end
>
> % Remove from the current pixel list pixels that are already
> currentPix(BW_Out(currentPix)) = [];
>
> % Remove duplicates from the list.
> currentPix = unique(currentPix);
> end
> end
First, it would help to know WHAT the error message is.
Some problems I see with your code:
1. With the break, your code will only find the FIRST pixel=1 value.
What about the other pixel=1 values?
2. How do you define the 8 surrounding values if the
current location is the first or last row or column?
3. Sums = sum(currentPix, nOffsets); makes no sense.
doc sum
It says:
S = sum(A,dim)
The dim input is a positive integer SCALAR.
You defined it as:
dim = [S, S+1, 1, S+1, S, S1, 1, S1]'
4. What do you meann by "mark" the current value?
Change it to zero? NaN? Some other valyue?
