I want to detect all the underlined words in a paragraph

Viewed 138

Original Image Click here for the image For this, I am trying to detect the underlines first. But as the underlines might be tilted, this code:

import time
from google.colab.patches import cv2_imshow 
from collections import OrderedDict 
# Let's load a simple image with 3 black squares 
image = cv2.imread("line_detected.png") 
cv2.waitKey(0) 
  
# Grayscale 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 


# Find Canny edges 
font = cv2.FONT_HERSHEY_COMPLEX
edged = cv2.Canny(gray, 30, 200) 
cv2.waitKey(0) 
  
# Finding Contours 
# Use a copy of the image e.g. edged.copy() 
# since findContours alters the image 
contours, hierarchy = cv2.findContours(edged,  cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
  
cv2_imshow(edged) 
cv2.waitKey(0) 
  
print("Number of Contours found = " + str(len(contours))) 
  
# Draw all contours 
# -1 signifies drawing all contours 
# cv2.drawContours(image, contours, -1, (0, 255, 0), 3) 
mask = np.ones(image.shape[:2], dtype="uint8") * 255
d=OrderedDict()
coords=[]
nuclei = [] 
l=[]
heading=[]
images=[]
lvalue=0
line=[]
h=[]
contours = contours[::-1]

for cnt in (contours):     
  peri = cv2.arcLength(cnt, True)
  approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)
  if (len(approx==2)):
      x, y, w, h = cv2.boundingRect(cnt)
      # print(h)
      cv2.rectangle(img,(x, y), (x+w, y+h),(0, 0, 255), 2)
cv2_imshow(img)

is not able to detect the slanting underlines very properly. Also, I want this code to extend to detecting only the gray underlines. "minor differences" has a single underline as it is slanted/tilted, it reads it as two straight lines. Also, it is reading the images in the left which it should not read(tesseract giving weird outputs).

For the gray shade only I found this mask thing online:


lower_range = np.array([110,50,50])
upper_range = np.array([130,255,255])

mask = cv2.inRange(hsv, lower_range, upper_range)

But Don't know how to incorporate in code... I'm a beginner, any help is much appreciated!

0 Answers
Related