Hough line transform in OpenCV, probability Hough line transform

This blog will introduce hough transforms in Python and OpenCV. Including what is Hough Transform, Probablistic Hough Transform, and how to use Cv2.houghlines (), cv2.houghlinesp () to detect the lines in the image.

1. The rendering

The original image VS the Hough transform effect is as follows:

All lines are tested in the figure, including an Angle of 0°180 degrees (red line, passing through the point below the origin) and the Angle is minus 180 degreesZero degrees (green line, passing through the point above the origin).The original VS probability Hough transform effect is as follows:Probability Hough transform effect can set the size of the gap between lines, detect small short lines;

Principle 2.

Any line can be represented by y=mx+c, or (ρ, θ), ρ=xcosθ+ysinθ. ρ is in pixels and θ is in radians.

If the line passes below the origin, its ρ is positive and the Angle is (0,180). If the line passes above the origin, ρ is negative and the Angle is (-180, 0). Any vertical line is 0 degrees, and the horizontal line is 90 degrees.

2.1 What is hough transform?

Hough transform is a popular technique for detecting any shape, even if it is slightly damaged or distorted.

lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)

  • Edges: Input image (binary image, so threshold or Canny edge detection is applied before applying hough transform lookup)
  • 1: ρ accuracy, ρ in pixels, θ in radians.
  • Np. PI /180: θ accuracy
  • 200: threshold value, assumed detection line, represents the minimum length of detected line, less than 200 pixel length will be discarded;
  • Lines: returns values (ρ,θ), ρ in pixels,θ in radians.

2.2 What is probabilistic Hough transform?

Probablistic Hough Transform is the optimization of Hough Transform.

  • The Hough transform takes all points of a line into consideration, while the probabilistic Hough transform considers only a random subset of points, which is sufficient for line detection.

  • The return value of Hough transform is (ρ, θ), while the probability Hough transform directly returns the end of the line is easier to operate.

lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap)

  • Edages, 1, Np. PI /180 are the same as Hough transform, which are input binary image, ρ accuracy, θ accuracy respectively
  • MinLineLength: Minimum line length. Lines less than this length will be discarded;
  • MaxLineGap: The maximum allowed space between straight segments, which are treated as single lines.
  • Lines: It returns directly to the two endpoints of the line.

3. The source code

3.1 Hough transform

# Hough transform (can detect any shape)

# lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
# -edges: input image (binary image, so apply threshold or Canny edge detection before applying hough transform lookup)
# -1: ρ accuracy, ρ in pixels, θ in radians.
# -Np.pi /100: Angle, θ precision
# -200: threshold, hypothetical detection line, indicating the shortest length of the detected line
# -lines: returns values (ρ,θ), ρ in pixels,θ in radians.
#
# Probablistic Hough Transform is an optimization of Hough Transform.
# Hough transform takes all points of the line into calculation, while probabilistic Hough transform does not consider all points, but only a random subset of points, which is sufficient for line detection.
# lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap)
# -edages, 1, Np. PI /180 is the same as hough transform, which is the input binary image, ρ accuracy, θ accuracy respectively
# -minlinelength: Minimum line length. Lines less than this length are discarded;
# -maxlineGap: The maximum allowed space between straight segments, which are treated as single lines.
# -lines: It directly returns the endpoints of the two lines.

import cv2
import imutils
import numpy as np

img = cv2.imread('hf1.jpg')
cv2.imshow("origin", imutils.resize(img, width=400))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50.150, apertureSize=3)

print(np.pi / 180, np.pi / 360)
lines = cv2.HoughLines(edges, 1, np.pi / 180.200)

for i, line in enumerate(lines):
    for rho, theta in line:
        # print(rho, theta * 180 - 180)
        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))
        
        # Radian turn Angle
        degree = theta * 180 - 180
        if (degree > 180):
            degree = degree - 360
        print(rho, degree)
        
        if rho > 0 and degree > 0:  # passes through the point below the origin at a positive Angle (0,180)
            cv2.line(img, (x1, y1), (x2, y2), (0.0.255), 2)
        else:  # passes through the point above the origin at a negative Angle (-180, 0)
            cv2.line(img, (x1, y1), (x2, y2), (0.255.0), 2)

cv2.imwrite('houghlines_hf.jpg', img)
cv2.imshow("hf_res", imutils.resize(img, width=400))
cv2.waitKey(0)
Copy the code

3.2 Probabilistic Hough transform

# Probability Hough transform

# Hough transform (can detect any shape)
# lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
# -edges: input image (binary image, so apply threshold or Canny edge detection before applying hough transform lookup)
# -1: ρ accuracy
# -Np.pi /100: θ precision
# -200: threshold, hypothetical detection line, indicating the shortest length of the detected line
# -lines: returns values (ρ,θ), ρ in pixels,θ in radians.

# Probablistic Hough Transform is an optimization of Hough Transform.
# Hough transform takes all points of the line into calculation, while probabilistic Hough transform does not consider all points, but only a random subset of points, which is sufficient for line detection.
# lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap)
# -edages, 1, Np. PI /180 is the same as hough transform, which is the input binary image, ρ accuracy, θ accuracy respectively
# -minlinelength: Minimum line length. Lines less than this length are discarded;
# -maxlineGap: The maximum allowed space between straight segments, which are treated as single lines.
# -lines: It directly returns the endpoints of the two lines.

# Probablistic Hough Transform is an optimization of Hough Transform.
# Hough transform takes all points of the line into calculation, while probabilistic Hough transform does not consider all points, but only a random subset of points, which is sufficient for line detection.

import cv2
import imutils
import numpy as np

img = cv2.imread('hf1.jpg')
cv2.imshow("origin", imutils.resize(img, width=400))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50.150, apertureSize=3)

minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180.10, minLineLength, maxLineGap)

Draw only the first line
# for x1, y1, x2, y2 in lines[0]:
# cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

print(len(lines))
Draw all detection results
for i, line in enumerate(lines):
    for x1, y1, x2, y2 in line:
        cv2.line(img, (x1, y1), (x2, y2), (0.255.0), 2)
cv2.imwrite('houghlines_hfp.jpg', img)
cv2.imshow("hfp_res", imutils.resize(img, width=400))
cv2.waitKey(0)
Copy the code

reference

  • Docs.opencv.org/3.0-beta/do…