Matlab image: return pixel values ​​along bounding box in image?

So, I have an RGB image and I put a rectangle (bounding) around the image area. However, I cannot get the pixel values ​​around the perimeter of this rectangle.

I tried looking into the regionprops function, but did not find anything useful.

I thought I could get pixel values ​​from knowing the list (x,y) points along the bounding box ( x_init , y_init , x_width , y_width ), but there is no specific function for this. Can anyone help?

+4
source share
2 answers

I don’t know if there is any specific function in the image processing toolbar, but the function you describe is simple enough to implement:

 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 
+2
source

For any other question related to this question, I had the same problem and I used the Rody Oldenhuis method, but in my state this did not work.

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

Source: https://habr.com/ru/post/1437503/


All Articles