import cv2
import numpy as np
img = cv2.imread("veins.jpg")
ht, wd = img.shape[:2]
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# create a binary thresholded image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
# apply morphology to remove small black spots
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# invert so veins are white
veins = 255 - morph
# count non-zero pixels
count = np.count_nonzero(veins)
# compute density
density = count / (ht * wd)
# print density
print("density :", density)
# the density also should be just the graylevel average (mean) of the binary veins image divided by 255, so
density2 = np.mean(veins)/255
# print density2
print("density2:", density2)
# save result
cv2.imwrite("veins_binary.jpg", morph)
# show results
cv2.imshow("thresh", thresh)
cv2.imshow("morph", morph)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值化和形态学清理图像:
密度结果:
density : 0.41110921223958335
density2: 0.4111092122395833
基本上,一个阈值的图像与大津自动阈值。然后选择性地使用形态学删除可能不是静脉一部分的小黑色区域。然后反转颜色,使纹理为白色,并计算图像中黑色像素的数量。然后,密度将是计数除以图像中的像素总数
输入:
阈值化和形态学清理图像:
密度结果:
这是一个包含Python/OpenCV/Skimage中的骨架化的版本
输入:
骨架图像:
密度:
相关问题 更多 >
编程相关推荐