Question

I have a rotated rectangle representing my region of interest:

[[634 547]
[353 504]
[436 -41]
[717   1]]

I'd like to quickly filter out all pixels not inside of that rotated rectangle. Note that the rectangle actually is beyond the bounds of the image, so I have to deal with that edge case if it matters.

Était-ce utile?

La solution

You can use fillPoly() to draw the rectangle on a mask array and you the mask array to select the pixels that you want:

import cv2
import numpy as np
rect = [[634, 547],[353, 504],[436, -41],[717,   1]]
poly = np.array([rect], dtype=np.int32)
img = np.zeros((800, 800), np.int8)
cv2.fillPoly(img, poly, 255)
imshow(img, cmap="gray")

the mask image:

enter image description here

Autres conseils

  • Set an outscribed, upright rectangle as ROI in your image (setRoi),
  • Create a mask image for the rotated rectangle (fillPoly)
  • Apply whatever image operation you would like, and specify (the optional) mask argument.

For example you can:

  • Invert the mask to select the defect between the ROI and the rotated rectangle;
  • Set the defect pixels to zero, using the mask.
  • Display the ROI
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top