MATLAB Circular sliding window and pixel values on diameters extraction

时间:2015-07-08 15:45:58

标签: algorithm matlab image-processing sliding-window

I'm trying to implement an algorithm that needs a circular sliding window, that goes through all the pixels of the image, and for each window I need to extract only pixels that lay on diameters at different angles of the circle.

enter image description here

I try to explain better with the use of the image above. Imagine that this is the result of a circular sliding window, I want to get only the pixel values that lay on the red line (diameter tilted of pi/8).

So far, I wrote these lines of code:

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);

and the function 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;

But from here, I don't know how to continue for getting pixels that lay on diameter. The diameter can be tilted of any angle. Any help is greatly appreciated!

1 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

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

%% 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提供所有像素,为C{2}等提供theta=15 ...