gpt4 book ai didi

python - 如何使用 OpenCV 从图像中删除特定标签/贴纸/对象?

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:28 26 4
gpt4 key购买 nike

我有数百张珠宝产品图片。其中一些带有“畅销书”标签。标签的位置因图像而异。我想遍历所有图像,如果图像有此标签,则将其删除。生成的图像将在移除对象的像素上渲染背景。

带有标签/贴纸/对象的图像示例:

enter image description here

要删除的标签/贴纸/对象:

enter image description here

import numpy as np
import cv2 as cv

img = plt.imread('./images/001.jpg')
sticker = plt.imread('./images/tag.png',1)
diff_im = cv2.absdiff(img, sticker)

我希望结果图像是这样的:

最佳答案

这是一个使用修改后的尺度不变的方法 Template Matching方法。总体策略:

  • 加载模板,转换为灰度,执行canny边缘检测
  • 加载原始图像,转换为灰度
  • 不断重新缩放图像,使用边缘应用模板匹配,并跟踪相关系数(值越高表示匹配越好)
  • 找到最适合边界框的坐标,然后删除不需要的 ROI

首先,我们加载模板并执行 Canny 边缘检测。应用与边缘匹配的模板而不是原始图像可以消除颜色变化差异并提供更稳健的结果。从模板图像中提取边缘:

enter image description here

接下来我们不断缩小图像并对调整大小的图像应用模板匹配。我使用 old answer 在每次调整大小时保持纵横比.这是策略的可视化

enter image description here

我们调整图像大小的原因是因为使用 cv2.matchTemplate 的标准模板匹配不稳健,如果模板和图像的尺寸不匹配,可能会产生误报。为了克服这个尺寸问题,我们使用这种修改后的方法:

  • 以各种较小的比例连续调整输入图像的大小
  • 使用 cv2.matchTemplate 应用模板匹配并跟踪最大的相关系数
  • 具有最大相关系数的比率/尺度将具有最佳匹配的 ROI

一旦获得 ROI,我们就可以通过使用白色填充矩形来“删除” Logo

cv2.rectangle(final, (start_x, start_y), (end_x, end_y), (255,255,255), -1)

检测到 -> 已删除

import cv2
import numpy as np

# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]

# Return original image if no need to resize
if width is None and height is None:
return image

# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))

# Return the resized image
return cv2.resize(image, dim, interpolation=inter)

# Load template, convert to grayscale, perform canny edge detection
template = cv2.imread('template.png')
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
cv2.imshow("template", template)

# Load original image, convert to grayscale
original_image = cv2.imread('1.png')
final = original_image.copy()
gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
found = None

# Dynamically rescale image for better template matching
for scale in np.linspace(0.2, 1.0, 20)[::-1]:

# Resize image to scale and keep track of ratio
resized = maintain_aspect_ratio_resize(gray, width=int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])

# Stop if template image size is larger than resized image
if resized.shape[0] < tH or resized.shape[1] < tW:
break

# Detect edges in resized image and apply template matching
canny = cv2.Canny(resized, 50, 200)
detected = cv2.matchTemplate(canny, template, cv2.TM_CCOEFF)
(_, max_val, _, max_loc) = cv2.minMaxLoc(detected)

# Uncomment this section for visualization
'''
clone = np.dstack([canny, canny, canny])
cv2.rectangle(clone, (max_loc[0], max_loc[1]), (max_loc[0] + tW, max_loc[1] + tH), (0,255,0), 2)
cv2.imshow('visualize', clone)
cv2.waitKey(0)
'''

# Keep track of correlation value
# Higher correlation means better match
if found is None or max_val > found[0]:
found = (max_val, max_loc, r)

# Compute coordinates of bounding box
(_, max_loc, r) = found
(start_x, start_y) = (int(max_loc[0] * r), int(max_loc[1] * r))
(end_x, end_y) = (int((max_loc[0] + tW) * r), int((max_loc[1] + tH) * r))

# Draw bounding box on ROI to remove
cv2.rectangle(original_image, (start_x, start_y), (end_x, end_y), (0,255,0), 2)
cv2.imshow('detected', original_image)

# Erase unwanted ROI (Fill ROI with white)
cv2.rectangle(final, (start_x, start_y), (end_x, end_y), (255,255,255), -1)
cv2.imshow('final', final)
cv2.waitKey(0)

关于python - 如何使用 OpenCV 从图像中删除特定标签/贴纸/对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56132149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com