stm32 digital recognition---realization of template matching algorithm based on Hamming distance

1. An overview of
OCR character recognition. The most commonly used is image segmentation + template matching. The previous blog has already written the process of image segmentation processing in the process of license plate recognition. This article focuses on how to use the Hamming distance to calculate the template matching of the image to perform digital identify.
Image recognition hardware: stm32F407VET6, 192KB RAM, 168MHz main frequency, with DCMI digital image interface, adopts OV7670 image without FIFO, directly collects camera data; 2. The
overall design idea
Hamming distance is a method used to calculate the similarity between two images A fast and effective method, especially for simple and clear-structured target images such as numbers/characters, directly calculating the Hamming distance can effectively perform template matching; the principle of the Hamming distance is to calculate the cumulative number of corresponding positions of 1 in two images number, the more the same number, the higher the similarity;
for a binary image, if 0xff represents an effective pixel, it means that the more corresponding 0xff, the higher the similarity; that is to say, directly select the template as binary Image binarization and digital segmentation can be used, and then image scaling can be performed for fast calculation; finally, digital template matching can be realized by calculating the Hamming distance between the target image and the binarized template;

2. Digital identification process

(1) Image acquisition. The previous blog post introduced the image acquisition process. If you don’t understand, you can go and have a look. Here, directly set the camera to YUV422 image output, and then copy the Y component to the image buffer in the DMA transmission interrupt processing function. The grayscale image can be obtained directly without complex image transformation processing.
(2) Image binarization processing, direct mean value binarization for simple background images, and OTSU adaptive threshold segmentation for images affected by light;
OTSU binarization segmentation:

int OtsuThread(unsigned char *src,int width,int height)
{
    
    	
	int th;
	const int GrayScale = 256;	//单通道图像总灰度256级
	unsigned char*psrc=src;
	int pixCount[GrayScale] = {
    
    0};//每个灰度值所占像素个数	
	int pixSum = width * height;//图像总像素点	
	float pixPro[GrayScale] = {
    
    0};//每个灰度值所占总像素比例	
	float w0, w1, u0tmp, u1tmp, u0, u1, deltaTmp, deltaMax = 0;  
	for(int i = 0; i < pixSum ; i++)	
	{
    
    	
		pixCount[*psrc++];//统计每个灰度级中像素的个数  			
	} 	
	for(int i = 0; i < GrayScale; i++)	
	{
    
    		
		pixPro[i] = pixCount[i] * 1.0 / pixSum;//计算每个灰度级的像素数目占整幅图像的比例  	
	} 	
	for(int i = 0; i < GrayScale; i++)//遍历所有从0到255灰度级的阈值分割条件,测试哪一个的类间方差最大	
	{
    
    		
		w0 = w1 = u0tmp = u1tmp = u0 = u1 = deltaTmp = 0;  		
		for(int j = 0; j < GrayScale; j++)		
		{
    
    		
			if(j <= i)//背景			
			{
    
    				
				w0 += pixPro[j];
				u0tmp += j * pixPro[j]; 		
			}
			else//前景			
			{
    
    
				w1 += pixPro[j];				
				u1tmp += j * pixPro[j];
							
			}		
		}	
		u0 = u0tmp / w0;		
		u1 = u1tmp / w1;		
		deltaTmp = (float)(w0 *w1* pow((u0 - u1), 2)); //类间方差公式 
		g = w1 * w2 * (u1 - u2) ^ 2		
		if(deltaTmp > deltaMax) 	
		{
    
    			
			deltaMax = deltaTmp;			
			th = i;  		
		}  	
	}	
	return th;
}

(2) Image segmentation, perform horizontal and vertical pixel statistics on the binarized image, and then perform digital area segmentation processing to extract the position coordinates of each number; for the alignment target, this simple segmentation can be used. For non-aligned targets, the position of each target can be obtained by calculating the connected domain; and for tilted targets, it can be segmented first and then processed by image rotation;

(3) Image scaling, scaling the image to the size of the binarized template, the fast algorithm for scaling is also introduced in the previous blog post on license plate recognition; character modulo method: first use character modulo software to generate character templates, or use matlab to The image is converted into a binary template for easy calculation;
(4) Hamming distance calculation similarity

//***************
输入: 目标图像,模板个数
输出:模板匹配结果
*********************//
void Hanming_zoen(unsigned char *iamge_zoen,unsigned char numb)
{
    
    

	 unsigned short i,kk,number,hanming_value,numb_tem=0;
	 unsigned char error[numb];//记录每个模板的误差个数
	 float zoen_rate;
	 char result; //字符结果
	 for(i=0;i<numb;i++)  
	 {
    
    
	 	for(kk=0;kk<MODULE_LENGTH;K++)  //MODULE_LENGTH为单个模板的长度
	 	{
    
    
	 		if(image_zoen[kk]==TARGET_VAL)  //TARGET_VAL由二值化后有效像素决定
	 		{
    
    	
	 			number++;
	 			if(module[i][kk]==TARGET_VAL)  //module为const 型模板数据
	 			{
    
    
	 				hanming_value++;
	 			}
	 		}
	 	}
		zoen_rate=1.0*hanming_value/number;//将汉明值转换成相似度;
		if(zoen_rate>0.5) error[i]=number-hanming_value;
		else error[i]=255;
		number=0;
		hanming_value=0;
	}
	for(i=0,number=0;i<numb;i++)
	{
    
    
		if(error[i]<error[number]
		{
    
    
			number=i;	//找误差最小的一个
		}
	}
	result=table_char[number];
	printf("result:%s",result)//串口输出结果	
}
	
	```
	
	

Guess you like

Origin blog.csdn.net/weixin_40672861/article/details/122309828