且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从连接组件制作检查器的有效方法

更新时间:2023-11-26 12:25:16

没有什么比一揽子检查更有效率了。您需要做的就是添加小型连接组件。

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;