inverting image in Python with OpenCV

后端 未结 3 432
无人及你
无人及你 2020-12-07 21:44

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 singl

3条回答
  •  [愿得一人]
    2020-12-07 22:44

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

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

    You can also invert the image using the bitwise_not function of OpenCV:

    imagem = cv2.bitwise_not(imagem)
    

提交回复
热议问题