Home >Backend Development >Python Tutorial >How to perform Hough transform on images using Python

How to perform Hough transform on images using Python

WBOY
WBOYOriginal
2023-08-26 13:16:451035browse

How to perform Hough transform on images using Python

How to use Python to perform Hough transform on images

Abstract:
The Hough transform is a commonly used image processing technique used to detect straight lines or certain shapes. This article will introduce how to use the OpenCV library in Python to implement the Hough transform, and explain its implementation process in detail through code examples.

Introduction:
The Hough transform is an image processing technique proposed by Hough in 1962. It was originally used to detect straight lines in images. Subsequently, the Hough transform has been widely used and expanded to detect circles, ellipses and other shapes. In the field of computer vision and image processing, the Hough transform is a very important tool.

1. Principle of Hough Transform
The basic principle of Hough transform is to convert each pixel in the image into a curve in polar coordinate (Hough Space) space (or parameter space) . For a straight line, the two parameters represent the distance and angle of the straight line respectively. By selecting points in the parameter space and finding intersections on the curves, all straight lines in the original image can be obtained.

2. Introduction to OpenCV library
OpenCV is an open source computer vision and machine learning software library that provides a wealth of image processing and computer vision algorithms. It is one of the most popular image processing libraries in Python, with powerful image processing functions and an easy-to-use interface. This article will use the OpenCV library to implement the Hough transform.

3. Use Python to perform Hough transformation
The following uses a specific example to demonstrate how to use Python to perform Hough transformation on images.

First, we need to import the required libraries:

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

Then, read and display the original image:

image = cv2.imread("image.jpg")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

Next, convert the image to grayscale:

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Then, perform edge detection on the image:

edges = cv2.Canny(gray, 50, 150)

Next, perform Hough transform:

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

Traverse and draw the detected straight lines:

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)

Finally, display the processed image:

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

Conclusion:
This article details how to use the OpenCV library in Python to perform Hough transform on images. Through the sample code, we can see the powerful ability of Hough transform in detecting straight lines. In addition to straight lines, OpenCV also provides Hough transform implementations of other shapes, readers can further learn and try.

Reference:

  1. Hough, P. V. C. “Methods and Means for Recognizing Complex Patterns,” U.S. Patent 3 069 654, December 18, 1962.
  2. Bradski, G., Kaehler, A. “Learning OpenCV: Computer Vision with the OpenCV Library.” O'Reilly Media, Inc., 2008.

Appendix:
See the code block below for the complete code :

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

# 读取并显示原始图像
image = cv2.imread("image.jpg")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 对图像进行边缘检测
edges = cv2.Canny(gray, 50, 150)

# 进行霍夫变换
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

# 遍历并绘制检测到的直线
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)

# 显示处理后的图像
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()

Note: Please replace "image.jpg" in the code with your own image path.

The above is the detailed content of How to perform Hough transform on images using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn