I'm doing a personal project in which I want to select some very specific lines in a microscopy image. The first picture is the raw image, the second picture is the image for the lines (in yellow) I would like to detect. Not straight lines yes, but lines indeed.
Now, I made a very basic code for line detection that kind of "works" detecting lines, but it takes a lot of undesired detected lines in the process. See an example with another image (and the code below it, in Python, using OpenCV).
import cv2 as cv
import numpy as np
#Load and normalize .tif image:
img = 'imagenes.tif'
img = cv.imread(img, 0)
img_scaled = cv.normalize(img, np.zeros((800, 800)), 0, 255, cv.NORM_MINMAX)
#Resizing images:
pct = 30
w = int(img.shape[1] * pct / 100)
h = int(img.shape[0] * pct / 100)
dim = (w, h)
resized = cv.resize(img_scaled, dim, interpolation = cv.INTER_AREA)
# First stage: Gaussian blur.
gaussian = cv.GaussianBlur(resized,(5, 5),0)
# Second Stage: edge detection with Canny.
low_threshold = 90
high_threshold = 150
edges = cv.Canny(gaussian, low_threshold, high_threshold)
# Third stage: Hough Line transform.
rho = 1
theta = np.pi / 180
threshold = 15
minLineLenght = 15
maxLineGap = 15
myBlankLines = resized.copy()
lines = cv.HoughLinesP(edges, rho, theta, threshold, np.array([]),
minLineLenght, maxLineGap)
for line in lines:
for x1,y1,x2,y2 in line:
cv.line(myBlankLines,(x1,y1),(x2,y2),(255,0,0),5)
cv.imshow('image_resized', resized)
cv.imshow('Stage 1: Gaussian', gaussian)
cv.imshow('Stage 2: Edges', edges)
cv.imshow('Stage 3: Lines', myBlankLines)
cv.waitKey(0)
cv.destroyAllWindows()
Now. I don't mind building a filter from zero and avoid using OpenCV or any other image processing/computer vision library. But I feel a bit lost on what direction I should go. I'm not sure if:
- Doing more pre-filtering.
- Adjust parameters for Hough Lines Transform.
- Something else related with Machine Learning perhaps.
What would you suggest? Thanks for any heads up you might have on this interesting task.