-3

I have a gray level image.i want to scan the image pixel by pixel from left to right and top to bottom using 8-neighbours. I want to check how many neighbours are 1 or greater and what is the value of neighbour? I want to label the pixel based on the value of neighbours

[m n]=size(Img)
kernel = [-1 -1 -1; -1 8 -1; -1 -1 -1];
outputImage = conv2(I, kernel);
for i=1:m
 for j=1:n
   if(Img(I,j)==1) check neighbours
end
end

if I have an image as

D =

 1     0     1     0     1     0
 0     1     0     1     0     1
 1     0     1     0     1     0
 0     1     1     0     1     0
 1     0     0     1     0     1
 0     0     0     1     0     0

no.of neighbours are counted as

neighbour=

 1     3     2     3     2     2
 3     2     3     3     3     2
 1     3     2     2     1     1
 2     4     2     3     1     1
 1     2     3     2     3     1
 1     1     2     1     3     1

Scan from left to right D(1,1)==1 and neighbour(1,1)>=1 So label it as 1 as no neighbours are labeld.if one of the neighbour labelled then copy it if more than one neighbous are labelled then copy one of the label and mark it as equivalent

I tried your code on this image

enter image description here

but all the labels are 1.but bwlabel produces a different result

user
  • 87
  • 10
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/143463/discussion-on-question-by-user-how-to-scan-gray-level-image-pixel-by-pixel-and-a). – Bhargav Rao May 05 '17 at 06:59

1 Answers1

0

First binarize your image with threshold 1

The apply the correct kernel, so the convolution will result in the number of "activated" neighbours

Img_binarized = double(Img >= 1);
kernel = [1 1 1; 1 0 1; 1 1 1];
Img2 = conv2(Img_binarized, kernel);
% here each pixel of Img2 contains the number of neighbours >= 1
  • 1
    You have to specify in your question what do you actually want to do. Write a small example with a 5x5 matrix and tell us what you want to achieve. This answer tells you "how many neighbours are 1 or greater" which is your question. –  May 03 '17 at 14:13
  • Img=[1 0 1 0 1 0;0 1 0 1 0 1;1 0 1 0 1 0;0 1 1 0 1 0;1 0 0 1 0 1; 0 0 0 1 0 0]; img=double(Img); Img_binarized = Img >= 1 kernel = [1 1 1; 1 0 1; 1 1 1]; Img2 = conv2(Img_binarized, kernel); – user May 03 '17 at 14:33
  • Error using conv2 First and second arguments must be single or double. Error in Untitled6 (line 5) Img2 = conv2(Img_binarized, kernel); – user May 03 '17 at 14:33
  • Forget about it. You should edit your question and clearly explain what you are trying to achieve with an example. Any comments here will not be read or taken in account by anyone. –  May 03 '17 at 14:35
  • @ Sembei Norimaki updated question. your answer produces error – user May 03 '17 at 14:57
  • @SembeiNorimaki You need to cast `Img_binarized` to `double`. `conv2` does not work with `logical` inputs. – rayryeng May 03 '17 at 15:02
  • @rayryeng can you help me to complete my algorithm – user May 03 '17 at 15:16
  • @rayryeng sure, but this is a minor thing. Updated my answer, however it doesn't seem what OP wants. –  May 03 '17 at 16:07
  • @SembeiNorimaki I agree. I made that comment before I found out what the OP really wanted. – rayryeng May 03 '17 at 17:31