我有一個包含對象和一些不需要的區域(小點)的圖像。我想刪除它。因此,我使用一些形態操作符示例'close'來刪除。但它並不完美。你有其他方法可以更清楚地刪除?您可以在raw image通過matlab刪除圖像中的不需要的區域
下載示例圖像這是我的代碼
load Image.mat %load Img value
Img= bwmorph(Img,'close');
imshow(Img);
我有一個包含對象和一些不需要的區域(小點)的圖像。我想刪除它。因此,我使用一些形態操作符示例'close'來刪除。但它並不完美。你有其他方法可以更清楚地刪除?您可以在raw image通過matlab刪除圖像中的不需要的區域
下載示例圖像這是我的代碼
load Image.mat %load Img value
Img= bwmorph(Img,'close');
imshow(Img);
你可能更喜歡af使用bsxfun
以及從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;
EDIT 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);
輸出
基準測試結果
--- 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;
輸出
基準測試結果
--- With regionprops method:
Elapsed time is 0.116706 seconds.
---- With bsxfun method:
Elapsed time is 0.012406 seconds.
正如前面所指出的那樣,我與其他較大的圖像,並用了很多不必要的斑點,對於這bsxfun
方法不提供任何改善過度regionprops
方法進行測試。由於MATLAB庫中沒有任何這種更大的圖像,因此無法在這裏討論。總而言之,可以建議基於輸入特徵使用這兩種方法中的任一種。看看這兩種方法如何執行輸入圖像會很有趣。
您可以使用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
謝謝先生。但是你的代碼有一些錯誤。我建立並收到錯誤:「Error using horzcat CAT參數尺寸不一致。 removeUnwantedRegion錯誤(第6行) newImg([st(toRemove).PixelIdxList])= 0;%remove」。我的圖像大小是384x384 – user3336190
錯誤發生在newImg([st(toRemove).PixelIdxList])= 0; 。我調試它我發現它 – user3336190
@ user3336190我的不好:Matlab存儲'PixelIdxList'作爲每個區域的列向量。將所有這些元素連接在一起時,需要垂直(使用'vertcat')而不是水平(使用'[]')來CAT。 – Shai
'原始圖像'鏈接似乎被打破。 – Divakar