# how calculate difference 5 adjacent pixels?

8 views (last 30 days)

Show older comments

How calculate difference 5 adjacent pixels in under algorithm ?

please give me code in matlab

img = imread(“image path”)

img = rgb2gray(img);

row = no. of pixels in row of image

col = no. of pixels in column of image

for x from 2 to col-6

{

for y from 2 to row-6

{

find the difference of a particular with its adjacent 5 pixels

if difference of intensity > threshold

{ intensity=0 }

else

{ intensity=255 }

}

}

img1 = imsopen(img)

img2 = imerode(img1)

img3 = imclose(img2)

##### 9 Comments

Image Analyst
on 21 Nov 2021

In the paper it says "Code Environment

The above algorithm was written in a MATLAB code and was run on ofﬁcial MATLAB software. "

So, what happened when you contacted the authors and asked for the source code. Did you do that (I would)? What did they say?

### Answers (3)

Sulaymon Eshkabilov
on 21 Nov 2021

Here is the corrected code (see: row vs. col):

for x=2:row-6

for y=2:col-6

difference_of_intensity = abs(m(x,y) - m(x+1, y))+abs(m(x,y) - m(x+2, y))+abs(m(x,y) - m(x+3, y))+abs(m(x,y) - m(x+4, y))+abs(m(x,y) - m(x+5, y));

end

end

##### 0 Comments

DGM
on 21 Nov 2021

Edited: DGM
on 21 Nov 2021

Here. This implements a sliding-window filter based on your interpretation of what "find the difference of a particular with its adjacent 5 pixels" means.

In this filter, the output pixel is the sum of absolute differences between the pixel and its neighbors in a 5x5 window.

% this isn't the original image, but it's as good as we have

A = rgb2gray(imread('7.png'));

A = imsharpen(imresize(A,2));

% do filter thing

fs = 5;

centerpix = ceil(fs^2/2);

neighbors = [(1:centerpix-1) (centerpix+1:fs^2)];

F = @(x) sum(abs(x(centerpix) - x(neighbors)));

C = nlfilter(im2double(A),[fs fs],F);

[min(C(:)) max(C(:))] % result is poorly-scaled

ans =

0.0235 14.1020

imshow(C,[]) % show the result

No thresholding is performed internally, though it could. It can just as simply be applied afterwards.

How that relates to the operations performed in the paper is anybody's guess. There's no description of threshold selection or the parameters used for subsequent morphological operations.

The core of the paper is undocumented nonsense. It is unclear how the pseudocode relates to any of the described operations in the text or to which examples it applies. The preceding examples regarding sobel and prewitt filters appear to have been simply performed with imfilter() without actually calculating the gradient magnitude or paying particular attention to threshold levels. I don't know what you hope to glean from the results.

##### 0 Comments

Image Analyst
on 21 Nov 2021

##### 3 Comments

Image Analyst
on 22 Nov 2021

@m madia, use fspecial() to get the filter, then imfilter() to filter the image.

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!