一.任务说明
用python实现静脉清晰度提升。
二.代码实现
import cv2
import numpy as np
def enhance_blood_vessels(image):
# 调整图像对比度和亮度
enhanced_image = cv2.convertScaleAbs(image, alpha=0.5, beta=40)
# 应用CLAHE(对比度受限的自适应直方图均衡化)
clahe = cv2.createCLAHE(clipLimit=10.0, tileGridSize=(8, 8))
enhanced_image = clahe.apply(enhanced_image)
# 应用中值滤波平滑图像
enhanced_image = cv2.medianBlur(enhanced_image, 9)
return enhanced_image
def extract_blood_vessels(image):
# 阈值分割提取静脉血管
ret, thresholded_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# 使用形态学操作(膨胀和腐蚀)进一步清理和连接血管
kernel = np.ones((3, 3), np.uint8)
thresholded_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_OPEN, kernel)
return thresholded_image
# 读取图像
image = cv2.imread('input_pic.png', cv2.IMREAD_GRAYSCALE)
# 图像增强
enhanced_image = enhance_blood_vessels(image)
# 提取静脉血管
vessels_image = extract_blood_vessels(enhanced_image)
# 将灰度图转换为彩色图
color_image = np.zeros((enhanced_image.shape[0], enhanced_image.shape[1], 3), dtype=np.uint8)
for i in range(enhanced_image.shape[0]):
for j in range(enhanced_image.shape[1]):
color_image[i][j] = (enhanced_image[i][j], enhanced_image[i][j], 100) # 使用灰度值作为RGB通道的值
# 显示彩色图
cv2.imshow('Color Image', color_image)
# 显示图像
cv2.imshow('Original Image', image)
cv2.imshow('Enhanced Image', enhanced_image)
cv2.imshow('Blood Vessels', vessels_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
三.识别效果