问题
I have a image that includes object and some unwanted region (small dots). I want to remove it. Hence, I use some morphological operator example 'close' to remove. But it is not perfect. Do you have other way to remove more clear? You can download example image at raw image
This is my code
load Image.mat %load Img value
Img= bwmorph(Img,'close');
imshow(Img);
回答1:
You might prefer a faster and vectorized approach using bsxfun
along with the information obtained from bwlabel
itself.
Note: bsxfun
is memory intensive, but that's precisely what makes it faster. Therefore, watch out for the size of B1
in the code below. This method will get slower once it reaches the memory constraints set by the system, but until then it provides good speedup over the regionprops
method.
Code
[L,num] = bwlabel( Img );
counts = sum(bsxfun(@eq,L(:),1:num));
B1 = bsxfun(@eq,L,permute(find(counts>threshold),[1 3 2]));
NewImg = sum(B1,3)>0;
EDIT 1: Few benchmarks for comparisons between bsxfun
and regionprops
approaches are discussed next.
Case 1
Benchmark Code
Img = imread('coins.png');%%// This one is chosen as it is available in MATLAB image library
Img = im2bw(Img,0.4); %%// 0.4 seemed good to make enough blobs for this image
lb = bwlabel( Img );
threshold = 2000;
disp('--- With regionprops method:');
tic,out1 = regionprops_method1(Img,lb,threshold);toc
clear out1
disp('---- With bsxfun method:');
tic,out2 = bsxfun_method1(Img,lb,threshold);toc
%%// For demo, that we have rejected enough unwanted blobs
figure,
subplot(211),imshow(Img);
subplot(212),imshow(out2);
Output
Benchmark Results
--- With regionprops method:
Elapsed time is 0.108301 seconds.
---- With bsxfun method:
Elapsed time is 0.006021 seconds.
Case 2
Benchmark Code (Only the changes from Case 1 are listed)
Img = imread('snowflakes.png');%%// This one is chosen as it is available in MATLAB image library
Img = im2bw(Img,0.2); %%// 0.2 seemed good to make enough blobs for this image
threshold = 20;
Output
Benchmark Results
--- With regionprops method:
Elapsed time is 0.116706 seconds.
---- With bsxfun method:
Elapsed time is 0.012406 seconds.
As pointed out earlier, I have tested with other bigger images and with a lot of unwanted blobs, for which bsxfun
method doesn't provide any improvement over regionprops
method. Due to the unavailability of any such bigger images in MATLAB library, they couldn't be discussed here. To sum up, it could be suggested to use either of these two approaches based on the input features. It would be interesting to see how these two approaches perform for your input images.
回答2:
You can use regionprops
and bwlabel
to select all regions that are smaller than a certain area (=number of pixels)
lb = bwlabel( Img );
st = regionprops( lb, 'Area', 'PixelIdxList' );
toRemove = [st.Area] < threshold; % fix your threshold here
newImg = Img;
newImg( vertcat( st(toRemove).PixelIdxList ) ) = 0; % remove
来源:https://stackoverflow.com/questions/22627071/remove-unwanted-region-in-image-by-matlab