使用 OpenCV 在 Python 中反转图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19580102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:06:05  来源:igfitidea点击:

inverting image in Python with OpenCV

pythonarraysopencvnumpyimage-processing

提问by Mansueli

I want to load a color image, convert it to grayscale, and then invert the data in the file.

我想加载一个彩色图像,将其转换为灰度,然后反转文件中的数据。

What I need: to iterate over the array in OpenCV and change every single value with this formula (it might be wrong but it seems reasonable for me):

我需要的是:在 OpenCV 中迭代数组并使用此公式更改每个值(这可能是错误的,但对我来说似乎是合理的):

img[x,y] = abs(img[x,y] - 255)

but I don't understand why doesn't it works:

但我不明白为什么它不起作用:

def inverte(imagem, name):
    imagem = abs(imagem - 255)
    cv2.imwrite(name, imagem)


def inverte2(imagem, name):
    for x in np.nditer(imagem, op_flags=['readwrite']):
        x = abs(x - 255)
    cv2.imwrite(name, imagem)


if __name__ == '__main__':
    nome = str(sys.argv[1])
    image = cv2.imread(nome)
    gs_imagem = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    inverte(gs_imagem, "invertida.png")
    inverte2(gs_imagem, "invertida2.png")

I don't want to do an explicit loop (I am trying to be more pythonic). I can see that in one image that got a white background it turned black, but only this it doesn't looks like the other colors are having much (if any) change.

我不想做一个显式循环(我试图变得更加 Pythonic)。我可以看到,在一张白色背景的图像中,它变成了黑色,但仅此而已,其他颜色看起来并没有太多(如果有)变化。

采纳答案by Saullo G. P. Castro

You almost did it. You were tricked by the fact that abs(imagem-255)will give a wrong result since your dtypeis an unsigned integer. You have to do (255-imagem)in order to keep the integers unsigned:

你几乎做到了。你被一个事实欺骗了,abs(imagem-255)因为你dtype是一个无符号整数,所以会给出错误的结果。(255-imagem)为了保持整数无符号,您必须这样做:

def inverte(imagem, name):
    imagem = (255-imagem)
    cv2.imwrite(name, imagem)

You can also invert the image using the bitwise_notfunction of OpenCV:

您还可以使用bitwise_notOpenCV的功能反转图像:

imagem = cv2.bitwise_not(imagem)

回答by Eric Olmon

Alternatively, you could invert the image using the bitwise_notfunction of OpenCV:

或者,您可以使用bitwise_notOpenCV的功能反转图像:

imagem = cv2.bitwise_not(imagem)

I liked the example at: https://www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/

我喜欢这个例子:https: //www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/

回答by Diroallu

You can use "tilde" operator to do it:

您可以使用“波浪号”运算符来做到这一点:

import cv2
image = cv2.imread("img.png")
image = ~image
cv2.imwrite("img_inv.png",image)

This is because the "tilde" operator (also known as unary operator) works doing a complement dependent on the type of object

这是因为“波浪号”运算符(也称为一元运算符)根据对象的类型进行补码

for example for integers, its formula is:

例如对于整数,它的公式是:

x + (~x) = -1

x + (~x) = -1

but in this case, opencv use an "uint8 numpy array object" for its images so its range is from 0 to 255

但在这种情况下,opencv 对其图像使用“uint8 numpy 数组对象”,因此其范围是从 0 到 255

so if we apply this operator to an "uint8 numpy array object" like this:

因此,如果我们将此运算符应用于“uint8 numpy 数组对象”,如下所示:

import numpy as np
x1 = np.array([25,255,10], np.uint8) #for example
x2 = ~x1
print (x2)

we will have as a result:

结果是:

[230 0 245]

because its formula is:

因为它的公式是:

x2 = 255 - x1

x2 = 255 - x1

and that is exactly what we want to do to solve the problem.

而这正是我们想要解决的问题。