MATLAB Answers

Sliding pixel window to get 8 neighbour pixel values

12 views (last 30 days)
thorati chiranjeevi
thorati chiranjeevi on 18 Mar 2021
Edited: Adam Danz on 31 Jul 2021
Hi, Can anyone plz tell me how to slide a filter window of 3*3 over the pixels of an image one by one and get the 8 neighbouring pixel values.
  5 Comments
thorati chiranjeevi
thorati chiranjeevi on 19 Mar 2021
@Adam Danz It has to pass from top left corner like a boxcar by 3 units and cover the entire image. sorry for the inconvenience as i am very new to this MATLAB.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 19 Mar 2021
Edited: Adam Danz on 31 Jul 2021
Here's the framework to get you started.
An image is displayed and the starting and end coordinates of each 4x3 (width x height) window is stored in table T. The window moves horizontally across the image and then downward.
Using a for-loop, each window is plotted and the uint8 color values are printed in each pixel which can be compared to the colorbar to confirm accuracy. Don't try this with a large image where pixel sizes are a lot smaller.
The variable Isection contains the section of values from image I within the window.
% Load and display demo image
I = imread('cameraman.tif');
I = I(60:88, 130:157);
fig = figure('WindowState','maximized');
imagesc(I)
colormap('gray')
axis equal
axis tight
hold on
colorbar()
% Get end points (lower right corner) of each window
% stored in (end_x, end_y).
windowSize = [4,3]; % width, height
rowIndex = windowSize(2):windowSize(2):size(I,1);
columnIndex = windowSize(1):windowSize(1):size(I,2);
[end_x, end_y] = ndgrid(columnIndex, rowIndex);
% Get the start point (upper left corner) of each window
% stored in (start_x, start_y).
start_x = end_x - windowSize(1) + 1;
start_y = end_y - windowSize(2) + 1;
% Put window coordinates into a table for easier viewing.
T = table(start_x(:), end_x(:), start_y(:), end_y(:), ...
'VariableNames', {'X0', 'X1', 'Y0', 'Y1'})
% loop through each window
for i = 1:numel(start_x)
% Extract image data within window
Isection = I(T.Y0(i):T.Y1(i),T.X0(i):T.X1(i));
% Show window
rectangle('position',[T.X0(i)-.5, T.Y0(i)-.5, windowSize],'EdgeColor','r','LineWidth',1)
% Show color values
[y,x] = ndgrid(T.Y0(i):T.Y1(i), T.X0(i):T.X1(i));
text(x(:), y(:), compose('%d',Isection(:)), 'VerticalAlignment', 'middle', 'HorizontalAlignment', 'center', 'FontSize', 9, 'Color', 'c')
drawnow
pause(.2)
end
To group the data without showing the animation, replace the for-loop above with this version below. Iboxcar is an NxMxJ array for window size NxM for J windows. So data in the 2nd window in the animation is stored in Iboxcar(:,:,2).
Iboxcar = nan(windowSize(2),windowSize(1),numel(start_x));
for i = 1:numel(start_x)
% Extract image data within window
Iboxcar(:,:,i) = I(T.Y0(i):T.Y1(i),T.X0(i):T.X1(i));
end
  4 Comments
Adam Danz
Adam Danz on 31 Jul 2021
@Sparsh Garg, to avoid the visual animation and collect just group the data into varible, replace the loop in my answer with this loop. This takes less than 20 ms.
Iboxcar = nan(windowSize(2),windowSize(1),numel(start_x));
for i = 1:numel(start_x)
% Extract image data within window
Iboxcar(:,:,i) = I(T.Y0(i):T.Y1(i),T.X0(i):T.X1(i));
end
Iboxcar is an NxMxJ array for window size NxM and J windows. So data in the 2nd window in the animation is stored in Iboxcar(:,:,2).
I'll update my answer to include this option.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 19 Mar 2021
  2 Comments
Adam Danz
Adam Danz on 19 Mar 2021
Looking briefly at nlfilter.m, the filtering is done by a sliding window by 1 unit rather than acting as a boxcar filter that slides by the width/height of the window.
> can you please provide the code
@thorati chiranjeevi, it's unlikely that someone's going to volunteer to do your work for you. Some questions are specific and providing code is the best way to answer them. Your question does not point to a specific problem so the responses you'll likely get are ones that will point you in the right direction.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!