方法一:调用cv2的内置函数bitwise_not
image_invert = cv2.bitwise_not(image)
方法二:针对rgb数据,直接使用255进行减法操作
image_invert = 255 - image
实践与方法对比:
image_path = 'lena.jpg'
image = cv2.imread(image_path)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_invert_1 = cv2.bitwise_not(image_gray)
image_invert_2 = 255 - image_gray
cv2.imshow('1', image_invert_1)
cv2.waitKey(0)
cv2.imshow('2', image_invert_2)
cv2.waitKey(0)
cv2.destroyAllWindows()
原图
方法1
方法2
可以看出两种处理方法产生结果一致 都可以使用~