i need to code for neighboring pixel

hi i need to code for image processing: i have a binary image. If the value of the current pixel is 255, and as soon as it is found out that there exists a neighboring pixel whose value is zero, then the value of this corresponding pixel in the C matrix is assigned the value 127.Similarly, if the value of the current pixel is zero and if there exists a neighboring pixel whose value is 255, then the current pixel (i,j) in matrix C is assigned the value 127. pleas help me.

Answers (3)

For the first question, call imerode(), then subtract from the original binary image. Then set all those pixels to 127. Untested code off the top of my head:
binaryImage = binaryImage - imerode(binaryImage);
grayImage = zeros(size(binaryImage), 'uint8'); % Create an output image.
grayImage(binaryImage) = 127; % This is your output image.
for the second question, basically the same except you call imdilate() and reverse the order of the subtraction:
binaryImage = imdilate(binaryImage) - binaryImage;
grayImage = zeros(size(binaryImage), 'uint8'); % Create an output image.
grayImage(binaryImage) = 127; % This is your output image.

3 Comments

By the way, I saw the tag called edge detection. This is not usually how you'd do edge detection on binary images. You'd use bwperim() or bwboundaries().
but i want to edge detection with bacterial algorithm. bacterium are the current pixel. The direction of the next move of the bacterium is found out by finding the difference of the current pixel with each of the eight neighboring pixels. I do not want to do it all the pixels simultaneously. In a loop,and in every step it do. you think index of current pixel is [i,j]
Then you don't have a binary image at all. You have a grayscale image, and you probably want to use graycomatrix(). Or maybe you just want the Laplacian:
edgeStrengthImage = conv2(double(grayImage), [-1,-1,-1,-1,8,-1,-1,-1,-1]/8, 'same');
That image gives you the average gray level difference between a pixel and its 8 neighbors.

Sign in to comment.

i1 = imdilate(A,ones(3));
l1 = find(A==0);
i2 = i1(l1);
A(l1(i2 == 255)) = 127;
fatemeh
fatemeh on 18 Apr 2013
i convert grayimage to binary image by local thresholding.and my threshold is 127. i have a matrix C for edge detection, and a matrix J for bacterium movment. my current index is img[i,j]=0 now i want to set c[i,j]=127 if neghborhood of current pixel is 255.

Asked:

on 18 Apr 2013

Community Treasure Hunt

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

Start Hunting!