- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有数百张珠宝产品图片。其中一些带有“畅销书”标签。标签的位置因图像而异。我想遍历所有图像,如果图像有此标签,则将其删除。生成的图像将在移除对象的像素上渲染背景。
带有标签/贴纸/对象的图像示例:
要删除的标签/贴纸/对象:
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 边缘检测。应用与边缘匹配的模板而不是原始图像可以消除颜色变化差异并提供更稳健的结果。从模板图像中提取边缘:
接下来我们不断缩小图像并对调整大小的图像应用模板匹配。我使用 old answer 在每次调整大小时保持纵横比.这是策略的可视化
我们调整图像大小的原因是因为使用 cv2.matchTemplate
的标准模板匹配不稳健,如果模板和图像的尺寸不匹配,可能会产生误报。为了克服这个尺寸问题,我们使用这种修改后的方法:
cv2.matchTemplate
应用模板匹配并跟踪最大的相关系数一旦获得 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/
是否可以使用 Facebook Messenger 的新 API 发送原生贴纸? 我尝试使用以下代码,但贴纸显示为下载的图像,而不是原始形式 messageData = { "
我正在创建一个 iMessage 贴纸包扩展,其中包含包含文本的贴纸。我在 Xcode 中没有看到任何本地化贴纸的选项?是否可以本地化贴纸? 最佳答案 也许这很明显,但您可以在扩展程序中管理它。如果您
我构建了一个消息应用程序扩展,灵感来自 Ice Cream Builder(Apple 示例代码):https://developer.apple.com/library/content/sample
所以我的问题的基本动机有点模糊: 我想跟踪用户对特定商店的忠诚度。每家商店的收银机上都会有一张 NFC 标签。购买时,用户会在他们的手机上使用一个应用程序(当然可以使用 NFC),贴纸会发回一些指定商
我一直在考虑开发我自己的自定义 Viber 贴纸,但我找不到任何好的来源来给我一些这方面的信息。 有人知道怎么做吗?或者有可能吗?如果是,请告诉我我需要做什么。 谢谢。 最佳答案 当您发送/接收贴纸时
我尝试使用 Google 键盘 Gboard 等键盘将表情符号插入到我的 edittext 但它显示 toast 此文本字段不支持从键盘插入 GIF. 有几个关于相同的问题,但没有正确的答案。我读了d
我想构建一个 Android 应用程序,它可以获取一组图像并制作一个 Telegram 贴纸包。我知道如何通过@stickers bot 创建贴纸。但我想自动完成。我应该做什么以及最好的方法是什么?
我有数百张珠宝产品图片。其中一些带有“畅销书”标签。标签的位置因图像而异。我想遍历所有图像,如果图像有此标签,则将其删除。生成的图像将在移除对象的像素上渲染背景。 带有标签/贴纸/对象的图像示例: 要
我正在将 Noble/Bleacon 与 Raspberry-Pi 和 MacbookProRetina 一起使用,并且我试图发现 Estimote 贴纸,但这确实很困难,因为它每 X 分钟广播一次
我正在尝试在我的应用中实现图像键盘支持。我关注了official documentation .为了支持这一点,我需要重写 EditText 的 onCreateInputConnection 来告诉
我是一名优秀的程序员,十分优秀!