从图像中移除/擦除对象使用Python。

huangapple go评论73阅读模式
英文:

Remove/Erase object from image using python

问题

以下是您提供的内容的中文翻译:

我一直在尝试使用OpenCV从图像中删除/擦除用红色突出显示的对象 从图像中移除/擦除对象使用Python。

我已经取得了一些接近的结果,但它只是降低了对象的透明度
从图像中移除/擦除对象使用Python。 但我希望它完全消失。

以下是代码:

import numpy as np
import sys

input = sys.argv[1]

# 加载图像
img = cv2.imread(input)

# 定义要删除的对象的颜色范围
lower_red = np.array([0, 0, 200])
upper_red = np.array([50, 50, 255])

# 创建一个标识要删除对象的掩码
mask = cv2.inRange(img, lower_red, upper_red)

# 使用TELEA算法修复对象区域
inpaint = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)

# 保存修复后的图像
cv2.imwrite(input, inpaint)
英文:

I have been trying to Remove/Erase an object from image which is highlighted with red color using opencv 从图像中移除/擦除对象使用Python。

I have achieved a bit closer result but it's just reducing the transparency of the object
从图像中移除/擦除对象使用Python。 but i want it to make it completely invisible.

Here is the code:

import numpy as np
import sys

input = sys.argv[1]

# Load the image
img = cv2.imread(input)

# Define the color range of the object to be removed
lower_red = np.array([0, 0, 200])
upper_red = np.array([50, 50, 255])

# Create a mask that identifies the object to be removed
mask = cv2.inRange(img, lower_red, upper_red)

# Inpaint the object area using the TELEA algorithm
inpaint = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)

# Save the inpainted image
cv2.imwrite(input, inpaint)

答案1

得分: 2

以下是翻译好的内容:

"边缘可能存在问题,无法覆盖所有偏红的像素。您可以膨胀掩码:

import cv2
import numpy as np
import sys

input = sys.argv[1]

# 加载图像
img = cv2.imread(input)

# 定义要移除的对象的颜色范围
lower_red = np.array([0, 0, 150])
upper_red = np.array([100, 100, 255])

# 创建一个标识要移除的对象的掩码
mask = cv2.inRange(img, lower_red, upper_red)

# 使用TELEA算法修复对象区域
kernel = np.ones((5,5),np.uint8)
dilated_mask = cv2.dilate(mask,kernel,iterations = 1)
inpaint = cv2.inpaint(img, dilated_mask, 3, cv2.INPAINT_TELEA)

# 保存修复后的图像
cv2.imwrite('out_'+input, inpaint)
英文:

The mask may have problem on the edges, and not covering all red-ish pixels. You can dilated the mask:

import cv2
import numpy as np
import sys

input = sys.argv[1]

# Load the image
img = cv2.imread(input)

# Define the color range of the object to be removed
lower_red = np.array([0, 0, 150])
upper_red = np.array([100, 100, 255])

# Create a mask that identifies the object to be removed
mask = cv2.inRange(img, lower_red, upper_red)

# Inpaint the object area using the TELEA algorithm
kernel = np.ones((5,5),np.uint8)
dilated_mask = cv2.dilate(mask,kernel,iterations = 1)
inpaint = cv2.inpaint(img, dilated_mask, 3, cv2.INPAINT_TELEA)

# Save the inpainted image
cv2.imwrite('out_'+input, inpaint)

答案2

得分: 2

Firstly, you need to save your mask as an image so you can check how well it is selecting your reds:

首先,您需要将您的遮罩保存为图像,以便检查它是否正确选择了红色:

cv2.imwrite("mask.png", mask)


Secondly, you will probably find it more accurate to locate highly saturated reds (and greens etc) in HSL/HSV colourspace. So, have a look for other answers tagged [tag:opencv] and containing "HSV". You'll need

其次,您可能会发现在[HSL/HSV][1]颜色空间中更准确地定位高饱和度的红色(以及绿色等)。因此,请查看其他标记为[tag:opencv]并包含"HSV"的答案。您需要:

HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

and be aware that reds wrap around zero, because the hue wheel is cyclic. See example here.

英文:

Firstly, you need to save your mask as an image so you can check how well it is selecting your reds:

cv2.imwrite("mask.png", mask)

Secondly, you will probably find it more accurate to locate highly saturated reds (and greens etc) in HSL/HSV colourspace. So, have a look for other answers tagged [tag:opencv] and containing "HSV". You'll need

HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

and be aware that reds wrap around zero, because the hue wheel is cyclic. See example here.

huangapple
  • 本文由 发表于 2023年5月11日 13:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定