How I can compare one pixel to a neighbor pixel and also another pixel?

Also, how I can make a difference histogram of the comparison results? Or how I can make a difference histogram generally?

 Accepted Answer

How do you want to compare them? Do you want to subtract them? If so you can use conv2(). Do you want to know how many times one gray level occurs next to another gray level? If so you can use graycomatrix().

13 Comments

I want to compare gray levels by pixel to pixel and make difference histogram from the compare data.
what is the best way to do it?
Define "compare" - does that mean subtract? Define "difference histogram" - that is not standard terminology.
diffImage = conv2(grayImage, [-1 -1 -1;-1 8 -1; -1 -1 -1], 'valid');
[counts values] = hist(diffImage(:));
I said it all wrong. I mean that I have to calculate gray level difference between neighbour pixels and also pixel distance 1-3 in x- and y-directions. and then make histogram from the calculate data.
If you only want the above, below, and beside pixels then you need to change the kernel:
diffImage = conv2(grayImage, [0 -1 0;-1 4 -1; 0 -1 0], 'valid');
[counts values] = hist(diffImage(:));
I don't know what "pixel distance 1-3" is. Again, that is non-standard terminology.
I mean neighbour pixels and next to neighbour pixel, so there will be gray level compare for neighbour pixels and pixels which are two pixels away (next to neighbour pixels). Thanks for your answers.
You said "and also" so I thought it was a different thing. What the conv does is to take the (central pixel - top pixel) + (central pixel - left pixel) + (central pixel - right pixel) + (central pixel - bottom pixel) and then sums them up. So each output pixel is the sum of the pixel differences of the four closest neighbors. Is that what you want? If you want the mean instead of the sum, you can use [0 -1 0;-1 4 -1; 0 -1 0]/4 as the kernel - it's the same but divided by 4 to get the average delta gray level.
ok, now I understand. How I can do it when the pixel are not neighbour pixels?
Enlarge the mask (the sliding window). If you want, you can use a 5 by 5 window and only consider pixels that are 2 away. The value in the kernel window is the multiplicative factor on the intensity. So if you want to sum the pixel differences between the center and the 4 corners in a 5 by 5 you'd use this:
-1 0 0 0 -1
0 0 0 0 0
0 0 4 0 0
0 0 0 0 0
-1 0 0 0 -1
You can use any pattern and any weights that you want.
How I can do it without conv2 function? Can I do it with loop? So I like to subtract pixel gray level to it´s neighbour pixel gray level using loop. How can I do it?
ok. If you know the answer could you help me?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!