我正在尝试通过将黑色区域替换为平均像素值来去除图像中的黑色边框和多余的非眼睛特征(例如,请参见下面的文字和“剪辑”)来预处理眼睛血管的照片从3个随机方块开始。
crop1 = randomCrop(image2, 50, 50) #Function that finds random 50x50 area
crop2 = randomCrop(image2, 50, 50)
crop3 = randomCrop(image2, 50, 50)
mean1 = RGB_Mean(crop1)
mean2 = RGB_Mean(crop2)
mean3 = RGB_Mean(crop3)
#RGB Mean
result = [statistics.mean(k) for k in zip(mean1, mean2, mean3)]
for i in range(len(image2[:,0, 0])):
for j in range(len(image2[0,:,0])):
thru_pixel = image2[i, j]
if (thru_pixel[0] < 50 and thru_pixel[1] < 50 and thru_pixel[2] < 50):
image2[i,j, :] = result
if (thru_pixel[0] > 190 and thru_pixel[1] > 190 and thru_pixel[2] > 190):
image2[i,j, :] = result
但是,图像边框周围有剩余的杂点,左下角还有剩余的文本和剪辑。
这是示例图片。
原始:
和后处理
您可以看到,仍然存在黑灰色边框噪声以及右下方的文本和左下方的“剪辑”。在保持眼部血管完整性的同时,我还能尝试消除这些伪影吗?
感谢您的时间和帮助!
答案 0 :(得分:2)
假设您要隔离眼部血管,此方法可分为两个阶段,一个阶段是去除伪影,另一个阶段是分离血管
从您的原始图像开始,我们将转换为灰度和大津的阈值以获得二进制图像
现在,我们执行变形打开以移除伪像(左)。我们反转此蒙版以获取白色边框,然后执行一系列按位操作以获取去除的伪像图像(右)
从这里我们自适应阈值以获取静脉
请注意,这里有不需要的边框,因此我们使用最大阈值区域查找轮廓并进行过滤。如果轮廓通过滤镜,我们会将其绘制到空白蒙版上
最后,我们对原始图像进行按位运算,然后得到结果
请注意,我们可以在自适应阈值之后执行其他变形打开操作,以去除细小的噪声颗粒,但是要权衡的是它将删除静脉细节。我将把这个可选步骤留给您
import cv2
import numpy as np
# Grayscale, Otsu's threshold, opening
image = cv2.imread('1.png')
blank_mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,15))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)
inverse = 255 - opening
inverse = cv2.merge([inverse,inverse,inverse])
removed_artifacts = cv2.bitwise_and(image,image,mask=opening)
removed_artifacts = cv2.bitwise_or(removed_artifacts, inverse)
# Isolate blood vessels
veins_gray = cv2.cvtColor(removed_artifacts, cv2.COLOR_BGR2GRAY)
adaptive = cv2.adaptiveThreshold(veins_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,3)
cnts = cv2.findContours(adaptive, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area < 5000:
cv2.drawContours(blank_mask, [c], -1, (255,255,255), 1)
blank_mask = cv2.cvtColor(blank_mask, cv2.COLOR_BGR2GRAY)
final = cv2.bitwise_and(image, image, mask=blank_mask)
# final[blank_mask==0] = (255,255,255) # White version
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('removed_artifacts', removed_artifacts)
cv2.imshow('adaptive', adaptive)
cv2.imshow('blank_mask', blank_mask)
cv2.imshow('final', final)
cv2.waitKey()