[英]After cropping a image, how to find new bounding box coordinates?
Here's a receipt image that I've got and I've plotted it using matplotlib,这是我得到的一张收据图片,我已经使用 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)
Then, I've cropped the original gray image and plotted it again with same bounding box coordinates and here's the result,然后,我裁剪了原始的灰色图像并使用相同的边界框坐标再次绘制,结果如下,
# 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)
So, I'm looking for a way to make bounding box to fit the cropped image .所以,我正在寻找一种方法来制作边界框以适应裁剪后的图像。 I couldn't figure out how I can achieve it.我不知道如何实现它。
Edit:编辑:
Here's an another image if you want to replicate the question, receipt-2
and these are the b-box coordinates for the image [1638,1462,2974,1462,2974,1549,1638,1549]
.如果您想复制问题,这是另一个图像, receipt-2
,这些是图像[1638,1462,2974,1462,2974,1549,1638,1549]
的 b 框坐标。
If you cropped 25
pixels on the left and 147
pixels on the top then you have to substract 25
pixels from all X
values and 147
pixels from Y
values because all elemenets on image moved 25 pixel to the left and 147 pixels to the top.如果您在左侧裁剪了25
147
像素,在顶部裁剪了147
像素,那么您必须从所有X
值中减去25
像素,从Y
值中减去147
像素,因为图像上的所有元素都向左移动了 25 个像素,向顶部移动了 147 个像素。
box_coords = [
[650-25, 850-147],
[1040-25, 850-147],
[1040-25, 930-147],
[650-25, 930-147]
]
print(bbox_coords)
EDIT: Using code编辑:使用代码
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)
BTW: and it doesn't matter how many pixels you cropped on right and bottom.顺便说一句:你在右边和底部裁剪了多少像素并不重要。
EDIT: Calculation for rescaling image编辑:重新缩放图像的计算
Calculate size which keep proportions计算保持比例的大小
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))
Calculate position when image change size (I assume that it doesn't keep proportions).当图像改变大小时计算位置(我假设它不保持比例)。
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)
If image keeps propotion then scale_x == scale_y
and you can calculate and use only one scale for all values.如果图像保持比例,则scale_x == scale_y
并且您可以计算所有值并仅使用一个比例。
You have to move the coordinates of the Polygon, the same quantity that you cropped in x
and in y
coordinates.您必须移动多边形的坐标,与您在x
和y
坐标中裁剪的数量相同。
Take into account that when you apply gray_new = gray[25:4314, 147:2880]
, this means [rows, columns]
, so for the plot, you are removing the first 25 pixels on y
axis and the first 147 pixels on x
axis.考虑到当您应用gray_new = gray[25:4314, 147:2880]
,这意味着[rows, columns]
,因此对于绘图,您将删除y
轴上的前 25 个像素和x
上的前 147 个像素轴。
The results would be结果将是
bbox_coords = [[x - 147, y-25] for x, y in bbox_coords]
and in values:和价值观:
bbox_coords = [[503, 825], [893, 825], [893, 925], [503, 925]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.