MATLAB Circular sliding window 和直径提取的像素值

MATLAB Circular sliding window and pixel values on diameters extraction

我正在尝试实现一种需要循环滑动 window 的算法,该算法遍历图像的所有像素,并且对于每个 window 我只需要提取放置的像素关于圆不同角度的直径。

我尝试使用上图更好地解释。想象一下,这是一个圆形滑动的结果window,我只想得到位于红线上的像素值(直径倾斜pi/8)。

到目前为止,我写了这几行代码:

I= imread('lena.png'); 
I = im2double(I);
h = fspecial('disk',5);
h = h > 0;
dir = pi/8;
o_image = blockproc(I, [1 1], @(x) MMF2D(x,h,dir), 'BorderSize', [5 5],...
'TrimBorder', false, 'PadPartialBlocks', true);

和函数MMF2D:

function [ o_pixel ] = MMF2D( i_block, i_window, i_directions)
%MMF2D Summary of this function goes here
%   Detailed explanation goes here

new_block = i_block.data .* i_window;

但是从这里开始,我不知道如何继续获取位于直径上的像素。直径可以倾斜任何角度。非常感谢任何帮助!

我会这样做:

首先创建遮罩(如果需要,可以将其放入函数中),并获取相关像素索引作为角度的函数:

%% create mask and get indices as function of angle
o=5;
m=zeros(2*o+1);
m(o+1,:)=1;
theta=0:15:90; % in degrees, theres no need to go beyond 90 deg becuase of symmetry
for n=1:numel(theta)
   id{n}=find(imrotate(m,theta(n),'nearest','crop'));
end;

我使用元胞数组,因为每个角度可以有不同数量的索引。

然后读取图片

I= imread('http://scipy-lectures.github.io/_images/lena.png'); 

将图像块重新排列成列

B = im2col(I,[2*o+1 2*o+1],'sliding');

你想要的只是:

for n=1:numel(theta)
    C{n} =  B(id{n},:);
end

C 中的每个单元格元素代表在长度 2*o+1 的直径上的像素,角度 theta 是每个定义的,对于图像中的每个像素。
所以 C{1} 将为您提供 theta=0 的所有像素,以及 theta=15C{2} 等...