15图像的灰度线性变换
程序员文章站
2023-12-25 22:28:33
...
#https://blog.csdn.net/Eastmount/article/details/88858696
一.图像灰度线性变换原理
也就是线性变换
img = cv2.imread('C:/Users/31035/Desktop/yifei/01.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#直接广播做加法,虽然简单,不过不好调节。
img = img + 80#图片花了
#显示图像
cv2.imshow("img", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
二.图像灰度上移变换
img = cv2.imread('C:/Users/31035/Desktop/yifei/01.jpg')
#图像灰度转换
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#获取图像高度和宽度
height = grayImage.shape[0]
width = grayImage.shape[1]
#创建一幅图像
result = np.zeros((height, width), np.uint8)
#图像灰度上移变换 DB=DA+50
for i in range(height):
for j in range(width):
if (int(grayImage[i,j]+80) > 255):
gray = 255
else:
gray = int(grayImage[i,j]+80)
result[i,j] = np.uint8(gray)
#显示图像
cv2.imshow("Gray Image", grayImage)
cv2.imshow("Result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
三.图像对比度增强变换
替换一段代码
if (int(grayImage[i,j]*1.5) > 255):
gray = 255
else:
gray = int(grayImage[i,j]*1.5)
result[i,j] = np.uint8(gray)
四.图像对比度减弱变换
乘以小于1的系数即可
五.图像灰度反色变换
#图像灰度反色变换 DB=255-DA
for i in range(height):
for j in range(width):
gray = 255 - grayImage[i,j]
result[i,j] = np.uint8(gray)