2014-10-31 37 views
0

我是Matlab中的新手。在每幀實時視頻中查找指定顏色

我的老闆給了我一個任務,我卡在最後一步。

任務是: 用戶應該在實時視頻快照中指定一個矩形,然後我應該檢測每個視頻幀中該矩形的平均顏色。

第一用戶指定視頻的與該代碼的快照矩形的邊界:

test = getsnapshot(vid); 
    imwrite(test,'mytest.png','png'); 
    testsnap = imread('mytest.png'); 
    Rectpos=getrect;  

然後我計算平均每R , G , B度:

bbx=boundingboxPixels(testsnap,Rectpos(1),Rectpos(2),Rectpos(3),Rectpos(4)); 
    rRect=mean(bbx(:,:,1)); 
    gRect=mean(bbx(:,:,2)); 
    bRect=mean(bbx(:,:,3)); 

其中boundingboxPixels方法是這樣的:

function pixel_vals = boundingboxPixels(img, x_init, y_init, x_width, y_width) 

    if x_init > size(img,2) 
     error('x_init lies outside the bounds of the image.'); end 
    if y_init > size(img,1) 
     error('y_init lies outside the bounds of the image.'); end 

    if y_init+y_width > size(img,1) || x_init+x_width > size(img,2) || ... 
      x_init < 1 || y_init < 1 
     warning([... 
      'Given rectangle partially falls outside image. ',... 
      'Resizing rectangle...']); 
    end 

    x_min = max(1, uint16(x_init)); 
    y_min = max(1, uint16(y_init)); 
    x_max = min(size(img,2), x_min+uint16(x_width)); 
    y_max = min(size(img,1), y_min+uint16(y_width)); 
    x_range = x_min : x_max; 
    y_range = y_min : y_max; 

    Upper = img(x_range, y_min , :); 
    Left = img( x_min, y_range, :); 
    Right = img( x_max, y_range, :); 
    Lower = img(x_range, y_max , :); 

    pixel_vals = [... 
     Upper 
     permute(Left, [2 1 3]) 
     permute(Right, [2 1 3]) 
     Lower]; 

end 

然後獲得計算的平均值RGB顏色與視頻的每一幀閾值:

tv=getdata(vid,1);//vid is real-time video    
r=tv(:,:,1,1); 
g=tv(:,:,2,1); 
b=tv(:,:,3,1); 

redVal = (r >= rRect-thr) & (r <= rRect+thr); 
greenVal = (g >= gRect-thr) & (g <= gRect+thr); 
blueVal = (b >= bRect-thr) & (b <= bRect+thr); 

現在我應該如何使用redVal , greenVal , blueVal檢測到這種顏色?

+3

之間加入&公正,他們()來解決,你會得到其中的人描述的位置,其中閾值的顏色是一個合乎邏輯的矩陣。 – Steffen 2014-10-31 10:30:48

+0

@Steffen謝謝,但我如何將矩陣位置轉換爲顏色值矩陣? – Arash 2014-10-31 12:10:35

+1

我認爲redVal描述了紅色組件被檢測到的位置,對於綠色和藍色也是如此。通過和你會發現所有三個組件都在範圍內的位置。由於您指定了要檢測的顏色,爲什麼要使用檢測到的位置來獲取檢測到的顏色? – Steffen 2014-10-31 12:18:00

回答

0

爲斯特芬說,問題由陣列