如果裁剪四角的坐标,我如何裁剪图像

问题描述 投票:0回答:2

我需要使用python从车的图像中裁剪车牌,给出板块边界框的坐标。 (4坐标)。有关如何做到这一点的任何提示?

我有以下代码,但没有按预期工作。

> x1, y1: 1112 711 
> x2, y2: 1328 698 
> x3, y3: 1330 749 
> x4, y4: 1115 761
image = cv2.imread(IMAGE_PATH)
fixed_image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

new_img = cv2.rectangle(fixed_image, (x3,y3), (x1,y1), (0, 255, 0), 5) 

plt.figure(figsize=(12,13))
plt.imshow(new_img)

Image for reference

Cropped image

谢谢。

python opencv matplotlib image-processing crop
2个回答
0
投票

因为你得到的坐标是一个POLYGON而不是RECTANGLE,你必须在你的切片中做一些调整,最简单的做法是调整你的矩形:

x1,y1:1112 711 x2,y2:1328 698 x3,y3:1330 749 x4,y4:1115 761

top_left_x = min([x1,x2,x3,x4])
top_left_y = min([y1,y2,y3,y4])
bot_right_x = max([x1,x2,x3,x4])
bot_right_y = max([y1,y2,y3,y4])

现在你可以做到

img[top_left_y:bot_right_y, top_left_x:bot_right_x]

请注意切片不包括终点,所以你可能想要这样做

img[top_left_y:bot_right_y+1, top_left_x:bot_right_x+1]

1
投票

在OpenCV中,如果要裁剪印版,可以执行以下操作

import cv2
img = cv2.imread("image.png")
cropped__img = img[y1:y2, x1:x2]

在这里也回答:How to crop an image in OpenCV using Python

或者将像素的颜色更改为白色或黑色(或任何其他颜色)。

import cv2
img = cv2.imread("image.png")
img[y1:y2, x1:x2] = [255,255,255]
© www.soinside.com 2019 - 2024. All rights reserved.