How to implement line detection in images using OpenCV

Computer vision is one of the most crucial advancements in artificial intelligence. It provides us with another medium to communicate with the computer. Computer vision serves as the foundational concept that supports real-life applications such as autonomous cars, improved medical diagnosis, and facial recognition. Various supporting techniques have contributed to the field of computer vision, which include:

  • Facial recognition: Facial recognition is a specialized technique to identify and differentiate specific individuals within images. It plays a crucial role in applications such as biometric authentication and surveillance systems.

  • Object detection: Object detection identifies specific objects within an image. It can even recognize multiple objects simultaneously using bounding boxes.

  • Image segmentation: This technique involves dividing an image into separate regions or segments for individual analysis. It enables precise examination of different parts of an image.

  • Edge detection: By focusing on identifying the outer boundaries of objects or landscapes, edge detection enhances image understanding. It is commonly used in image processing tasks like edge enhancement and feature extraction.

These are some of the supporting techniques, and the recent advancements in deep learning, neural networks, and artificial intelligence have propelled the field of computer vision to new heights, enabling remarkable applications deemed impossible at some time. One of the techniques that we are going to see implemented is the Hough line transform algorithm.

Using Hough line transform

The Hough line transform is a computer vision technique that detects straight lines in any provided image. This is possible by transforming the image space into a parameter space called the Hough space. This helps identify lines based on their geometric properties. However, before using the Hough line technique, we need to apply an edge detection algorithm. This is done to obtain an edge image, which serves as the input for the Hough line algorithm.

Read up in detail about the Hough line transform.

Implementation of Hough line transform

Libraries used

In this code implementation, we'll be using the following libraries:

  • cv2: Used for implementing the functionality of OpenCV.

pip install opencv-python
  • numpy: Used for numerical computations and array manipulation.

pip install numpy
  • matplotlib.pyplot: Used for creating visualizations and plots.

pip install matplotlib

Line detection code

import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('images.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=69)
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 6))
plt.imshow(image_rgb)
plt.axis('off')
plt.show()

Explanation

  • Line 4: We are reading an image file using the OpenCV function cv2.imread().

  • Line 5: We are converting the provided image into a grayscale image for edge detection using cv2.cvtcolor() and the parameters include the image we are using and a flag, cv2.COLOR_BGR2GRAY, for indicating the conversion from BGR (Blue-Green-Red) color space to grayscale.

  • Line 6: The cv2.Canny() function is applied to convert the grayscale image to an edge image. The parameters it has been provided are the grayscale image file, the minimum and maximum threshold value of gradient magnitudeMeasure of the intensity of the change in pixel values across an image, indicating the rate of change of the image gradient., and the apertureSize which determines the level of detail in the detected edges.

  • Line 7: The cv2.HoughLines() function detects straight lines in an image using the Hough line transform, taking parameters including the edges binary image, distance resolution, angle resolution, and a threshold specifying the minimum number of votes for a line to be considered detected.

  • Line 819: In this part of the code, it draws the detected lines on the image by iterating over each line, extracting the details about the lines, calculating its endpoints, and using the cv2.line() function to draw the line segment.

  • Line 20: This line converts the image back to color graded image using the cv2.cvtColor() function with the cv2.COLOR_BGR2RGB flag.

  • Line 21 24: This section of code displays the final output using the matplotlib library.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved