Detection of Blood Vessels Junctions

asked 2020-03-27 04:29:40 -0600

Steven.Dragoz gravatar image

Hello all, I have a problem of Detecting blood vessels from a particular video, for a start, I would like to start of with an image of blood vessels. image description

So, I followed the following example on detecting junctions https://answers.opencv.org/question/1...

However, it didn't work. My code is as follows

import numpy as np
import cv2

def getJunctions(src):
    # the hit-and-miss kernels to locate 3-points junctions to be used in each directions
    # NOTE: float type is needed due to limitation/bug in warpAffine with signed char
    k1 = np.asarray([
        0,  1,  0,
        0,  1,  0,
        1,  0,  1], dtype=float).reshape((3, 3))
    k2 = np.asarray([
        1,  0,  0,
        0,  1,  0,
        1,  0,  1], dtype=float).reshape((3, 3))
    k3 = np.asarray([
        0, -1,  1,
        1,  1, -1,
        0,  1, 0], dtype=float).reshape((3, 3))

# Some useful declarations
tmp = np.zeros_like(src)
ksize = k1.shape
center = (ksize[1] / 2, ksize[0] / 2) # INVERTIRE 0 E 1??????
# 90 degrees rotation matrix
rotMat = cv2.getRotationMatrix2D(center, 90, 1)
# dst accumulates all matches
dst = np.zeros(src.shape, dtype=np.uint8)

# Do hit & miss for all possible directions (0,90,180,270)
for i in range(4):
    tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k1.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)     
    dst = cv2.add(dst, tmp)
    tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k2.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
    dst = cv2.add(dst, tmp)
    tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k3.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
    dst = cv2.add(dst, tmp)
    # Rotate the kernels (90deg)
    k1 = cv2.warpAffine(k1, rotMat, ksize)
    k2 = cv2.warpAffine(k2, rotMat, ksize)
    k3 = cv2.warpAffine(k3, rotMat, ksize)

return dst
src = cv2.imread("C:/Users/Steven/Desktop/maxresdefault.jpg")
src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
src = cv2.resize(src, (600,400), interpolation = cv2.INTER_AREA)
ridge_filter = cv2.ximgproc.RidgeDetectionFilter_create(cv2.CV_32FC1, 1, 1 , 3, cv2.CV_8UC1, 1, 0 , cv2.BORDER_DEFAULT)
ridges = ridge_filter.getRidgeFilteredImage(src)
cv2.imshow('Ridges', ridges)

blank_mask = np.zeros(src.shape, dtype=np.uint8)
thresh = cv2.threshold(ridges, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cv2.imshow("Adaptive Threshold", thresh)

thresh *= 255;
# Morphology logic is: white objects on black foreground
thresh = 255 - thresh;

# Get junctions
junctionsScore = getJunctions(thresh)

# Draw markers where junction score is non zero
dst = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
# find the list of location of non-zero pixels
junctionsPoint = cv2.findNonZero(junctionsScore)

for pt in junctionsPoint:
    pt = pt[0]
    dst[pt[1], pt[0], :] = [0, 0, junctionsScore[pt[1], pt[0]]]

# show the result
winDst = "Dst"
winSrc = "Src"
winJunc = "Junctions"

cv2.imshow(winSrc, src)
cv2.imshow(winJunc, junctionsScore)
cv2.imshow(winDst, dst)
cv2.waitKey()
cv2.destroyAllWindows()

The result is as follows: image description

Any help is appreciated. Thank you!

edit retag flag offensive close merge delete

Comments

Probably you should do a skeletonization on the binarized image (cv2.ximgproc.thinning) before detecting the intersections.

Maybe you should change the parameters of the ridge detection function as it seems too sensible (it has answers even where I see no blood vessels).

kbarni gravatar imagekbarni ( 2020-03-27 07:42:00 -0600 )edit

junctionsPoint = cv2.findNonZero(junctionsScore <== You're getting none.

for pt in junctionsPoint: <===NoneType object is not iterable.

supra56 gravatar imagesupra56 ( 2020-04-06 07:45:36 -0600 )edit

print(junctionScore). What you're seeing.

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]
supra56 gravatar imagesupra56 ( 2020-04-06 07:53:28 -0600 )edit

Unfortunately, I cannot detect red blood. I'll have to work around.

supra56 gravatar imagesupra56 ( 2020-04-06 07:57:36 -0600 )edit

Do you want to detect area of red ball or whole veins??. I come up better

supra56 gravatar imagesupra56 ( 2020-04-06 08:16:03 -0600 )edit

Can you post original image w/out white marking?

supra56 gravatar imagesupra56 ( 2020-04-06 14:55:16 -0600 )edit

@supra56, thats the image I have, the white marking is there when I received the photo

Steven.Dragoz gravatar imageSteven.Dragoz ( 2020-04-06 20:54:45 -0600 )edit

@supra56, I have already implemented, Hit or Miss already but now Its the drawing of the junctions

Steven.Dragoz gravatar imageSteven.Dragoz ( 2020-04-06 20:55:44 -0600 )edit