2016-08-15 11 views
0

在MATLAB中使用功能regionprops時,可以選擇提取每個連接組件的二進制圖像。二進制映像的大小被縮小爲連接組件的大小。我不希望二進制圖像的大小減少。我希望二進制圖像的大小保持其原始大小,同時僅在原始圖像大小的相應位置顯示選定的連接組件。如何以原始圖像大小提取連接的組件?如何使用`regionprops`顯示單個連接組件中的多個

回答

2

只需創建一個與原始圖像大小相同的空白圖像,而不是每個blob提取圖像,參考每個blob的原始圖像提取實際像素位置,然後通過將這些位置設置爲二進制true在這張空白圖片。使用regionprops中的PixelIdxList屬性獲取所需組件的列主要位置,然後使用它們將這些位置處的輸出圖像設置爲true

假設你regionprops結構存儲在S,並要提取出k個分量和原始圖像存儲在A,請執行下列操作:

% Allocate blank image 
out = false(size(A, 1), size(A, 2)); 

% Run regionprops 
S = regionprops(A, 'PixelIdxList'); 

% Determine which object to extract 
k = ...; % Fill in ID here 

% Obtain the indices 
idx = S(k).PixelIdxList; 

% Create the mask to be the same size as the original image 
out(idx) = true; 

imshow(out); % Show the final mask 

如果你有多個對象,並希望創建這個面具是每個物體的原始圖像大小,您可以使用for循環爲您做到這一點:

% Run regionprops 
S = regionprops(A, 'PixelIdxList'); 

% For each blob... 
for k = 1 : numel(S) 
    out = false(size(A, 1), size(A, 2)); % Allocate blank image 

    % Extract out the kth object's indices 
    idx = S(k).PixelIdxList; 

    % Create the mask 
    out(idx) = true; 

    % Do your processing with out ... 
    % ... 
end 
+0

非常歡迎您! – rayryeng

相關問題