图像进行反转:白变黑,黑变白
二值图对图像进行反转
import cv2
img = "5.png"
img = cv2.imread(img, 1)
cv2.imshow('img', img)
img_shape = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
print(gray.shape)
dst = 255 - gray
ret2, binary2 = cv2.threshold(dst, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('dst', dst)
cv2.imshow("binary",binary)
cv2.imshow("binary2",binary2)
cv2.waitKey(0)
cv2.destroyAllWindows()
灰度图图片反转
import PIL.ImageOps
from PIL import Image
img = Image.open(img)
img = img.convert('L')
img.show()
img = img.convert('L')
img = PIL.ImageOps.invert(img)
img = img.convert('L')
img.show()
彩色的图片反转
im02 = Image.open(img)
im= PIL.ImageOps.grayscale(im02)
im.show()
im= PIL.ImageOps.invert(im02)
im.show()
调整阈值对其反转
im0= PIL.ImageOps.solarize(img, 100)
im0.show()