# Detection of Blood Vessels Junctions

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.

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)
tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k2.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k3.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
# 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.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)

thresh = cv2.threshold(ridges, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

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:

Any help is appreciated. Thank you!

edit retag close merge delete

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).

( 2020-03-27 07:42:00 -0500 )edit

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

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

( 2020-04-06 07:45:36 -0500 )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]]

( 2020-04-06 07:53:28 -0500 )edit

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

( 2020-04-06 07:57:36 -0500 )edit

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

( 2020-04-06 08:16:03 -0500 )edit

Can you post original image w/out white marking?

( 2020-04-06 14:55:16 -0500 )edit

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

( 2020-04-06 20:54:45 -0500 )edit

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

( 2020-04-06 20:55:44 -0500 )edit