Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to sum the 8-neighbor pixel values of the current pixel.

Subject: How to sum the 8-neighbor pixel values of the current pixel.

From: W W

Date: 3 Oct, 2013 07:57:07

Message: 1 of 4

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 8-neighbor pixel values. If the summation of the 8-neighbor 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, -S-1, -1, S-1]'; % 8-neighbors 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 8-neighbor 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

Subject: How to sum the 8-neighbor pixel values of the current pixel.

From: someone

Date: 3 Oct, 2013 20:01:09

Message: 2 of 4

"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 8-neighbor pixel values. If the summation of the 8-neighbor 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, -S-1, -1, S-1]'; % 8-neighbors 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 8-neighbor 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, -S-1, -1, S-1]'
4. What do you meann by "mark" the current value?
   Change it to zero? NaN? Some other valyue?

Subject: How to sum the 8-neighbor pixel values of the current pixel.

From: W W

Date: 4 Oct, 2013 06:04:07

Message: 3 of 4

@someone
The answers for your question are as follow:
(2) Due to all of the border of the processed image have the pixel value of 0, then the results would be the same when the zero padding is used. So, I overlooked this issue to reduce the computational cost.
(4) The term "mark the current value", I meant that to mark or label the current pixel (which have value = 1 and the sum of 8-neighbor value =1) with the markers for creating the bounding box for further processing (not change it to any value).
Thanks for the useful advice.
 

Subject: How to sum the 8-neighbor pixel values of the current pixel.

From: Dehuan Xin

Date: 6 Oct, 2013 13:51:10

Message: 4 of 4

ROI=zeros(size(BW));
for i=2:1:size(BW,2)-1
for j=2:1:size(BW,1)-1
if (BW(j,i)==1)
temp=BW(j-1:j+1,i-1:i+1);
ROI(j,i)=[1,1,1,0,1,0,1,1,1]*temp(:);
end
end

i dont know whether it is what you want but i really have some difficulty reading your code.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us