Python Forum
how to detect horizontal dotted lines in an image using OpenCV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to detect horizontal dotted lines in an image using OpenCV (/thread-25924.html)



how to detect horizontal dotted lines in an image using OpenCV - pframe - Apr-15-2020

Hi all,

I am learning techniques on how to detect horizontal lines. So far I know how to detect a continuous horizontal line. So I am trying to learn how to detect a broken (dotted) horizontal line.

The code that I use to detect continuous horizontal line is below.
import cv2
import functions
import numpy as np

# Load image, convert to grayscale, Otsu's threshold
image = cv2.imread('new.jpg')
no_horizontal = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Detect horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(no_horizontal, [c], -1, (255,255,255), 3)

A test image I am using is below:
[url=https://pasteboard.co/J3YpAY6.jpg[/url]

I have tried HoughLinesP from OpenCV but I can't seem to get the parameters right.
Any direction/advice will be greatly appreciated.