/// <summary> /// 车牌校正 /// </summary> public void RectifyLP(out GrayImg pDestImg, GrayImg pSrcImg, GrayImg pModelImg, ref PAngle Angle) { pDestImg = new GrayImg(); GrayImg RowDiffImg; Diff(out RowDiffImg, pModelImg, "TPrjType.H_FILTER"); HoughTrans(ref Angle, RowDiffImg, "TPrjType.V_FILTER"); Diff(out RowDiffImg, pModelImg, "TPrjType.V_FILTER"); HoughTrans(ref Angle, RowDiffImg, "TPrjType.H_FILTER"); RectifyLP_2(out pDestImg, pSrcImg, Angle); }
/// <summary> /// 倾斜度校正(新增) /// </summary> /// <param name="pDestImg"></param> /// <param name="pSrcImg"></param> /// <param name="Angle"></param> public void RectifyLP_2(out GrayImg pDestImg, GrayImg pSrcImg, PAngle Angle) { int i, j; double RectifyAngle = 0; ; int iAngle; GrayImg TempImg1, TempImg2; PicBase.ImgMalloc(out TempImg1, pSrcImg.Width, pSrcImg.Height); if (Math.Abs(Angle.HAngle + 90) != 90.0) RectifyAngle = -(Angle.HAngle + 90.0) / 180.0f * Math.PI; for (i = 0; i < pSrcImg.Width; i++) for (j = 0; j < pSrcImg.Height; j++) { if (i * Math.Tan(RectifyAngle) < 0.0) iAngle = (int)(i * Math.Tan(RectifyAngle) - 0.5); else iAngle = (int)(i * Math.Tan(RectifyAngle) + 0.5); if ((j - iAngle >= 0) && (j - iAngle < pSrcImg.Height)) { TempImg1.Img[j * TempImg1.Width + i] = pSrcImg.Img[(j - iAngle) * pSrcImg.Width + i]; } } PicBase.ImgMalloc(out TempImg2, pSrcImg.Width, pSrcImg.Height); if (Math.Abs(Angle.VAngle) != 90.0) RectifyAngle = Angle.VAngle / 180.0f * Math.PI; for (i = 0; i < TempImg1.Height; i++) for (j = 0; j < TempImg1.Width; j++) { if (i * Math.Tan(RectifyAngle) < 0.0) iAngle = (int)(i * Math.Tan(RectifyAngle) - 0.5); else iAngle = (int)(i * Math.Tan(RectifyAngle) + 0.5); if ((j - iAngle >= 0) && (j - iAngle < TempImg1.Width)) { TempImg2.Img[i * TempImg2.Width + j] = TempImg1.Img[i * TempImg1.Width + j - iAngle]; } } pDestImg.Width = TempImg2.Width; pDestImg.Height = TempImg2.Height; pDestImg.Img = TempImg2.Img; }
/// <summary> /// Hough变换进行车牌校正 /// </summary> /// public int HoughTrans(ref PAngle pAngle, GrayImg pSrcImg, string PrjType) { int i, j, p, o, Max, Tempo; int[] H; int HWidth, HHeight; double temp; o = -90; p = 0; HHeight = 4 * pSrcImg.Width + 1; HWidth = 181; H = new int[HWidth * HHeight]; for (i = -(HHeight / 2); i <= (HHeight / 2); i++) for (j = -(HWidth / 2); j <= (HWidth / 2); j++) H[(i + HHeight / 2) * HWidth + j + HWidth / 2] = 0; switch (PrjType) { case "TPrjType.H_FILTER": for (i = 0; i < pSrcImg.Height; i++) for (j = 0; j < pSrcImg.Width; j++) { if (pSrcImg.Img[i * pSrcImg.Width + j] == 255) { for (o = -10; o <= 10; o++) { if (o >= 0) Tempo = o + 80; else Tempo = o - 80; temp = j * Math.Cos(Tempo * Math.PI / 180.0f) + i * Math.Sin(Tempo * Math.PI / 180.0f); if (temp < 0.0) p = (int)(temp - 0.5); else p = (int)(temp + 0.5); if ((p + HHeight / 2 >= 0) && (p + HHeight / 2 < HHeight)) H[(p + HHeight / 2) * HWidth + Tempo + HWidth / 2]++; } } } Max = 0; for (i = -HHeight / 2; i <= HHeight / 2; i++) for (j = -10; j <= 10; j++) { if (j >= 0) Tempo = j + 80; else Tempo = j - 80; if (Max < H[(i + HHeight / 2) * HWidth + Tempo + HWidth / 2]) { Max = H[(i + HHeight / 2) * HWidth + Tempo + HWidth / 2]; o = Tempo; p = i; } } pAngle.HAngle = o; break; case "TPrjType.V_FILTER": for (i = 0; i < pSrcImg.Height; i++) for (j = 0; j < pSrcImg.Width; j++) { if (pSrcImg.Img[i * pSrcImg.Width + j] == 255) { for (o = -10; o <= 10; o++) { temp = j * Math.Cos(o * Math.PI / 180.0f) + i * Math.Sin(o * Math.PI / 180.0f); if (temp < 0.0) p = (int)(temp - 0.5); else p = (int)(temp + 0.5); if ((p + HHeight / 2 >= 0) && (p + HHeight / 2 < HHeight)) H[(p + HHeight / 2) * HWidth + o + HWidth / 2]++; } } } Max = 0; for (i = -HHeight / 2; i <= HHeight / 2; i++) for (j = -10; j <= 10; j++) { if (Max < H[(i + HHeight / 2) * HWidth + j + HWidth / 2]) { Max = H[(i + HHeight / 2) * HWidth + j + HWidth / 2]; o = j; p = i; } } pAngle.VAngle = o; break; default: return 0; } return 1; }