最近做的项目需要用到灰度反转,就写了一下,浅浅的记录一下,我已经封装成函数了,大家有需自取~
灰度反转函数
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()

文章介绍了一个用于灰度图像反转的Python函数,该函数接受np.uint8格式的灰度图作为输入,通过计算灰度值的负值并加上最大值来实现反转。作者提供了代码示例,展示如何使用该函数处理Lena图像,并用OpenCV读取和显示图像,以比较原图和反转后的效果。
1552





