I'm trying to identify brain tumors with blob detection in Open CV, but so far, Open CV only detects tiny circles in brain MRIs, but never the tumor itself.
Here's the code:
import cv2
from cv2 import SimpleBlobDetector_create, SimpleBlobDetector_Params
import numpy as np
# Read image
def blobber(filename):
im = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector_create()
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 50
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.1
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im,keypoints,np.array([]),(0,0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
Here's what happens when I feed the program an image of a b/w contrasted brain (I contrasted the brain so the tumor would appear in black, and the rest of the brain would be mostly white):
The tumor is not a perfect circle, by any means, but it's clearly the biggest "blob" in the brain. Open CV can't pick it up, I suspect because it has a black outer shell, and a white core.
Only when I choose a more distinguishable tumor, without a large white inner core, can it pick up the tumor.
Any advice? I need to be able to peel these blobs (once they work accurately) out of the original pictures and use their keypoints to reconstruct the entire 3D tumor in the brain from JUST the 2D tumor in each slice. I'm a bit far removed from that step, but this blob detector issue is the crucial link between 2D and 3D. Appreciate all help!