MATLAB Circular sliding window and pixel values on diameters extraction

巧了我就是萌 提交于 2019-12-11 03:26:59

问题


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.

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:


Here's what I'd do:

First create the mask (you can put this into a function if you want), and get the relevant pixels indices as function of angle:

%% 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 use a cell array because there can be different number of indices per angle.

Then read image

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

Rearrange image blocks into columns

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

what you want is just:

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

each cell element in C represents the pixels that were on a diameter of length 2*o+1 at an angle theta that was per-defined, for each pixel in the image.
So C{1} will give you all the pixels for theta=0, and C{2} for theta=15 etc...



来源:https://stackoverflow.com/questions/31297554/matlab-circular-sliding-window-and-pixel-values-on-diameters-extraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!