我正在尝试裁剪文件夹中包含的所有图像。 我想将对象保留在图片中,并通过复制裁剪后的图像并将其粘贴到与从源获得的坐标相同的另一个文件中使背景变黑。
我将所有坐标存储在具有这种结构的 csv 文件中:
文件名 | xmax | 最小 | ymax | ymin |
---|---|---|---|---|
a.jpg | 768 | 4 | 512 | 8 |
b.jpg | 1200 | 3 | 899 | 10 |
我使用此代码来实现我的目标:
import cv2
import numpy as np
save_path = '/content/drive/MyDrive/Colab Notebooks/object_cropped'
train_path = '/content/train'
with open('min_max.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
xmax = int(row['xmax'])
xmin= int(row['xmin'])
ymax = int(row['ymax'])
ymin = int(row['ymin'])
image = cv2.imread(os.path.join(train_path , row['Filename']))
object_ = image[ymin:xmax, xmin:ymax]
h, w, c = image.shape
black_background = np.zeros([h, w, 3])
for y in range(h):
for x in range(w):
black_background[y,x] = [0,0,0]
black_background[ymin:xmax, xmin:ymax] = object_
cv2.imwrite(os.path.join(save_path , 'cropped_' + row['Filename']), black_background)
它适用于从网络拍摄的图像,但是当我尝试将此代码应用于手机拍摄的照片时,它不起作用。
两张图片(网络图片和手机图片)都是 jpg,横向和纵向分辨率:96dpi 位深度:24,唯一不同的是图片大小。
我该如何解决这个问题?
编辑: 示例:
我需要裁剪边界框内的对象。
使用的值:
xmax = 949
xmin= 489
ymax = 1829
ymin = 181
这种情况只发生在用手机或平板电脑拍摄的照片上。
答案 0 :(得分:1)
问题是 object_ = image[ymin:xmax, xmin:ymax]。解决办法是:
import cv2
import numpy as np
save_path = '/content/drive/MyDrive/Colab Notebooks/object_cropped'
train_path = '/content/train'
with open('min_max.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
xmax = int(row['xmax'])
xmin= int(row['xmin'])
ymax = int(row['ymax'])
ymin = int(row['ymin'])
image = cv2.imread(os.path.join(train_path , row['Filename']))
object_ = image[ymin:ymax, xmin:xmax] ####the right solution
h, w, c = image.shape
black_background = np.zeros([h, w, 3])
for y in range(h):
for x in range(w):
black_background[y,x] = [0,0,0]
black_background[ymin:ymax, xmin:xmax] = object_ ####the right solution
cv2.imwrite(os.path.join(save_path , 'cropped_' + row['Filename']), black_background)