Zebra Crossing Extraction in Digital Image Processing

Zebra crossing extraction refers to the process of automatically detecting and extracting zebra crossings from digital images. This process is often used in traffic scenarios, such as roads, sidewalks, etc.

The process of zebra crossing extraction usually includes the following steps:

1. Preprocessing: Denoise and smooth the image to make it easier to detect zebra crossings.

2. Edge detection: Use an edge detection algorithm, such as the Canny algorithm, to detect the edges in the image.

3. Line detection: Line detection is performed on the edge, such as using Hough transform to detect lines.

4. Zebra crossing detection: According to the geometric shape and color characteristics of the zebra crossing, the straight line that meets the conditions is screened out, which is the zebra crossing.

To achieve zebra crossing extraction in Matlab, the following steps can be used:

1. Read the image: use the imread function in Matlab to read the image.

2. Preprocessing: use image processing functions in Matlab, such as imnoise, imgaussfilt and other functions to denoise and smooth the image.

3. Edge detection: use the edge detection functions in Matlab, such as Canny, Sobel and other functions, to detect the edges in the image.

4. Line detection: Use the Hough transform function hough in Matlab to detect the lines in the image.

5. Zebra crossing detection: According to the geometric shape and color characteristics of the zebra crossing, the straight line that meets the conditions is screened out, which is the zebra crossing.

The following is a Matlab code:
% read image
img = imread('zebra_crossing.jpg');

% Preprocessing
img_noise = imnoise(img,'gaussian');
img_smooth = imgaussfilt(img_noise,3);

% Edge detection
BW = edge(img_smooth,'Canny');

% Line detection
[H,T,R] = hough(BW);
P = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T, R,P,'FillGap',10,'MinLength',50);

% Zebra crossing detection
for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    % Determine whether the line is a zebra crossing
    % ...
end

% 显示结果
imshow(img);
hold on;
for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
end

In the above code, we first read the image and then perform preprocessing, including denoising and smoothing operations. Next use the Canny algorithm for edge detection, and then use the Hough transform to detect straight lines. Finally, we need to judge whether the straight line is a zebra crossing according to the characteristics of the zebra crossing, and the specific implementation method is omitted here. Finally, the detected straight lines are displayed on the original image.

To achieve zebra crossing extraction in C++, the following steps can be used:

1. Read the image: use the imread function in OpenCV to read the image.

2. Preprocessing: use the image processing functions in OpenCV, such as GaussianBlur, medianBlur and other functions to denoise and smooth the image.

3. Edge detection: Use the edge detection functions in OpenCV, such as Canny, Sobel and other functions, to detect the edges in the image.

4. Line detection: Use the Hough transform function HoughLines in OpenCV to detect lines in the image.

5. Zebra crossing detection: According to the geometric shape and color characteristics of the zebra crossing, the straight line that meets the conditions is screened out, which is the zebra crossing.

The following is a C++ code:
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{     // read image     Mat img = imread("zebra_crossing.jpg");

    // 预处理
    Mat img_gray, img_smooth, img_edge;
    cvtColor(img, img_gray, COLOR_BGR2GRAY);
    GaussianBlur(img_gray, img_smooth, Size(3, 3), 0);
    Canny(img_smooth, img_edge, 50, 150);

    // Line detection
    vector<Vec2f> lines;
    HoughLines(img_edge, lines, 1, CV_PI / 180, 100);

    // Zebra detection
    for (size_t i = 0; i < lines.size(); i++)
    {         float rho = lines[i][0], theta = lines[i][1];         double a = cos(theta) , b = sin(theta);         double x0 = a * rho, y0 = b * rho;         Point pt1(cvRound(x0 + 1000 * (-b)), cvRound(y0 + 1000 * (a)));         Point pt2 (cvRound(x0 - 1000 * (-b)), cvRound(y0 - 1000 * (a)));         // Determine whether the line is a zebra crossing         // ...     }







    // 显示结果
    for (size_t i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0], theta = lines[i][1];
        double a = cos(theta), b = sin(theta);
        double x0 = a * rho, y0 = b * rho;
        Point pt1(cvRound(x0 + 1000 * (-b)), cvRound(y0 + 1000 * (a)));
        Point pt2(cvRound(x0 - 1000 * (-b)), cvRound(y0 - 1000 * (a)));
        line(img, pt1, pt2, Scalar(0, 0, 255), 2, LINE_AA);
    }
    imshow("output", img);
    waitKey();

    return 0;
}

In the above code, we first read the image, and then perform preprocessing, including converting to grayscale image, denoising and smoothing operations, and Canny algorithm for edge detection. Next use the Hough transform to detect straight lines. Finally, we need to judge whether the straight line is a zebra crossing according to the characteristics of the zebra crossing, and the specific implementation method is omitted here. Finally, the detected straight lines are displayed on the original image.

To achieve zebra crossing extraction in Python, the following steps can be used:

1. Read the image: use the imread function in OpenCV to read the image.

2. Preprocessing: use the image processing functions in OpenCV, such as GaussianBlur, medianBlur and other functions to denoise and smooth the image.

3. Edge detection: Use the edge detection functions in OpenCV, such as Canny, Sobel and other functions, to detect the edges in the image.

4. Line detection: Use the Hough transform function HoughLines in OpenCV to detect lines in the image.

5. Zebra crossing detection: According to the geometric shape and color characteristics of the zebra crossing, the straight line that meets the conditions is screened out, which is the zebra crossing.

Here is a simple Python code example:
import cv2

# read image
img = cv2.imread("zebra_crossing.jpg")

# 预处理
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_smooth = cv2.GaussianBlur(img_gray, (3, 3), 0)
img_edge = cv2.Canny(img_smooth, 50, 150)

# Line detection
lines = cv2.HoughLines(img_edge, 1, cv2.PI / 180, 100)

# Zebra detection
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        a, b = cv2.cos(theta), cv2.sin(theta)
        x0, y0 = a * rho, b * rho
        pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2
        = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a) ))
        # Determine whether the line is a zebra crossing
        # ...

# 显示结果
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        a, b = cv2.cos(theta), cv2.sin(theta)
        x0, y0 = a * rho, b * rho
        pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
        pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))
        cv2.line(img, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("output", img)
cv2.waitKey()

In the above code, we first read the image, and then perform preprocessing, including converting to grayscale image, denoising and smoothing operations, and Canny algorithm for edge detection. Next use the Hough transform to detect straight lines. Finally, we need to judge whether the straight line is a zebra crossing according to the characteristics of the zebra crossing, and the specific implementation method is omitted here. Finally, the detected straight lines are displayed on the original image.

Guess you like

Origin blog.csdn.net/weixin_43271137/article/details/130078703