Remove unwanted region in image by matlab -
i have image includes object , unwanted region (small dots). want remove it. hence, use morphological operator example 'close' remove. not perfect. have other way remove more clear? can download example image @ raw image
this code
load image.mat %load img value img= bwmorph(img,'close'); imshow(img);
you might prefer faster , vectorized approach using bsxfun
along information obtained bwlabel
itself.
note: bsxfun
memory intensive, that's precisely makes faster. therefore, watch out size of b1
in code below. method slower once reaches memory constraints set system, until provides speedup on 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 comparisons between bsxfun
, regionprops
approaches discussed next.
case 1
benchmark code
img = imread('coins.png');%%// 1 chosen available in matlab image library img = im2bw(img,0.4); %%// 0.4 seemed make enough blobs image lb = bwlabel( img ); threshold = 2000; disp('--- regionprops method:'); tic,out1 = regionprops_method1(img,lb,threshold);toc clear out1 disp('---- bsxfun method:'); tic,out2 = bsxfun_method1(img,lb,threshold);toc %%// demo, have rejected enough unwanted blobs figure, subplot(211),imshow(img); subplot(212),imshow(out2);
output
benchmark results
--- regionprops method: elapsed time 0.108301 seconds. ---- bsxfun method: elapsed time 0.006021 seconds.
case 2
benchmark code (only changes case 1 listed)
img = imread('snowflakes.png');%%// 1 chosen available in matlab image library img = im2bw(img,0.2); %%// 0.2 seemed make enough blobs image threshold = 20;
output
benchmark results
--- regionprops method: elapsed time 0.116706 seconds. ---- bsxfun method: elapsed time 0.012406 seconds.
as pointed out earlier, have tested other bigger images , lot of unwanted blobs, bsxfun
method doesn't provide improvement on regionprops
method. due unavailability of such bigger images in matlab library, couldn't discussed here. sum up, suggested use either of these 2 approaches based on input features. interesting see how these 2 approaches perform input images.
Comments
Post a Comment