Remove unwanted image area with matlab

I have an image that includes an object and some unwanted areas (small dots). I want to delete it. Therefore, I use the example of the morphological operator "close" to delete. But this is not perfect. Do you have another way to remove more clearly? You can upload a raw image sample image

This is my code.

load Image.mat %load Img value
Img= bwmorph(Img,'close');
imshow(Img);

enter image description hereenter image description here

+4
source share
2 answers

You may prefer a faster and more vectorized approach, using bsxfun, as well as information obtained from itself bwlabel.

. bsxfun - , . B1 . , , , regionprops.

[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;

1: bsxfun regionprops.

1

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);

enter image description here

--- With regionprops method:
Elapsed time is 0.108301 seconds.
---- With bsxfun method:
Elapsed time is 0.006021 seconds.

2

( 1)

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;

enter image description here

--- With regionprops method:
Elapsed time is 0.116706 seconds.
---- With bsxfun method:
Elapsed time is 0.012406 seconds.

, , bsxfun regionprops. - MATLAB . , . , .

+9

regionprops bwlabel, , (= )

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
+4

Source: https://habr.com/ru/post/1533277/


All Articles