Python 在图像上绘制纵横比为3:2的矩形

Python 在图像上绘制纵横比为3:2的矩形,python,opencv,image-processing,Python,Opencv,Image Processing,我试图画一个长宽比为3:2的矩形。 我正在使用OpenCV检测图像中的对象。因此,从输出值,我画了一个最小X,最小Y,最大X,最大Y的矩形。现在我需要使该矩形与起点(即最小X和最小Y)的纵横比为3:2 它不应超出原始图像的最大X和最大Y以及 矩形不应小于周围的现有矩形 检测到的对象 这里有一种方法可以解决这个问题: # determine the y / x length of the rectangle len_X = maxX - minX len_Y = maxY - minY # de

我试图画一个长宽比为3:2的矩形。 我正在使用OpenCV检测图像中的对象。因此,从输出值,我画了一个最小X,最小Y,最大X,最大Y的矩形。现在我需要使该矩形与起点(即最小X和最小Y)的纵横比为3:2

它不应超出原始图像的最大X和最大Y以及 矩形不应小于周围的现有矩形 检测到的对象


这里有一种方法可以解决这个问题:

# determine the y / x length of the rectangle
len_X = maxX - minX
len_Y = maxY - minY

# determine the largest side, this will be the 3 in the aspect ratio
if len_X > len_Y:
    # check if the shorter side is larger than a 3:2 ration
    if len_Y > len_X * (3/2):
        # if so, increase larger side to 3:2 ratio
        len_X = len_Y * 1.5
    else:
        # else, increase shorter side to 3:2 ratio
        len_Y = len_X * (3/2) 
else:
    # same as above
    if len_X > len_Y * (3/2):
        len_Y = len_X * 1.5
    else:
        len_X = len_Y * (3/2) 

# if the rectangle exceeds the image, constrain the rectangle
# other option (commented): move the starting position
if minX + len_X > img.shape[1]:
    len_X = img.shape[1]-minX
    #minX = img.shape[1]-len_X
if minY + len_Y > img.shape[0]:
    len_Y = img.shape[0]-minY
    #minY = img.shape[0]-len_Y

# draw the rectangle
cv2.rectangle(img, (minX, minY), (minX + len_X, minY + len_Y), (0,0,255),1)

首先计算新的长方体大小,使其w:h为3:2。然后,如果长方体的一侧比图像的一侧长,则修剪长方体

确定长方体大小后,我们计算长方体中心。默认情况下,长方体中心保持不变,但如果长方体穿过图像的边界,它将移动

最后,我们可以使用长方体大小和长方体中心来计算长方体角点的坐标

import cv2

def draw_rectangle(img, min_x, min_y, max_x, max_y):
    # resize box to 3:2(only enlarge it)
    # determine the box_w and box_h
    box_w, box_h = max_x-min_x, max_y-min_y
    if box_w/box_h < 3/2:
        box_w = int(box_h*(3/2))
    else:
        box_h = int(box_w*(2/3))

    # trim the box so it won't be bigger than image
    h, w = img.shape[:2]
    box_w = w if box_w > w else box_w
    box_h = h if box_h > h else box_h

    # determine the center of box

    # the default box center
    box_center_x = (min_x+max_x)//2
    box_center_y = (min_y+max_y)//2

    # shift the box if it cross the boundary
    if box_center_x + box_w//2 > w:
        box_center_x = w - box_w//2
    elif box_center_x - box_w//2 < 0:
        box_center_x = box_w//2
    if box_center_y + box_h//2 > h:
        box_center_y = h - box_h//2
    elif box_center_y - box_h//2 < 0:
        box_center_y = box_h//2

    # calculate the corner of the box
    min_x, max_x = box_center_x - box_w//2, box_center_x + box_w//2
    min_y, max_y = box_center_y - box_h//2, box_center_y + box_h//2
    cv2.rectangle(img, (min_x, min_y), (max_x, max_y), (255,0,0), thickness=10)
    return img

img = cv2.imread('image.jpg')
min_x, min_y, max_x, max_y = 0, 0, 400, 230
img = draw_rectangle(img, min_x, min_y, max_x, max_y)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
导入cv2
def绘制矩形(img、min_x、min_y、max_x、max_y):
#将框大小调整为3:2(仅放大)
#确定框w和框h
框w,框h=max\u x-min\u x,max\u y-min\u y
如果箱带/箱高<3/2:
方框w=int(方框h*(3/2))
其他:
方框h=int(方框w*(2/3))
#修剪长方体,使其不会比图像大
h、 w=图像形状[:2]
如果方框w>w,则方框w=w,否则方框w
如果box\u h>h,则box\u h=h,否则box\u h
#确定盒子的中心
#默认的长方体中心
长方体_中心_x=(最小_x+最大_x)//2
长方体中心y=(最小y+最大y)//2
#如果长方体跨越边界,请将其移动
如果长方体_中心_x+长方体_w//2>w:
长方体中心长方体x=w-长方体w//2
elif box\u center\u x-box\u w//2<0:
长方体_中心_x=长方体_w//2
如果长方体中心y+长方体h//2>h:
框体中心y=h-框体h//2
elif box\u center\u y-box\u h//2<0:
长方体中心y=长方体h//2
#计算长方体的角
最小值x,最大值x=box\u中心值x-box\u w//2,box\u中心值x+box\u w//2
最小值y,最大值y=box\u中心值y-box\u h//2,box\u中心值y+box\u h//2
cv2.矩形(img,(最小x,最小y),(最大x,最大y),(255,0,0),厚度=10)
返回img
img=cv2.imread('image.jpg')
最小值x,最小值y,最大值x,最大值y=0,0400230
img=绘制矩形(img、最小x、最小y、最大x、最大y)
cv2.imshow(“图像”,img)
cv2.等待键(0)
cv2.destroyAllWindows()

您需要任何3:2的新矩形(
r\u new
),以便(i)
r\u new
r\u old
的交点等于
r\u old
,并且(ii)
r\u new
不应外推“原始”图像。是这样吗?是的@BerrielHi J.D.我试过你的答案,它给出的是3/2.5的比例,而不是3/2的比例。你能提供一个例子吗?我需要3:2的比例,但你是2:3的比例,无论如何,它是通过交换来工作的。还有一件事,3/2的结果是1.5,浮动,所以我们应该用3.0/2.0代替3/2。很抱歉,我想你的意思是h:w=3:2。我使用的是Python3,所以代码适合我。我不这么认为。我现在看不见了。@Codecracker还有什么可以帮你的吗?