2012-10-03 59 views
4

所以,我有一個RGB圖像,並且我在圖像的一個區域周圍放置了一個矩形(邊界框)。但是,我無法獲得沿矩形周長的像素值。matlab圖像:沿圖像中的邊界框返回像素值?

我試過尋找功能regionprops,但沒有找到有用的東西。

我想我可以從瞭解的(x,y)點名單沿boundingBox的獲得的像素值(x_inity_initx_widthy_width),但沒有具體的函數,該函數。誰能幫忙?

回答

2

我不知道是否有在圖像處理工具箱這個特定的功能,但你所描述的功能是很簡單的實現自己:

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 
+0

,所以我的埋入「IMG」用我的形象 – user1715908

+1

的像素矩陣,但有一個錯誤 - >標指標必須是真正的正整數或邏輯。 – user1715908

+0

所以我需要做一個循環的上,左,右,下? 有沒有更簡單的方法來處理它? – user1715908

0

對於其他指的這個問題, ,我有同樣的問題,並使用Rody Oldenhuis方法,但在我的情況下效果不佳。

您可以使用內置的功能的MATLAB此:

imgRect=getrect;//get a rectangle region in image 
    cropedImg=imcrop(orgImg,[xtopleft ytopleft width height]);//in croppedImg you have the value of specified region