MATLAB / Octave:从图像中剪切掉很多圆圈 [英] MATLAB/Octave: cut a lot of circles from a image

查看:192
本文介绍了MATLAB / Octave:从图像中剪切掉很多圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵(图像)和圆圈内有趣部分的信息
(给出中心corrdinates和半径)。我想为所有圆圈切割矩阵的
部分,以便为每个圆圈做更多的计算。或者至少我想要一个带有所有圆圈的位掩码。

I have an matrix (image) and information about interesting part within circles (center corrdinates and radii given). I want to cut for all the circles the parts of the matrix in order to do some more calculations for each circle. Or at least I want to have a bitmask with all the circle.

我使用Octave(但也可以使用MATLAB,但由于许可证的使用会很困难)并且以下脚本包含stackoverflow中的一些提示。我有20个圈子的信息,使用Octave在我的Core i5上需要大约0.7秒:

I use Octave (but could also use MATLAB but it would be difficult because of licence iusses) and have the following script with some hints from stackoverflow. I have information of 20 circles and it takes about 0.7 s on my Core i5 using Octave:

% image
dim_x = 1000;
dim_y = 1000;
A=rand(dim_x,dim_y);

% center positions and ...
c = [222 111; 878 112; 81 718; 89 112; 222 111; 878 112; 81 718; 89 112; 222 111; 878 112; 81 718; 89 112; 222 111; 878 112; 81 718; 89 112; 222 111; 878 112; 81 718; 89 112];
%...  radii of the circles
r = [10 33 55 2 22 10 33 55 2 22 10 33 55 2 22 10 33 55 2 22];

tic;
for i=1:size(c,1)
    % create a bitmask ...
    mask = bsxfun(@plus, ((1:dim_y) - c(i,1)).^2, (transpose(1:dim_x) - c(i,2)).^2) < r(i)^2;
    % ... cut the circles out of the image
    B=A.*mask;
end;
toc;

你知道一个更高效的解决方案,因为我想拥有大约600个圈子。

Do you know a more performant solution since I want to have about 600 circles.

提前致谢

推荐答案

试试

mask = bsxfun(@lt, ((1:dim_y) - c(i,1)).^2,  r(i)^2 - ((1:dim_x).' - c(i,2)).^2);

根据我的MATLAB分析器,这比你的版本快4倍。此外, B = A. *掩码行所需的时间与原始掩码相同... 线。不确定你可以做多少。

According to my MATLAB profiler, this is about 4 times faster than your version. Also, the B = A.*mask line takes about the same amount of time as the original mask = ... line. Not sure there's much you can do about that.

这篇关于MATLAB / Octave:从图像中剪切掉很多圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆