简体   繁体   English

使用 python (openCV) 删除背景图像

[英]Remove Background Image with python (openCV)

I am new to computer version, I am trying to remove the background of the image given and make it white background.我是计算机版本的新手,我正在尝试删除给定图像的背景并将其设为白色背景。 I have tried most of the codes shared here but non is working on my image.我已经尝试了这里共享的大部分代码,但没有在我的图像上工作。

input:输入:

删除背景的图像

desired output:所需的 output:

去除背景后(参考图片)

If you remove background you not have your desider output.如果你删除背景,你就没有你的设计者 output。

在此处输入图像描述

The object in the image have a different processing, similar negative, but isn't negative.图像中的object有不同的处理,类似负片,但不是负片。

If it was negative you got this result:如果它是否定的,你会得到这个结果:
负像

From output image it's very difficult understand what operations have been carried out.从 output 图像很难理解执行了哪些操作。

Here is a simple way to do that in Python/OpenCV/Numpy.这是在 Python/OpenCV/Numpy 中执行此操作的简单方法。

Convert to gray.转换为灰色。 Copy the input and use Numpy to change the background to white where gray is close to black.复制输入并使用 Numpy 将背景更改为白色,灰色接近黑色。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# read image
img = cv2.imread("a_blob.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# change black to white
result = img.copy()
result[gray<=2] = (255,255,255)

# write results to disk
cv2.imwrite("a_blob_white.jpg", result)

# show results
cv2.imshow("RESULT", result)
cv2.waitKey(0)

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM