1

我打電話給for循環中的一些圖像,然後對這些圖像進行一些處理。之後,我使用step函數在視頻播放器中顯示這些幀及其蒙版。如何將邊界添加到蒙版圖像內的對象?另外,如何使邊界更加粗糙,並在掩模圖像中繪製每個斑點在掩模中的質心?以下是代碼的粗略草圖。如何在step()函數內調用的視頻幀上繪製邊界和質心

videoPlayer = vision.VideoPlayer(); 
    maskPlayer = vision.VideoPlayer(); 
    for ii = 1:nfiles 
    filenameii = [............] 
    frame= imread(filenameii); 
    mask = dOB(frame,BackgroundImg); 
% some processing on the images 
    mask= bwareaopen(mask,27); 
    boundaries = bwboundaries(mask,'noholes'); 
    B=boundaries{1}; 
    Centroid = regionprops(mask,'centroid'); 
    Centroids = cat(1, Centroid.Centroid); 
    plot(B(:,2),B(:,1),'g','LineWidth',3); 
    plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10); step(videoPlayer,frame); 
    step(maskPlayer, mask); 

P.S:我知道如何顯示它使用hold on一個數字,但我想在圖像上直接完成這件事在視頻播放器顯示它之前。任何指導將不勝感激。

回答

2

在將視頻顯示在視頻播放器中之前,只需簡單地在屏蔽上繪製像素。你有什麼工作,但它會爲面具玩家繪製圖形中的邊界。因此,請從bwboundaries中檢測出您的邊界,從這些座標創建線性索引,並將圖像中的值設置爲白色。甚至可以更簡單的是取出檢測到的掩碼並使用bwperim自動生成一個包含斑點邊界的掩碼。我還看到您正在填充面罩的孔,因此您可以直接在後處理的輸出中使用imfill,以便它爲您提供圖像而不是座標。然後,您將使用此蒙版直接將圖像索引到您的圖像中,並將blob邊界的座標設置爲所需的顏色。如果您希望使周邊更粗,則使用適當大小的方形結構元素進行簡單圖像膨脹將有所幫助。只需將此結構化元素的鄰域的大小定義爲您所需的周邊厚度即可。最後,如果要將質心插入蒙版,並且具有MATLAB計算機視覺系統工具箱,請使用insertMarker函數,以便可以爲每個質心使用一組點並將它們直接放在圖像中。但是,您必須確保將掩碼從logical更改爲更適合圖像的數據類型。 uint8應該可以工作。因此,將圖像轉換爲此類型,然後將所有非零值乘以255以確保白色保持在蒙版中。用insertMarker,你想插入大小爲10的紅色加號,所以我們需要確保我們呼叫insertMarker來反映這一點。另外,因爲你想要一個彩色圖像,你將不得不人爲地使你的面具變成彩色,並且爲每個飛機單獨繪製你想要的顏色。既然你想要綠色,這對應於(0,255,0)的RGB值。

因此,我修改了您的代碼,以便它可以執行此操作。另外,我已經計算了填充的面膜的質心,而不是原來的面膜。我們不想錯誤地報告有間隙的物體的質心...除非這是你的目標,但讓我們假設你不是:

videoPlayer = vision.VideoPlayer(); 
maskPlayer = vision.VideoPlayer(); 

% New - Specify colour you want 
clr = [0 255 0]; % Default is green 

% New - Define thickness of the boundaries in pixels. 
thickness = 3; 

% New - Create structuring element 
se = strel('square', thickness); 

for ii = 1:nfiles 
    filenameii = [............] 
    frame = imread(filenameii); 
    mask = dOB(frame, BackgroundImg); 
    % some processing on the images 
    mask = bwareaopen(mask,27); 
    %boundaries = bwboundaries(mask,'noholes'); 
    %B=boundaries{1}; 

    % New code - fill in the holes 
    mask = imfill(mask, 'holes'); 

    Centroid = regionprops(mask,'centroid');  

    % New code - Create a boundary mask 
    mask_p = bwperim(mask, 8); 

    % New code - Make the boundaries thicker 
    mask_p = imdilate(mask_p, se); 

    % New code - create a colour image out of the mask 
    [red, green, blue] = deal(255*uint8(mask)); 

    % Paint the perimeter of the blobs in the desired colour 
    red(mask_p) = clr(1); green(mask_p) = clr(2); blue(mask_p) = clr(3);   

    Centroids = cat(1, Centroid.Centroid); 
    %plot(B(:,2),B(:,1),'g','LineWidth',3); 
    %plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10); 

    % New - Make mask into RGB image for marker painting and to 
    % show to the user 
    mask_p = cat(3, red, green, blue); 

    % New - Insert the centroids directly in the mask image 
    mask_p = insertMarker(mask_p, Centroids, '+', 'color', 'r', 'size', 10); 

    step(videoPlayer, frame); 

    % New - Show new mask in the player 
    step(maskPlayer, mask_p); 
end 
+0

非常感謝您的迴應。這確實在播放器中顯示blob的邊界,但是它顯示了具有邊界的原始圖像而不是二進制圖像。我想要在二進制圖像周圍顯示邊界。你能告訴我爲什麼這樣嗎?另外我想知道使用bwboundaries和bwperim的區別是什麼。 – BlueBee

+0

啊,我以爲你想在原始圖像,而不是二進制圖像。我會更新我的帖子。 'bwboundaries'和'bwperim'之間的區別在於'bwboundaries'將blob的邊界看作一組'(x,y)'點。 'bwperim'作爲圖像返回blob的邊界。此圖像對繪製像素以在圖上顯示該圖像至關重要。 – rayryeng

+0

非常感謝,它的工作現在完美:)。是否可以增加邊界/邊界的寬度?此外,如何將遮罩圖像頂部的質心繪製爲當前顯示在單獨窗口中的圖形。 – BlueBee