为包含裁剪图像的给定大小创建新图像

2024-06-02 06:40:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在进行以下工作,并努力理解最佳方法。 我已经搜索了很多,但是没有找到与我所尝试的匹配的答案

问题:

  1. 将现有图像(白色背景)中的对象(如鞋)重新定位到特定位置(如上移)
  2. 使用用户指定的新高度/宽度,在新背景(仍为白色)内的用户指定位置插入和定位对象(如鞋)

enter image description here

我走了多远:

我已经设法用CV2识别了图片中的对象,获得了外部轮廓,添加了一点填充并裁剪了对象(见下文)。我很高兴这样裁剪,因为我所有的图像都有一个单色背景,我会保持背景的颜色不变

我被困的地方: 我的裁剪对象和旧图像背景/新背景不共享相同的形状,因此我无法覆盖/连接/合并。。。 鉴于这两幅图像都存储为np数组,我假设答案是以某种方式将Shoe crop np.array放置在背景np.array中,但是我不知道如何执行此操作。 也许有一种更简单/不同的方法可以做到这一点

如果有人能带领我走向正确的方向,我将非常感激

代码

#importing dependencies    
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt 

# Config 
path = '/Users/..../Shoes/'
img_list = os.listdir(path)
img_path = path + img_list[0]
#Outline
color = (0,255,0)
thickness = 3
padding = 10

# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Identifying outer contours 
x_axis = []
y_axis = []
for i in range(len(contours)):
    for y in range (len(contours[i])):
        x_axis.append(contours[i][y][0][0])
        y_axis.append(contours[i][y][0][1])

min_x = min(x_axis) - padding
min_y = min(y_axis) - padding
max_x = max(x_axis) + padding
max_y = max(y_axis) + padding

# Defining start and endpoint of outline Rectangle based on identified outer corners + Padding 
start_point = (min_x, min_y) 
end_point = (max_x, max_y) 

image_outline = cv2.rectangle(image, start_point, end_point, color, thickness)

plt.imshow(image_outline)
plt.show()

#Crop Image
crop_img = image[min_y:max_y, min_x:max_x]
print(crop_img.shape)
plt.imshow(crop_img)
plt.show()

1条回答
网友
1楼 · 发布于 2024-06-02 06:40:49

我想我找到了解决方案,这将使图像居中于任何新的给定背景高度/宽度

仍然对更快/更干净的方式感兴趣

#Define the new height and width you want to have  
new_height = 1200
new_width = 1200
#Check current hight and with of Cropped image 
crop_height = crop_img.shape[0]
crop_width = crop_img.shape[1]

#calculate how much you need add to the sides and top - basically halft of the remaining height / with ... currently not working correctly for odd numbers
add_sides = int((new_width - crop_width)/2)
add_top_and_btm = int((new_height - crop_height)/2)

# Adding background to the sides
bg_sides = np.zeros(shape=[crop_height, add_sides, 3], dtype=np.uint8)
bg_sides2 = 255 * np.ones(shape=[crop_height, add_sides, 3], dtype=np.uint8)
new_crop_img = np.insert(crop_img, [1], bg_sides2, axis=1)
new_crop_img = np.insert(new_crop_img, [-1], bg_sides2, axis=1)

# Then adding Background to top and bottom 
bg_top_and_btm = np.zeros(shape=[add_top_and_btm, new_width, 3], 
dtype=np.uint8)
bg_top_and_btm2 = 255 * np.ones(shape=[add_top_and_btm, new_width, 3], 
dtype=np.uint8)
new_crop_img = np.insert(new_crop_img, [1], bg_top_and_btm2, axis=0)
new_crop_img = np.insert(new_crop_img, [-1], bg_top_and_btm2, axis=0)

plt.imshow(new_crop_img) 

enter image description here

相关问题 更多 >