1

I am searching everywhere with regards to Python Open CV. Is there a way to remove specific horizontal lines from the given image below? I tried this method remove lines but it seems to remove all horizontal lines.

Original image:

Original image

Desire image:

Desire image

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
xing
  • 11
  • 2
  • You could remove the long horizontal lines with a long kernel and the vertical lines. Then get the difference to find the short horizontal lines. Then subtract those from the input. – fmw42 Oct 11 '21 at 04:23

1 Answers1

2

Check if this helps

import cv2

# Load image, convert to grayscale. Choosen Otsu's method for automatic image thresholding.
image = cv2.imread('Kw2gB.png')
result = 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, (80,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(result, [c], -1, (36,255,12), 2)

# Detect vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(result, [c], -1, (36,255,12), 2)

cv2.imshow('result', image-result)
cv2.waitKey()

enter image description here

Subasri sridhar
  • 809
  • 5
  • 13