# A Real-Time Car Towing Management System Using ML-Powered Automatic Number Plate Recognition
## 3 Methods
### Plate Detection  
Algorithm 1 describes the process of plate localization and detection. Image preprocessing is the first in our plate detection module, which is required to reduce the complexity of processing color images by converting the colored input image into a grayscale version by applying image desaturation. The desaturation process makes pixel colors more muted by adding more black and white colors. The cv2.cvtColor function uses the following formula to convert the color RGB value (24-bit) of every pixel \((i,j)\) to a gray-scale level (8-bit) value:  
\[S(i,j)=\frac{1}{10}(3*R(i,j)+6*G(i,j)+B(i,j)) \tag{1}\]  
where the spectrum of \(R\), \(G\) and \(B\) is \(R(i,j)\), \(G(i,j)\) and \(B(i,j)\), respectively; and \(S(i,j)\) is the converted grey-scale image. The cv2.adaptiveThreshold function is used to convert the grayscale image into a binary image (see Figure 2b). To achieve the best accuracy of determining every pixel as an object or background, we used the adaptive threshold method that works better in the case of having different lighting conditions in the input image. The adaptive threshold algorithm calculates a different threshold value for every small region of the image, which gives better results for images with varying illumination. The cv2.ADAPTIVE_THRESH_GAUSSIAN_C is used as a threshold parameter, which is the weighted sum of neighborhood pixel values where weights are represented by a Gaussian window [13]. The effect of applying the adaptive thresholding function is demonstrated in Figure 2c. Figure 2: Phases of the plate detection, character segmentation, and recognition modules. ```
procedureFindingPlateLocation Input: InputVehicleImage(carImage) Output:ExtractedPlateImage(plateImage) /*ImagePreprocessing*/ imgGrayscale=cv2.cvtColor(carImage,cv2.COLOR_BGR2 HSV.value); imgThreshold =cv2.adaptiveThreshold(imgGrayscale,cv2.ADAPTIVE_THRESH_GAUSSIAN_C); /*MorphologicalTransformation*/ contourList=extractImageContours(imgThreshold); foreachcontourcincontourListdo t_c =morphological_Transformation(n_c);  reducedContourList.add(t_c); endfor /*ImageSegmentation*/ charList=imageSegmentation(reducedContourList); /*Drawaboundingboxoverthecandidateregions*/ foreachcharSequencecincharListdo region_box=drawBoundingBox(c); ifisActualPlate(region_box)istruethen  plateImage=extractPlate(region_box);  returnplateImage; endif endfor
```  
**Algorithm 1** Plate Detection Algorithm. The adaptive threshold is calculated based on the local mean of pixels intensity in Gaussian windows of \(m\times n\) pixels, as shown below:  
\[O(x,y)= \begin{cases}255,&\text{if }I(x,y)<\frac{\sum\limits_{i=x-\frac{m}{2}} ^{x+\frac{m}{2}}\sum\limits_{j=y-\frac{m}{2}}^{y+\frac{m}{2}}I(i,j)}{m\pi n}\\ 0,&\text{if }I(x,y)\geq\frac{\sum\limits_{i=x-\frac{m}{2}}^{x+\frac{m}{2}} \sum\limits_{j=y-\frac{m}{2}}^{y+\frac{m}{2}}I(i,j)}{m\pi n}\end{cases}  \tag{2}\]  
where \(I\) and \(O\) are the input and output images, respectively; and \(m\) and \(n\) are the window size parameters, which are selected based on the character's size in the region. To reduce the irrelevant contours from the thresholded image, we used a series of morphological transformations: erosion, opening, closing and gradient operations. Figure 2d shows the effect of the used morphological adaptations on the image. Our model used the Connected Component Analysis (CCA) method [19] for image postprocessing to remove the remaining irrelevant contours and objects in the image. The CCA method groups the connected regions (pixels) that belong to the same object. In particular, the CCA method labels each discovered area of connected pixels by a unique component number. We used the eight-connected component exploration strategy to find the candidate regions in the image. Figure 2e illustrates the image after postprocessing using CCA. The last step in the plate detection module is the image segmentation process, which separates the relevant objects in the image. Our model identifies candidate regions in the image and draws a bounding box over them.