⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lprprocess.cpp

📁 TRY2LPR-1.0开源的车牌识别核心
💻 CPP
📖 第 1 页 / 共 4 页
字号:

		*(pProject+pSrcImg->Width-1) = 0;

		break;

	default:
		return 0;
	}
	return 1;
}

int ProjectFilter(int *pProject, int PrjLen, TPrjType PrjType, int LPAccurateHeight)
{
	int i, j, p;
	int Temp, Temp1;
	BOOL Flag1, Flag2;
	int avg, CharID;
	int TempLeft[100], TempRight[100];
	int TempWidth[100], MergeID[100];

	*(pProject + PrjLen - 1) = 0;

	switch (PrjType)
	{
	case H_FILTER:  
		for (i=0; i<PrjLen; i++)
		{
			if (*(pProject+i) <  38 * 255)
				*(pProject+i) = 0;
		}
		Flag1 = TRUE;

		for (i=0; i<PrjLen; i++)
		{
			avg = 0;
			if ((*(pProject+i) != 0) && Flag1)
			{
				Temp = i;
				Flag1 = FALSE;
			}
			if ((*(pProject+i) == 0) && (!Flag1))
			{
				for (j=Temp; j<=i; j++)
					avg = avg + *(pProject+j) / 255;

				avg = avg / (i - Temp);

				if ((i-Temp<(int)(g_LPHeight*0.35+0.5)) ||
							(avg<(int)(g_LPWidth*0.30+0.5)))
				{
					for (j=Temp; j<i; j++)
						*(pProject+j) = 0;
				}

				Flag1 = TRUE;
			}
		}
		break;

	case V_FILTER:
		for (j=0; j<PrjLen; j++)
        {
			if (*(pProject+j) < (int)(0.2*g_LPHeight*255+0.5))
				*(pProject+j) = 0;
		}
		Flag1 = TRUE;
		for (i=0; i<PrjLen; i++)
		{
			avg = 0;
			if ((*(pProject+i)!=0) && Flag1)
			{
				Temp = i;
				Flag1 = FALSE;
			}
			
			if ((*(pProject+i) == 0) && (!Flag1))
			{
				for (j=Temp; j<=i; j++)
					avg = avg + *(pProject+j)/255;

				avg = avg / (i - Temp);

				if ((i-Temp<(int)(g_LPWidth*0.015+0.5)) ||
								(avg<(int)(g_LPHeight*0.15+0.5)))
				{
					for (j=Temp; j<i; j++)
						*(pProject+j) = 0;
				}
				Flag1 = TRUE;
			}
		}

		Flag2 = TRUE; Flag1 = TRUE;
		for (i=0; i<PrjLen; i++)
		{
			if ((*(pProject+i)!=0) && Flag1)
			{
				Flag1 = FALSE;
			}

			if ((*(pProject+i)==0) && (!Flag1))
			{
				Temp1 = i;
				Flag1 = TRUE;
				Flag2 = FALSE;
			}

			if ((*(pProject+i)!=0) && (!Flag2))
			{
				if ((i-Temp1)<(int)(0.11*g_LPWidth+0.5))
				{
					for (j=Temp1; j<i; j++)
						*(pProject+j) = 17 * 255;
				}
				Flag2 = TRUE;
			}
		}

		Flag1 = TRUE; Temp = 0;
		for (i=0; i<PrjLen; i++)
		{
			if ((*(pProject+i) != 0) && Flag1)
			{
				Temp = i;
				Flag1 = FALSE;
			}

			if ((*(pProject+i)==0) && (!Flag1))
			{
				if (i-Temp > (int)(g_LPWidth*1.5+0.5))
				{
					for (j=Temp; j<i; j++)
						*(pProject+j) = 0;
				}
				Flag1 = TRUE;
			}
		}
		break;

	case IH_FILTER :
		
		for (i=0; i<PrjLen; i++)
		{
			if (*(pProject+i) < 16 * 255)
			{
				*(pProject+i) = 0;
			}
		}
		break;

	case IV_FILTER :
		for (i=0; i<PrjLen; i++)
		{
			if (*(pProject+i) < (int)(0.1*LPAccurateHeight+0.5)*255)
		        *(pProject+i) = 0;
		}
		
		Flag1 = TRUE;
		for (i=0; i<PrjLen; i++)
		{
			if ((*(pProject+i) != 0) && Flag1)
			{
				Temp = i;
				Flag1 = FALSE;
			}

			if ((*(pProject+i) == 0) && (!Flag1))
			{
				if (i - Temp < (int)(0.2 * LPAccurateHeight + 0.5))
				{
					for (j=Temp; j<i; j++)
						if (*(pProject+j) < (int)(0.3*LPAccurateHeight + 0.5)*255)
							*(pProject+j) = 0;
				}
				Flag1 = TRUE;
			}
		}

		if (PrjLen > (int)(4.6 * LPAccurateHeight + 0.5))
		{
			Flag2 = TRUE; Flag1 = TRUE;
			for (i=0; i<=(int)((PrjLen-1)/3.0); i++)
			{
				if ((*(pProject+i) != 0) && Flag1)
				{
					Flag1 = FALSE;
				}
				
				if ((*(pProject+i) == 0) && (!Flag1))
				{
					Temp1 = i;         
					Flag1 = TRUE;
					Flag2 = FALSE;
				}

		        if ((*(pProject+i) != 0) && (!Flag2))
				{
					if (i-Temp1 > (int)(0.6*LPAccurateHeight+0.5))
					{
						for (j=i-1; j>=0; j--)
							*(pProject+j) = 0;
					}
					Flag2 = TRUE;
				}
			}

			Flag1 = TRUE;
			CharID = 0;
			for (i=0; i<=(int)((PrjLen-1)/2.0); i++)
			{
				if ((*(pProject+i) != 0) && Flag1)
				{
					TempLeft[CharID] = i;
					Flag1 = FALSE;
				}

				if ((*(pProject+i) == 0) && (!Flag1))
				{
					TempRight[CharID] = i;
					TempWidth[CharID] = TempRight[CharID] - TempLeft[CharID];
					CharID++;
					if (CharID > 100)
					{
						return 0;
					}

					Flag1 = TRUE;
				}
			}

			j = 0;
			for (i=0; i<CharID; i++)
			{
				if (TempWidth[i] < (int)(0.35*LPAccurateHeight+0.5))
				{
					MergeID[j] = i;
					j++;
				}

				if ((i==CharID-1) || (TempWidth[i]>=(int)(0.35*LPAccurateHeight+0.5)))
				{
					 if (j > 1)
					 {
						for (p=TempLeft[MergeID[0]]; p<=TempRight[MergeID[j-1]]; p++)
						{
						   *(pProject+p) = (int)(0.5 * LPAccurateHeight + 0.5) * 255;
						}
					 }
					 j = 0;
				}
			}
		}

		Flag1 = TRUE;
		for (i=0; i<PrjLen; i++)
		{
			if ((*(pProject+i) != 0) && Flag1)
			{
				Temp = i;
				Flag1 = FALSE;
			}

			if ((*(pProject+i) == 0) && (!Flag1))
			{
				if (i-Temp < (int)(0.3*LPAccurateHeight+0.5))
				{
					for (j=Temp; j<i; j++)
						*(pProject+j) = 0;
				}
				Flag1 = TRUE;
			}
		}

		break;

	default:
		return 0;
	}

	return 1;
}

int TwoPixelCarImg(PGrayImg pDestImg, PGrayImg pSrcImg, int Threshold)
{
	int i, j;
	int res = 0;
	TGrayImg TempImg;

	res = GrayImg_Malloc(&TempImg, pSrcImg->Width, pSrcImg->Height);
	if (res == 0)
		return 0;

	for (i=0; i<pSrcImg->Height; i++)
		for (j=0; j<pSrcImg->Width; j++)
		{
			if (*(pSrcImg->pImg+i*pSrcImg->Width+j) > Threshold)
				*(TempImg.pImg+TempImg.Width*i+j) = 255;
			else
				*(TempImg.pImg+TempImg.Width*i+j) = 0;
		}

	pDestImg->Width = TempImg.Width;
	pDestImg->Height = TempImg.Height;
	pDestImg->pImg = TempImg.pImg;

	return 1;
}

int GetSubImg(PGrayImg pDestImg, PGrayImg pSrcImg, RECT Rect)
{
	int i, j;
	int res = 0;
	TGrayImg TempImg;

	int Left=0, Right=0, Top=0, Bottom=0;

	res = GrayImg_Malloc(&TempImg, Rect.right-Rect.left+1, Rect.bottom-Rect.top+1);
	if (0 == res)
		return 0;

	for (i=Rect.top; i<=Rect.bottom; i++)
		for (j=Rect.left; j<=Rect.right; j++)
		{
			if ((i>=0 && i<pSrcImg->Height) && (j>=0 && j<pSrcImg->Width))
				*(TempImg.pImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = *(pSrcImg->pImg+i*pSrcImg->Width+j);
			else
				*(TempImg.pImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = 0;
		}

	pDestImg->Width = TempImg.Width;
	pDestImg->Height = TempImg.Height;
	pDestImg->pImg = TempImg.pImg;

	return 1;
}

int GetSubColorImg(PColorImg pDestImg, PColorImg pSrcImg, RECT Rect)
{
	int i, j;
	int res = 0;
	TColorImg TempImg;

	res = ColorImg_Malloc(&TempImg, Rect.right-Rect.left+1, Rect.bottom-Rect.top+1);
	if (0 == res)
		return 0;

	for (i=Rect.top; i<=Rect.bottom; i++)
		for (j=Rect.left; j<=Rect.right; j++)
		{
			if ((i>=0 && i<pSrcImg->Height) && (j>=0 && j<pSrcImg->Width))
			{
				*(TempImg.pRImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = *(pSrcImg->pRImg+i*pSrcImg->Width+j);
				*(TempImg.pGImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = *(pSrcImg->pGImg+i*pSrcImg->Width+j);
				*(TempImg.pBImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = *(pSrcImg->pBImg+i*pSrcImg->Width+j);
				*(TempImg.pYImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = *(pSrcImg->pYImg+i*pSrcImg->Width+j);
			}
			else
			{
				*(TempImg.pRImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = 0;
				*(TempImg.pGImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = 0;
				*(TempImg.pBImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = 0;
				*(TempImg.pYImg+(i-Rect.top)*TempImg.Width+(j-Rect.left)) = 0;
			}
		}
	TempImg.BitCount = pSrcImg->BitCount;

	pDestImg->Width = TempImg.Width;
	pDestImg->Height = TempImg.Height;
	pDestImg->pRImg = TempImg.pRImg;
	pDestImg->pGImg = TempImg.pGImg;
	pDestImg->pBImg = TempImg.pBImg;
	pDestImg->pYImg = TempImg.pYImg;
	pDestImg->BitCount = TempImg.BitCount;

	return 1;
}

int RecoLPColor(TColorID *pLPColorID, PColorImg pSrcImg)
{
	int i, j;
	int ACount[4];
	int Temp;
	BYTE R, G, B;

	ACount[LP_BLACK] = 0;
	ACount[LP_BLUE] = 0; 
	ACount[LP_YELLOW] = 0; 
	ACount[LP_WHITE] = 0;

	for (i=(int)(0.15*pSrcImg->Height+0.5); i<(int)(pSrcImg->Height-0.15*pSrcImg->Height+0.5); i++)
		for (j=(int)(0.15*pSrcImg->Width+0.5); j<(int)(pSrcImg->Width-0.15*pSrcImg->Width+0.5); j++)
		{
			R = *(pSrcImg->pRImg+i*pSrcImg->Width+j);
			G = *(pSrcImg->pGImg+i*pSrcImg->Width+j);
			B = *(pSrcImg->pBImg+i*pSrcImg->Width+j);

			if ((B-G>g_BYDiff) && (B-R>g_BYDiff) && (B>g_BYLP))
				  ACount[LP_BLUE]++;

			if ((R-B>g_BYDiff) && (G-B>g_BYDiff) && (R>g_BYLP) && (G>g_BYLP))
				  ACount[LP_YELLOW]++;

			if ((B<g_BlackLP)&&(G<g_BlackLP)&&(R<g_BlackLP)&&(abs(G-B)<g_BWDiff)&&(abs(G-R)<g_BWDiff)&&(abs(B-R)<g_BWDiff))
				  ACount[LP_BLACK]++;

			if ((R>g_WhiteLP)&&(G>g_WhiteLP)&&(B>g_WhiteLP)&&(abs(G-B)<g_BWDiff)&&(abs(G-R)<g_BWDiff)&&(abs(B-R)<g_BWDiff))
				  ACount[LP_WHITE]++;
		}

	Temp = ACount[LP_BLUE];
	*pLPColorID = LP_BLUE;

	for (i=0; i<4; i++)
		if (Temp < ACount[i])
		{
			Temp = ACount[i];
			*pLPColorID = (TColorID)i;
		}
	return 1;
}

int TwoPixelLPImg(PGrayImg pDestImg, PGrayImg pSrcImg, 
				    double MaxBlackRatio, double MinBlackRatio, int *pThreshold)
{
	int i, j;
	int res = 0;
	int TempArray[9] = {1,1,1,
		                1,0,1,
						1,1,1};
	int TempArray1[9] = {-1,0,1,
						 -1,0,1,
						 -1,0,1};
	int *pProject=NULL;
	int WhitePixelCount, BlackPixelCount;
	int Bottom, Top;
	BOOL StopFlag1, StopFlag2;

	TGrayImg TempImg, EdgeImg, TempImg1;

	Top = 0;
	Bottom = pSrcImg->Height-1;

	res = Template(&EdgeImg, pSrcImg, TempArray1, 3, 1);
	if (0 == res)
		return 0;

	res = Template(&TempImg1, &EdgeImg, TempArray, 3, 1.0/8.0);
	if (0 == res)
		return 0;

	res = TwoPixelCarImg(&TempImg, &TempImg1, g_Threshold_HLP);
	if (0 == res)
		return 0;

	pProject = (int*)malloc(pSrcImg->Height*sizeof(int));
	if (pProject == NULL)
		return 0;

	res = ProjectImg(pProject, &TempImg, H_FILTER);
	if (0 == res)
		return 0;

	for (i=0; i<pSrcImg->Height; i++)
		if (*(pProject+i) < int(0.25*pSrcImg->Height))
			*(pProject+i) = 0;

	for (i=pSrcImg->Height/2; i>=0; i--)
	{
		if (*(pProject+i) == 0)
		{
			Top = i;
			break;
		}
	}
	for (i=pSrcImg->Height/2; i<pSrcImg->Height; i++)
	{
		if (*(pProject+i) == 0)
		{
			Bottom = i;
			break;
		}
	}

	if (pProject)
		free(pProject);

	GrayImg_Free(&EdgeImg);
	GrayImg_Free(&TempImg1);
	GrayImg_Free(&TempImg);

	*pThreshold = 50;

	res = GrayImg_Malloc(&TempImg, pSrcImg->Width, pSrcImg->Height);
	if (res == 0)
		return 0;

	StopFlag1 = FALSE;
    StopFlag2 = FALSE;

	while(TRUE)
	{
		BlackPixelCount = 0;
		WhitePixelCount = 0;

		for (i=0; i<pSrcImg->Height; i++)
			for (j=0; j<pSrcImg->Width; j++)
			{
				if (*(pSrcImg->pImg+i*pSrcImg->Width+j) > *pThreshold)
				{
					if (i>=Top && i<=Bottom)
						WhitePixelCount++;
					*(TempImg.pImg+i*TempImg.Width+j) = 255;
				}
				else
				{
					if (i>=Top && i<=Bottom)
						BlackPixelCount++;
					*(TempImg.pImg+i*TempImg.Width+j) = 0;
				}
			}

		if ((BlackPixelCount<=(int)(MaxBlackRatio*(BlackPixelCount+WhitePixelCount)+0.5))
		    && (BlackPixelCount>=(int)(MinBlackRatio*(BlackPixelCount+WhitePixelCount)+0.5)))
			break;

		if (BlackPixelCount>(int)(MaxBlackRatio*(BlackPixelCount+WhitePixelCount)+0.5))
		{
			StopFlag1 = TRUE;
			*pThreshold -= 5;
			if (StopFlag2)
				break;
		}

		if (BlackPixelCount < (int)(MinBlackRatio*(BlackPixelCount+WhitePixelCount)+0.5))
		{
			StopFlag2 = TRUE;
			*pThreshold += 5;
			if (StopFlag1)
				break;
		}
	}

	pDestImg->Width = TempImg.Width;
	pDestImg->Height = TempImg.Height;
	pDestImg->pImg = TempImg.pImg;

	return 1;
}

int Invert(PGrayImg pDestImg, PGrayImg pSrcImg)
{
	int i, j;
	int res = 0;
	TGrayImg TempImg;

	res = GrayImg_Malloc(&TempImg, pSrcImg->Width, pSrcImg->Height);
	if (0 == res)
		return 0;

	for (i=0; i<pSrcImg->Height; i++)
		for (j=0; j<pSrcImg->Width; j++)
		{
			*(TempImg.pImg+i*TempImg.Width+j) = 255 - *(pSrcImg->pImg+i*pSrcImg->Width+j);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -