从连接组件制作检查器的有效方法 [英] Efficient way of making checkers from connected components
问题描述
我有几个连接组件的二进制图像,一些大而一些小(可能只有1个像素)。有了这个,我正在寻找一种方法,以有效的方式使每个连接的组件成为一个检查器模式,而不是连接的blob。
I have a binary image of several connected components, some large and some small (maybe only 1 pixel). With this I am seeking a way to make each connected component into a checkers pattern, instead of the connected blobs, in an efficient way.
到目前为止,我已经提出了有两种方法可以尝试,但它们可能会产生错误,或者效率很低:
So far I have come up with two ways this could be tried, but they can either produce errors, or be quite unefficient:
-
我知道整个图像和可以制作一个跳棋图案蒙版来移除50%的像素。这非常快,但平均会删除50%的连接组件,这只是区域中的一个像素。
I know the entire image and can make a checkers pattern mask to remove 50% of the pixels. This is very fast, but will on average remove 50% of the connected components which are only one pixel in area.
使用 bwlabel ()
在MATLAB / Octave中,并循环遍历每个连接的组件,如果它超过1个像素,则仅将掩码应用于该组件(同时在循环到达时保留其他组件)。这可能是非常低效的。
Use bwlabel()
in MATLAB/Octave, and loop through each connected component only applying the mask to that component if it is over 1 pixel (while leaving the other components to be considered when the loop gets to them). This can be very inefficient.
可以使用任何智能/内置解决方案吗?
Any smart/built-in solutions which could be used?
生成数字的代码
T = zeros(40,40);
T(10:30,10:30) = 1;
chessVec = repmat([1;0],20,1);
T_wanted = (repmat([chessVec circshift(chessVec,1)],1,20).*T);
figure();
subplot(1,2,1);imshow(T);title('Start shape')
subplot(1,2,2);imshow(T_wanted);title('Wanted shape');
推荐答案
没有什么比一揽子检查更有效率了。您需要做的就是添加小型连接组件。
Nothing beats blanket checkering for efficiency. All you then need to do is add back the small connected components.
%# create a test image
img = rand(100)>0.8;
img = imclose(img,ones(5));
img = imerode(img,strel('disk',2));
%# get connected components
%# use 4-connect to preserve
%# the diagonal single-pixel lines later
cc = bwconncomp(img,4)
%# create checkerboard using one of Matlab's special matrix functions
chk = invhilb(100,100) < 0;
%# checker original image, add back small stuff
img(chk) = 0;
smallIdx = cellfun(@(x)x<2,cc.PixelIdxList);
img([cc.PixelIdxList{smallIdx}]) = 1;
这篇关于从连接组件制作检查器的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!