Question

I have 2D image data and I would like to compute the absolute difference between neighboring pixels. Each pixel has 8 neighbors (up down, left right, and the diagonals), and I need to record the results in a 1D vector.

Right now I first create a list of edge pairs (i,j) where i and j are the index of the pixel location. For example, the pixels in a 3x3 image are labeled as 1 to 9 and this defines the range of i and j. Then I loop over all edge pairs to compute the difference. The problem is that this is very slow since there are so many edges in an image.

Is there a better way to do this? Thanks very much.

Was it helpful?

Solution

Do you want to record the difference once or twice? If you count 8 differece per pixel, then you are recording each difference twice: once between i and j and once (the same abs difference) between j and i.

Here's a loop-free option for 2D image I

d1 = abs( I(:,1:end-1) - I(:,2:end) ); % difference left-right
d2 = abs( I(1:end-1,:) - I(2:end,:) ); % diff up
d3 = abs( I(1:end-1,1:end-1) - I(2:end,2:end) ); % 1st diagonal
d4 = abs( I(1:end-1,2:end) - I(2:end,1:end-1) ); % 1st diagonal
allDiff = [d1(:); d2(:); d3(:); d4(:) ]; % stack them together.

OTHER TIPS

Computing this way how to make abs difference between edge pixel and corresponding horizontal pixel and satisfying a threshold say 1,2,3..... and storing the value of horizontal pixel like same way also satisfying for horizontal opposite and performing same method as told above. suppose edge pixel I(i,j) then I(i,j+r) horizontal pixel and I(i,j-r) vertical pixel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top