问题
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:
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.
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?
Code to generate figure
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');
回答1:
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;
来源:https://stackoverflow.com/questions/12318990/efficient-way-of-making-checkers-from-connected-components