How To Detect The Zebra Crossing With OpenCV

发布于 23 天前  63 次阅读

Before the start

For some stupid race, I have to detect the zebra crossing without deep learning.

The base method is cut and Adapter Threshold the image. After that, do the edge detect for pix count to final got the goat.

Adapter Threshold

Comparison of Binarization Methods: Otsu's Method, and Adaptive Thresholding

In image processing, we always need to perform binarization on grayscale images, and these methods are commonly used.

Otsu's Method

The essence of the OTSU algorithm is to maximize the between-class variance.

So the main idea of this method is find a value t, and we using the t as a threshold, divide the image pixels into those greater than t (class A)and those less than t(class B).

First, we traverse each pixel depth from 0 to 255 and count the number of occurrences.

Let N𝑖​ be the number of pixels with grayscale value 𝑖, then the probability 𝑃𝑖​ of pixels with grayscale value 𝑖 is given by

P_i =\frac{n_i}{n_0+n_1+...+n_{255}}

We should find a 𝑡 such that the variance between classes A and B is maximized. This can be considered as a maximization problem. Traverse all integers from 0 to 255 with 𝑡, calculate the variance corresponding to each 𝑡, and select the 𝑡t with the maximum variance.

When the threshold value is set to 𝑘k, the probability that a pixel is assigned to A is denoted as 𝑝𝑎(𝑘)pa​(k), and the average gray level of pixels assigned to A is denoted as 𝑚𝑎(𝑘)ma​(k).

Similarly, the probability that a pixel is assigned to B is denoted as 𝑝𝑏(𝑘)pb​(k), and the average gray level of pixels assigned to B is denoted as 𝑚𝑏(𝑘)mb​(k).

variance σ could be solved with:

\sigma^{2} = \frac{(m_G *p_A(k)-m)^2}{p_A(k)(1-p_A(k))} 

We just need to find the k make the σ get the maximized.

Let`s do a example


Adaptive Thresholding

Adaptive Thresholding is another way but more easily to find the edge of the object.

Frist of the Adaptive Thresholding is constructing a Gaussian kernel.

Gaussian distribution, defined by its probability density function.

f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left( -\frac{(x - \mu)^2}{2\sigma^2} \right)

At this point, we extend to two-dimensional images. Generally, we set the σ of the x-axis and y-axis to be equal and μ to 0. By setting ρ to 0 in the above equation, we can obtain the expression for the two-dimensional Gaussian function as follows, where the value of σ determines the width of the Gaussian function.

g(x,y) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left( -\frac{(x^2 + y^2)}{2\sigma^2} \right)

For example, a 3x3 pixel Gaussian filter requires a template as follows.

For example the value of pix could be solved with:

\frac{1}{16} & \frac{2}{16} & \frac{1}{16} \\
\frac{2}{16} & \frac{4}{16} & \frac{2}{16} \\
\frac{1}{16} & \frac{2}{16} & \frac{1}{16}
Adaptive Thresholding

Edge Detect

最后更新于 2024-05-29