4

So, I have an RGB image and I've put a rectangle (boundingbox) around an area of the image. However, I can't get the pixel values along the perimeter of this rectangle.

I've tried looking into the function regionprops but didn't find anything useful.

I thought I could obtain pixel values from knowing the list of (x,y) points along the boundingbox ( x_init, y_init, x_width, y_width) but there's no specific function for that. Can anyone help?

4

2 に答える 2

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
于 2012-10-03T04:28:31.727 に答える
0

For any other refer to this question,i had same problem and used Rody Oldenhuis method,but it did not work well in my condition.

you can use matlab built in functions for this :

  imgRect=getrect;//get a rectangle region in image
  cropedImg=imcrop(orgImg,[xtopleft ytopleft width height]);//in croppedImg you have the value of specified region
于 2014-10-31T13:28:46.377 に答える