how calculate difference 5 adjacent pixels?

8 views (last 30 days)
m madia
m madia on 20 Nov 2021
Commented: Image Analyst on 22 Nov 2021
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 }
{ intensity=255 }
img1 = imsopen(img)
img2 = imerode(img1)
img3 = imclose(img2)
Image Analyst
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 official 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?

Sign in to comment.

Answers (3)

Sulaymon Eshkabilov
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));

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.

Image Analyst
Image Analyst on 21 Nov 2021
From your diagrams it looks like you want to scan your image with a 5x5 window, and have that window move in "jumps" of 5 pixels. This is different than conv2(), imfilter(), nlfilter(), etc. where the window moves over 1 pixel at a time. To have the filter window move in "jumps" you need blockproc(). I'm attaching several demos for blockproc(). I'm sure you'll be able to modify one to do the operation you want, like max-min to get the range or whatever. From r2017b and later, you can use the bounds() function to get the max and min
[minA,maxA] = bounds(A) returns the minimum value minA and maximum value maxA in an array. minA is equivalent to min(A) and maxA is equivalent to max(A).
Image Analyst
Image Analyst on 22 Nov 2021
@m madia, use fspecial() to get the filter, then imfilter() to filter the image.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!