how calculate difference 5 adjacent pixels?
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
Walter Roberson
on 20 Nov 2021
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?
DGM
on 20 Nov 2021
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?
m madia
on 20 Nov 2021
Edited: Walter Roberson
on 20 Nov 2021
m madia
on 20 Nov 2021
Walter Roberson
on 20 Nov 2021
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.
m madia
on 20 Nov 2021
Walter Roberson
on 21 Nov 2021
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.
DGM
on 21 Nov 2021
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.
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?
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
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
on 21 Nov 2021
0 votes
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
3 Comments
DGM
on 21 Nov 2021
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.
m madia
on 22 Nov 2021
Image Analyst
on 22 Nov 2021
@m madia, use fspecial() to get the filter, then imfilter() to filter the image.
Categories
Find more on Image Filtering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
