最近做的项目需要用到灰度反转,就写了一下,浅浅的记录一下,我已经封装成函数了,大家有需自取~
灰度反转函数
def reverse_gray(img):
"""灰度反转
输入:灰度图,np.uint8格式
输出:反转图,np.uint8格式
"""
max1 = img.max()
img1 = img* (-1)
img2 = img1 + max1
return img2
示例
imgpath = r"./Simulation-Result-on-Lena.png"
img = cv2.imread(imgpath,0)
img1 = reverse_gray(img)
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.subplot(122)
plt.imshow(img1,cmap='gray')
plt.show()