如何在不同大小的矩形中裁剪相同的ROI

How to crop the same ROI in a rectangle with different sizes

我想裁剪图像的一些区域(ROI),我目前正在从图像中提取一个矩形形状,在这个形状中我想提取一些始终位于同一局部但 rectangle/image将有不同的分辨率但相同的比例(身份证比例)所以我不能像我现在做的那样使用固定坐标:

 ((x1,y1),(x2,y2)) = box.position

 print(box.position)

 cv.rectangle(cvImage, (x1, y1), (x2, y2), (255,0,0), 2)

但这并不适用于所有图像,我想我已经说明了这一点:)

我如何使用 like % 或类似的东西来始终在矩形中获得相同的点,而不考虑矩形的 resolution/size。

谢谢。

是的,您总是可以 select 相同比例的图像。例如,提取一个坐标由固定比率(或百分比)定义的矩形

img_w = 1000 # = cvImage.width
img_h = 1000 # = cvImage.height
tl_x = 10.0 / 100.0
tl_y = 10.0 / 100.0
br_x = 90.0 / 100.0
br_y = 90.0 / 100.0

print (tl_x, tl_y, br_x, br_y)

rect_tl = (int(tl_x * img_w), int(tl_y * img_h))
rect_br = (int(br_x * img_w), int(br_y * img_h))

print (rect_tl, rect_br)

#cv.rectangle(cvImage, rect_tl, rect_br, (255,0,0), 2)

这将计算大小为 1000x1000 的图像中矩形的坐标,但它是一个定义为 img_w x img_h

的变量

硬编码矩形放置有 10% 的填充,使用 top-left 和 bottom-right 角定义

正如预期的那样,输出是:

0.1 0.1 0.9 0.9
(100, 100) (900, 900)

您可以调整阅读图像的大小,并可以裁剪相同的区域:

image = cv2.imread("image,jpg")
resize_image = cv2.resize(image, (640, 480)) 
...