I have followde the image:
I want to extract only lines from it -- only lines without text.
What would be the best practice to do this?
I tried with the cv2 Python library and HoughLinesP with following the code:
img = cv2.imread('/Users/tekija/Documents/image.png')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_thr = cv2.threshold(img_gray, 120, 255, cv2.THRESH_BINARY)
lines = cv2.HoughLinesP(
img_thr, rho=1, theta=np.pi / 180, threshold=128, minLineLength=600, maxLineGap=30)
lines = lines.squeeze()
but results are:
Since you have confirmed that the lines always have the same color, it makes sense to first filter by this color and then detect the lines.
import cv2
import numpy as np
# Load image
image = cv2.imread('demo.png')
# Convert to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Define color range (original is #7e3553)
lower_range = np.array([155, 100, 20])
upper_range = np.array([175, 255, 255])
# Create mask
mask = cv2.inRange(hsv_image, lower_range, upper_range)
# Dilate the mask to fill in small gaps
mask = cv2.dilate(mask, (3,3), iterations=2)
# Detect points that form a line
lines = cv2.HoughLinesP(mask, 1, np.pi/180, 100, minLineLength=10, maxLineGap=250)
# Draw lines on new mask
line_mask = np.zeros(image.shape[:2], dtype=np.uint8)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_mask, (x1, y1), (x2, y2), 255, 3)
# Erode to thin the lines
line_mask = cv2.erode(line_mask, (3,3), iterations=2)
cv2.imwrite('demo_output.png', line_mask)
This is the result, notice how all the gaps at the top and bottom right are closed: