2

I want to detect the division line in the scanned document, and instead of using cv2.Canny, I used cv2.threshold to get a pretty clean pre-processed image. However my parameter for cv2.HoughLines may be improper and I got a chaos in the final output. And the lines didn`t present as the set color.

My code is:

import cv2
import numpy as np
from matplotlib import pyplot as plt

## (1) read
img = cv2.imread("q11.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=(17,17))
plt.imshow(gray,cmap='gray')

## (2) threshold
th, threshed = cv2.threshold(gray, 200, 20, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
plt.figure(figsize=(15,15))
plt.imshow(threshed,cmap='gray')

## (3) HoughLines
lines = cv2.HoughLines(threshed,rho=1,theta=np.pi/180,threshold = 800)
for i in range(len(lines)):
    for rho,theta in lines[i]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
    cv2.line(threshed,(x1,y1),(x2,y2),(0,0,255),2)

plt.figure(figsize=(10, 10))
plt.subplot(111),plt.imshow(threshed)
plt.title('hough'), plt.xticks([]), plt.yticks([])

The original pic is: enter image description here

and after cv2.threshold with cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU I got: enter image description here

I want to have: enter image description here

What I actually got:

enter image description here

2
  • stackoverflow.com/questions/45322630/… Commented Jan 10, 2019 at 0:11
  • 1
    As far as your yellow and purple output is concerned, you can refer to this answer Commented Jan 10, 2019 at 6:23

1 Answer 1

0

Here's a simple approach

  • Convert image to grayscale and gaussian blur
  • Threshold image
  • Dilate to enhance contour
  • Detect lines

Threshold

Next we dilate to enhance contours then use cv2.HoughLinesP() to detect lines. You mention

The lines don`t show as the set color

This is because you tried to draw the line on a binary image (the thresholded image). Since it only has one channel, pixels are only white or black. Thus it will not display the color. You have to draw it on a colored image

import cv2
import numpy as np

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)

thresh = cv2.threshold(blur,190, 255,cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=1)

minLineLength = 10
maxLineGap = 200
lines = cv2.HoughLinesP(dilate,1,np.pi/180,100,minLineLength,maxLineGap)
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(image,(x1,y1),(x2,y2),(0,0,255),3)

cv2.imshow('image', image)
cv2.imshow('thresh', thresh)
cv2.waitKey()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.