这是我收到的收据图像,我已经使用matplotlib绘制了它,
# x1, y1, x2, y2, x3, y3, x4, y4
bbox_coords = [[650, 850], [1040, 850], [1040, 930], [650, 930]]
image = cv2.imread(IMG_FILE)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r');
rect = Polygon(bbox_coords, fill=False, linewidth=1, edgecolor='r')
ax.add_patch(rect)
plt.show()
print(gray.shape)
(4376, 2885)
然后,我裁剪了原始的灰色图像,并使用相同的边界框坐标再次对其进行了绘制,这是结果,
# cropped the original image
gray_new = gray[25:4314, 147:2880]
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray_new, cmap='Greys_r');
rect = Polygon(bbox_coords, fill=False, linewidth=1, edgecolor='r')
ax.add_patch(rect)
plt.show()
print(gray_new.shape)
(4289, 2733)
因此,我正在寻找一种使边框适合裁切后的图像的方法。我不知道该如何实现。
编辑:
如果您想复制问题,receipt-2
这是另一张图片,这些是图片的b-box坐标[1638,1462,2974,1462,2974,1549,1638,1549]
。
如果您裁剪25
左侧像素和147
顶部像素,那么你必须。减去25
所有像素X
值和147
像素的Y
值,因为在图像中的所有elemenets移动25像素的左侧和147个像素的顶部。
box_coords = [
[650-25, 850-147],
[1040-25, 850-147],
[1040-25, 930-147],
[650-25, 930-147]
]
print(bbox_coords)
编辑:使用代码
bbox_coords = [[650, 850], [1040, 850], [1040, 930], [650, 930]]
bbox_coords = [[x-25, y-147] for x,y in bbox_coords]
print(bbox_coords)
顺便说一句:在右侧和底部裁剪多少像素都没有关系。
编辑:重新缩放图像的计算
计算保持比例的大小
old_width = 4376
old_height = 2885
new_width = 550
#new_height = 270 # doesn't keep proportion
new_height = int(new_width/(old_width/old_height)) # keep proportion
print('new size:', new_width, new_height)
print('proportions:', (old_width/old_height), (new_width/new_height))
new_image = resize(original_img, shape=(new_width, new_height))
当图像更改大小时计算位置(我假设它不保持比例)。
scale_x = old_width/new_width
scale_y = old_height/new_height
print('scale:', scale_x, scale_y)
bbox_coords = [[int(x/scale_x), int(y/scale_y)] for x,y in bbox_coords]
print(bbox_coords)
如果图像保持不变,则scale_x == scale_y
您可以计算所有值并仅使用一个比例尺。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句