how calculate difference 5 adjacent pixels?

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

What does the difference with the 5 adjacent pixels mean ?
Suppose you have
5 9 3 1 4 7 8 2 6 0 4
and you are "at" the 7, then what would the desired outcome be?
How about
1 2 3
4 5 6
7 8 9
which are the five pixels adjacent to 5?
Do you mean four?
If so, is the what is the difference between 5 and [2 4 6 8]?
Is it [3 1 -1 -3]?
Is it -15?
Is it the absolute difference?
Is it the mean difference? The max difference? Something else?
The algorithm and the article do not explain what the adjacent 5 pixels are next to, and only write this pseudo-code to identify the edge by removing the background of the image.
I do not know how the difference of 5 adjacent pixels is calculated, please help me.
in other article adjacent 5 pixels Shown above.
Now what should be done for difference 5 adjacent pixels?
That section of the article sounds like some undergrad students were told to experiment and make something up and document their process, and got a B- on the project write-up part but decided to publish anyways.
If I were a supervisor or editor, I would would have sent that section back as being unacceptable.
for x=2:col-6
for y=2:row-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
Attempted to access m(90,2); index out of bounds because
size(m)=[89,441].
Error in Third_Approach (line 13)
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));
m above is the as image what I need to do to fix the error above?
for x=2:col-6
x has to do with columns. That's fine, makes sense, that is consistent with the way MATLAB works
for y=2:row-6
y has to do with rows. That's fine, makes sense, that is consistent with the way MATLAB works.
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));
But you are using x (columns) to index rows, and using y (rows) to index columns.
It's backwards due to the terrible naming convention used by the paper. "row" is the number of columns (length of the rows), not the number of rows.
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)

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
DGM
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.
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).

3 Comments

OP's diagram does suggest that, but the pseudocode and application suggest that it's a sliding window filter. The diagrams and code come from two different papers.
How to use Imgaussfilt function in MATLAB 2013؟
does not have this function.
@m madia, use fspecial() to get the filter, then imfilter() to filter the image.

Sign in to comment.

Asked:

on 20 Nov 2021

Commented:

on 22 Nov 2021

Community Treasure Hunt

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

Start Hunting!