-1

我有一个半径向量和数百个[X,Y]坐标的第二个向量。对于每个可能的坐标半径对,我计算了输入二进制图像上一个圆(其中心位于坐标中)内的所有黑色像素。

最快的方法是什么?我唯一的想法是遍历图像的每个像素,检查圆形方程,然后检查像素颜色,但对于数百个这样的操作似乎并没有优化多少。

4

2 回答 2

1

这是一种实现:

优点:

  • loopsmeshgrid/ndgrid。而是使用更快bsxfunpdist2

  • 即使圆圈重叠,点也只计算一次。

  • 使用的变量radius(所有圆的半径不同)

代码:

%// creating a binary image with little black dots
A = randi(600,256); 
imbw = A ~= 1;

%// Your binary image with black dots
imshow(imbw);

%// getting the index of black dots
[dotY, dotX] = find(~imbw);

nCoords = 10;               %// required number of circles

%// generating its random coordinates as it is unknown here
Coords = randi(size(A,1),nCoords,2);

%// calculating the distance from each coordinate with every black dots
out = pdist2(Coords,[dotX, dotY]).';  %//'

%// Getting only the black dots within the radius
%// using 'any' avoids calculating same dot twice
radius = randi([10,25],1,size(Coords,1));
pixelMask = any(bsxfun(@lt, out, radius),2);
nPixels = sum(pixelMask);

%// visualizing the results by plotting
hold on
scatter(dotX(pixelMask),dotY(pixelMask));
viscircles([Coords(:,1),Coords(:,2)],radius.');    %//'
hold off

输出:

>> nPixels

nPixels =

19

在此处输入图像描述

于 2015-05-23T13:44:23.983 回答
1

由于矩阵语法,Matlab 非常适合处理图像。它也适用于索引,因此大多数时候您可以避免“遍历像素”(尽管有时您仍然必须这样做)。

不是检查每个圆圈内的所有像素,也不必检测有多少像素被计算了两次,另一种方法是创建一个与图像大小相同的蒙版。为每个圆圈空白此蒙版(因此重叠像素仅“空白”一次),然后将蒙版应用于原始图片并计算剩余的照明像素。

例如,我必须采取一些样本数据,图像:

load trees
BW = im2bw(X,map,0.4);
imshow(BW)

树BW

以及 20 个随机点/圆坐标(您可以轻松更改点数和最小/最大半径):

%// still building sample data
s = size(BW) ;
npt = 20 ; Rmin=5 ; Rmax=20 ; %// problem constants

x = randi([1 s(2)]   ,npt,1); %// random X coordinates
y = randi([1 s(1)]   ,npt,1); %//        Y
r = randi([Rmin Rmax],npt,1); %// radius size between 5 to 20 pixels.

然后我们构建您的自定义掩码:

%// create empty mask with enough overlap for the circles on the border of the image
mask = false( s+2*Rmax ) ; 

%// prepare grid for a sub-mask of pixels, as wide as the maximum circle
xmask = -Rmax:Rmax ; 
[xg,yg] = ndgrid(xmask,xmask) ;
rg = sqrt( (xg.^2+yg.^2) ) ;    %// radius of each pixel in the subgrid

for ip=1:npt
    mrow = xmask+Rmax+y(ip) ;  %// calc coordinates of subgrid on original mask
    mcol = xmask+Rmax+x(ip) ;  %// calc coordinates of subgrid on original mask

    cmask = rg <= r(ip) ;      %// calculate submask for this radius
    mask(mrow,mcol) = mask(mrow,mcol) | cmask ; %// apply the sub-mask at the x,y coordinates
end
%// crop back the mask to image original size (=remove border overlap)
mask = mask(Rmax+1:end-Rmax,Rmax+1:end-Rmax) ;
imshow(mask)

气泡

然后你只需应用面具和计数:

%% // Now apply on original image
BWm = ~BW & mask ; %// note the ~ (not) operator because you want the "black" pixels
nb_black_pixels = sum(sum(BWm)) ;
imshow(BWm)

nb_black_pixels =
        5283

树蒙面

于 2015-05-23T13:54:37.243 回答