使用 python 裁剪手机图像

Crop cellphone image with python

我正在尝试裁剪文件夹中包含的所有图像。 我想保留图片中的对象并通过复制裁剪后的图像使背景变黑,然后将其粘贴到与从源中获得的坐标相同的另一个文件中。

我将所有坐标存储在具有以下结构的 csv 文件中:

文件名 xmax xmin 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

只有用手机或平板电脑拍摄的照片才会出现这种情况。

问题是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)