Sliding pixel window to get 8 neighbour pixel values

5 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2021-3-19
编辑:Adam Danz 2021-3-19
Is the 3X3 window sliding by 1 unit or like a boxcar, by 3 units?
Sliding rightward then downward starting from the top left corner?
Lots of time spent here on missing details.
thorati chiranjeevi
@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.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2021-3-19
编辑:Adam Danz 2021-7-31
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 个评论
Walter Roberson
Walter Roberson 2021-7-31
It is a demonstration, that is deliberately drawing lots of things on the screen for visual purposes. You could reduce the pause(.2) to pause() of a smaller number, but you are still left with drawing a lot of things on the screen. How quickly could you read the changing display?
Adam Danz
Adam Danz 2021-7-31
@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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-3-19
  2 个评论
thorati chiranjeevi
my image size is [562 649] can you please provide the code. Thanks.
Adam Danz
Adam Danz 2021-3-19
编辑:Adam Danz 2021-3-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by