专业学习|2021-09-09 OpenCV (python)学习笔记(四)

OpenCV 边缘检测之图像增强
【专业学习|2021-09-09 OpenCV (python)学习笔记(四)】图像增强代码示例:

import cv2 #读取图片 filename = C:\Users\username\Desktop\opencv img = cv2.imread(filname + 'picture.jpg',0) #读取图像的灰度图 row, column = img.shape img_f = np.copy(img) gradient = np.zeros((row,column)) #初始化gradient# 2. 计算图像梯度。首先要对读取的图像进行数据变换,因为使用了numpy对梯度进行数值计算,所以要使用img.astype('float')进行数据格式变换 img = img.astype('float') for x in range(row - 1): #之前没循环 for y in range(column - 1): #对img使用图像梯度公式,计算得到的梯度把可以将明暗边界提出来 gx = abs(img[x + 1, y] - img[x, y]) gy = abs(img[x, y + 1] - img[x, y]) gradient[x, y] = gx + gy#对img的hi度图像进行增强 share = img_f + gradientsharp = np.where(sharp > 255, 255, sharp) sharp = np.where(sharp < 0, 0, sharp)# 数据类型变换 gradient = gradient.astype('uint8') sharp = sharp.astype('uint8')# 保存图像 filepath = C:\Users\username\Desktop\opencv cv2.imwrite(filepath + 'out/gradient.png', gradient)

    推荐阅读