Python-based opencv image processing realizes the detection of zebra crossing!

The basic idea

Zebra crossing detection uses opencv image processing to perform gray value conversion, Gaussian filter denoising, threshold processing, corrosion and expansion to perform contour detection on the image, by judging the position of the vehicle and pedestrian, and the distance information between them, when the speed of the car is reached When it exceeds a certain threshold and is close to the pedestrian, the vehicle will be judged as an impolite pedestrian.

Example result

experiment process

First take a picture from the video to test, and if the result is satisfactory, then nest it into the video to achieve the desired effect.

1. Preprocessing (gray value conversion, Gaussian filter denoising, threshold processing, corrosion and expansion)> modify some values ​​according to your needs


#Gray value conversion imgGray = cv2.cvtColor(copy_img,cv2.COLOR_BGR2GRAY) #Gaussian 
filter denoising 
imgBlur = cv2.GaussianBlur(imgGray,(5,5),0) 
#Threshold 
processing ret,thresh = cv2.threshold(imgBlur , 127,255, cv2.THRESH_BINARY) 
# corrosion 
imgEro = cv2.erode (Thresh, kernel1, Iterations = 2) 
# expansion 
imgDia = cv2.dilate (imgEro, kernel2, iterations = 4)

After preprocessing (as shown in the figure below):


2. Contour detection

#轮廓检测
_,contouts,hie = cv2.findContours(imgDia,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contouts
cv2.drawContours(copy_img, cnt, -1, (0, 255, 0), 2)

Full outline (as shown below)

It can be seen that this is not what we want, so we need to judge the location and select the area we are interested in.

3. Region of interest

Change the position information value of x, y, w, h according to your own picture or video needs.

for i in cnt: 
    #Coordinate 
    assignment x,y,w,h = cv2.boundingRect(i) 
    #roi position judgment 
    if y>350 and y<450 and x<1200 and w>50 and h>10: 
        # Draw Contour 
        cv2.drawContours(copy_img, i, -1, (0, 255, 0), 2)

The complete result after obtaining roi (as shown in the figure below)


4. Complete code

import cv2 
import numpy as np 
#define two cores (kernel_Ero is used for corrosion, kernel_Dia is used for expansion) 
kernel_Ero = np.ones((3,1),np.uint8) 
kernel_Dia = np.ones((3,5), np.uint8) 

img = cv2.imread("../images/bmx.png") 
copy_img = img.copy() #Original 
image copy modify size 
copy_img = cv2.resize(copy_img,(1600,800)) 
#灰Degree value conversion 
imgGray = cv2.cvtColor(copy_img,cv2.COLOR_BGR2GRAY) #Gaussian 
filter denoising 
imgBlur = cv2.GaussianBlur(imgGray,(5,5),0) 
#Threshold 
processing ret,thresh = cv2.threshold(imgBlur,127,255 , cv2.THRESH_BINARY) 
# corrosion 
imgEro = cv2.erode (Thresh, kernel_Ero, Iterations = 2) 
# expansion 
imgDia = cv2.dilate (imgEro, kernel_Dia, iterations = 4)
 
# contour detection
_,contouts,hie = cv2.findContours(imgDia,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contouts

for i in cnt:
    #坐标赋值
    x,y,w,h = cv2.boundingRect(i)
    #roi位置判断
    if y>350 and y<450 and x<1200 and w>50 and h>10:
        # 画出轮廓
        cv2.drawContours(copy_img, i, -1, (0, 255, 0), 2)

cv2.imshow("img",copy_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

to sum up

In the business requirements, the result of this process is not feasible, it is just to frame what you want, but if you want to judge whether the vehicle is courteous to pedestrians in the traffic scene, you need to judge the coordinates. Draw a large rectangular frame (roi area) from the coordinates of the first zebra crossing to the coordinates of the last zebra crossing (horizontal), and then act on the coordinates of the motor vehicle (existing coordinates) according to the coordinates of the rectangular frame Judge, so as to meet the demand.

At last! ! !

First contact with opencv! So please don’t spray me, the big guys in the visual field! (/狗头) The
 
amount of code is very small, there is no generalization ability, a very low approach. . . But for me, learning opencv is still very helpful! That's it! Ollie give it!

Get the complete project code⬅Get it here

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109095656