裁剪图像后,如何找到新的边界框坐标? [英] After cropping a image, how to find new bounding box coordinates?

查看:139
本文介绍了裁剪图像后,如何找到新的边界框坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我收到的收据图像,我已使用matplotlib对其进行了绘制,

Here's a receipt image that I've got and I've plotted it using 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.

如果您想复制问题,请使用另一张图片 receipt-2 ,这些是b-图像[1638,1462,2974,1462,2974,1549,1638,1549]的框坐标.

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].

推荐答案

如果在左侧裁剪25像素,在顶部裁剪147像素,则必须从所有X值中减去25像素和147像素(来自Y值) 因为图像上的所有元素都向左移动25个像素,向顶部移动147个像素.

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.

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)

顺便说一句:,在右侧和底部裁剪多少像素都没有关系.

BTW: and it doesn't matter how many pixels you cropped on right and bottom.

编辑:重新缩放图像的计算

Calculation for rescaling image

计算尺寸并保持比例

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)

如果图像保持旋转,则scale_x == scale_y,并且您只能为所有值计算和使用一个比例尺.

If image keeps propotion then scale_x == scale_y and you can calculate and use only one scale for all values.

这篇关于裁剪图像后,如何找到新的边界框坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆