实例介绍
【实例简介】下图是 车牌的 识别结果
1、把MYsource放在G盘
2、运行环境是Net framework2.0
3、必须得有E盘
以上G盘E盘可以到程序里面改,很好找到的
此实例从别处获得,如果侵犯了您的权益,请联系作者上传者删除
【实例截图】
下图为原始车牌
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace MyLPR
{
public partial class MainForm : Form
{
private Bitmap m_Bitmap;//存放最先打开的图片
private Bitmap always_Bitmap;//永远是彩色图片,用以备用
private Bitmap other_c_Bitmap;
private Bitmap extract_Bitmap_one;
private Bitmap extract_Bitmap_two;
private Bitmap z_Bitmap0;
private Bitmap z_Bitmap1;
private Bitmap z_Bitmap2;
private Bitmap z_Bitmap3;
private Bitmap z_Bitmap4;
private Bitmap z_Bitmap5;
private Bitmap z_Bitmap6;
//private Bitmap z_Bitmap7;
private Bitmap objNewPic;
private Bitmap c_Bitmap;//车牌图像
private Bitmap[] z_Bitmaptwo = new Bitmap[7];//用于储存最终的黑白字体
private Bitmap[] charFont;
private Bitmap[] provinceFont;
string[] charString;//存储的路径
string[] provinceString;//省份字体
string[] charDigitalString;
string[] provinceDigitalString;
Pen pen1 = new Pen(Color.Black);
private String name;
private float count;
private float[] gl = new float[256];
int[] gray = new int[256];
int[] rr = new int[256];
int[] gg = new int[256];
int[] bb = new int[256];
float[,] m = new float[5000, 5000];
int flag = 0, flag1 = 0;
int xx = -1;
//private bool aline = false;
public static string SourceBathOne = "D:\\licensePlate\\";//备用
public static string charSourceBath = "d:\\MYsource\\char\\";
public static string provinceSourceBath = "D:\\MYsource\\font\\";
public MainForm()
{
other_c_Bitmap = new Bitmap(2, 2);//暂时存放原图片
extract_Bitmap_one = new Bitmap(2, 2);//
InitializeComponent();
//this.skinEngine1.SkinFile = "SportsBlack.ssk";
}
private string[,] _ChildModule;
private int _ModuleButtonHeight = 30;
private int _ChildButtonHeight = 25;
private Bitmap singleBitmap = null;//特征训练的那个小图片
private String bathFlag = null;//在特征入库是,标识应该入库的路径
/// <summary>
/// 初始子模块
/// </summary>
public string[,] ChildModule
{
get { return _ChildModule; }
set { _ChildModule = value; }
}
//Bitmap _videoBitmap=null;
public Bitmap videoBitmap
{
get { return m_Bitmap; }
set {
this.clearAllPanel();
m_Bitmap = value;
Console.WriteLine(m_Bitmap.Width "asdfghjkkl" m_Bitmap.Height);
this.always_Bitmap = m_Bitmap.Clone(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), PixelFormat.DontCare);
this.label1.Text = "";
this.panel1.AutoScroll = true;
this.panel1.AutoScrollMinSize = new Size((int)(m_Bitmap.Width), (int)m_Bitmap.Height);
this.panel1.Invalidate();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
// this.skinEngine1.SkinFile = "EmeraldColor1.ssk";
toolTip1.SetToolTip(this.FontPanel1, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel2, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel3, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel4, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel5, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel6, "双击进行样本训练");
toolTip1.SetToolTip(this.FontPanel7, "双击进行样本训练");
ChildModule = new string[6, 4];
ChildModule[0, 0] = "打开";
ChildModule[0, 1] = "重新载入";
ChildModule[0, 2] = "保存";
ChildModule[0, 3] = "退出";
ChildModule[1, 0] = "灰度化";
ChildModule[1, 1] = "灰度均衡化";
ChildModule[1, 2] = "中值滤波";
ChildModule[1, 3] = "";
ChildModule[2, 0] = "sobel边缘检测";
ChildModule[2, 1] = "车牌定位";
ChildModule[2, 2] = "";
ChildModule[2, 3] = "";
ChildModule[3, 0] = "灰度化";
ChildModule[3, 1] = "二值化";
ChildModule[3, 2] = "人工校正";
ChildModule[3, 3] = "区域化";
ChildModule[4, 0] = "车牌识别";
ChildModule[4, 1] = "";
ChildModule[4, 2] = "";
ChildModule[4, 3] = "";
ChildModule[5, 0] = "特征训练";
ChildModule[5, 1] = "摄像头捕获";
ChildModule[5, 2] = "一键识别";
ChildModule[5, 3] = "";
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 车牌定位ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void button_Click(object sender, EventArgs e)
{
//标志是否找到用户点击的Button
bool findOutStatus = false;
//清除上一次操作加载的子菜单项
for (int i = 0; i < this.ControlPanel.Controls.Count; i )
{
if (this.ControlPanel.Controls[i].GetType().Name == "Panel")
{
this.ControlPanel.Controls.RemoveAt(i);
}
}
for (int i = 0; i < this.ControlPanel.Controls.Count; i )
{
if (this.ControlPanel.Controls[i].GetType().Name == "Button")
{
//重新定义各个button位置
if (!findOutStatus)
{
this.ControlPanel.Controls[i].Top = _ModuleButtonHeight * i;
}
else
{
this.ControlPanel.Controls[i].Top = this.ControlPanel.Height - (_ModuleButtonHeight * (6 - i));
}
//找到所点击的Button,在其下加载子菜单
if (this.ControlPanel.Controls[i].Name == ((Button)sender).Name)
{
findOutStatus = true;
// MessageBox.Show(((Button)sender).Name);
Panel panel = new Panel();
panel.BackColor = Color.AliceBlue;
panel.Top = _ModuleButtonHeight * (i 1);
panel.Width = this.ControlPanel.Width;
panel.Height = this.ControlPanel.Height - _ModuleButtonHeight * 6;
this.ControlPanel.Controls.Add(panel);
for (int j = 0; j < ChildModule.Length / 6; j )
{
if (!string.IsNullOrEmpty(ChildModule[i, j]))
{
Button btn = new Button();
btn.FlatStyle = FlatStyle.Flat;
btn.Top = _ChildButtonHeight * j;
btn.Width = this.ControlPanel.Width-50;
btn.Left = 50;
btn.Height = _ChildButtonHeight;
btn.Name = string.Format("ChildButton{0}_{1}", i.ToString(), j.ToString());
//Console.WriteLine(btn.Name);
btn.Text = ChildModule[i, j];
if (i == 0)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(文件_打开_Click); break;
case 1:
btn.Click = new EventHandler(文件_重新载入_Click); break;
case 2:
btn.Click = new EventHandler(文件_保存_Click); break;
case 3:
btn.Click =new EventHandler(文件_退出_Click);
break;
default:
Console.WriteLine("switch !");
break;
}
}
if (i == 1)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(t灰度化_Click); break;
case 1:
btn.Click = new EventHandler(t灰度均衡化_Click); break;
case 2:
btn.Click = new EventHandler(t高斯滤波_Click); break;
case 3:
break;
default:
break;
}
}
if (i == 2)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(sobel边缘检测_Click); break;
case 1:
btn.Click = new EventHandler(车牌定位_Click); break;
case 2:
case 3:
break;
default:
// Console.WriteLine("switch !");
break;
}
}
if (i == 3)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(c灰度化_Click); break;
case 1:
btn.Click = new EventHandler(c二值化_Click); break;
case 2:
btn.Click = new EventHandler(c人工校正_Click); break;
case 3:
btn.Click = new EventHandler(c区域化_Click);
break;
default:
//Console.WriteLine("switch !");
break;
}
}
if (i == 4)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(车牌识别_Click); break;
case 1:
btn.Click = new EventHandler(摄像头捕获_Click); break;
case 2:
btn.Click = new EventHandler(日志查询_Click); break;
case 3:
break;
default:
//Console.WriteLine("4switch !");
break;
}
}
if (i == 5)
{
switch (j)
{
case 0:
btn.Click = new EventHandler(特征训练_Click); break;
case 1:
btn.Click = new EventHandler(摄像头捕获_Click); break;
case 2:
btn.Click = new EventHandler(一键识别_Click); break;
case 3:
btn.Click = new EventHandler(一键识别_Click); break;
default:
//Console.WriteLine("4switch !");
break;
}
}
// btn.Click = new EventHandler(btnChild_Click);
panel.Controls.Add(btn);
}
}
}
}
}
}
/***************************************各个按钮事件**************************************/
//文件按钮事件
private void 文件_打开_Click(object sender, EventArgs e)
{
this.文件_打开();
}
private void 文件_重新载入_Click(object sender, EventArgs e)
{
if (name != null)
{
//this.GraphyPanel.Dispose();
this.clearAllPanel();
m_Bitmap = (Bitmap)Bitmap.FromFile(name, false);
panel1.Invalidate();//使panel1内的图像重新绘制
}
}
private void 文件_保存_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Bitmap文件(*.bmp)|*.bmp| Jpeg文件(*.jpg)|*.jpg| 所有合适文件(*.bmp/*.jpg)|*.bmp/*.jpg";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (DialogResult.OK == saveFileDialog.ShowDialog())
{
c_Bitmap.Save(saveFileDialog.FileName);
}
}
private void 文件_退出_Click(object sender, EventArgs e)
{
this.Close();
}
//车辆图片处理事件
private void t灰度化_Click(object sender, EventArgs e)
{
t灰度化();
}
private void t灰度均衡化_Click(object sender, EventArgs e)
{
t灰度均衡化();
}
private void t高斯滤波_Click(object sender, EventArgs e)
{
t高斯滤波();
}
//定位处理事件
private void sobel边缘检测_Click(object sender, EventArgs e)
{
sobel边缘检测();
}
private void 车牌定位_Click(object sender, EventArgs e)
{
//always_Bitmap = (Bitmap)Bitmap.FromFile(name, false);
this.c_Bitmap = Recoginzation.licensePlateLocation(m_Bitmap, always_Bitmap, m);
extract_Bitmap_one = c_Bitmap.Clone(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), PixelFormat.DontCare);
this.panel1.Invalidate();
this.LocatedPanel.Invalidate();
this.ExtractPanel.Invalidate();
}
//车牌处理事件
private void c灰度化_Click(object sender, EventArgs e)
{
c灰度化();
}
private void c二值化_Click(object sender, EventArgs e)
{
c二值化();
}
private void c人工校正_Click(object sender, EventArgs e)
{
rotation rotForm = new rotation();
Console.WriteLine("ren gong jiao zheng");
if (rotForm.ShowDialog() == DialogResult.OK)
{
int degree = Convert.ToInt32(rotForm.GetDegree);
this.c_Bitmap = GeneralTools.artificialController(c_Bitmap, extract_Bitmap_one, degree);
}
Rectangle sourceRectangle1 = new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height);
extract_Bitmap_one = c_Bitmap.Clone(sourceRectangle1, PixelFormat.DontCare);
LocatedPanel.Invalidate();
}
private void c区域化_Click(object sender, EventArgs e)
{
Accuracy();
}
//车牌识别事件
private void 车牌识别_Click(object sender, EventArgs e)
{
MaptoCharReg();
}
//其他工具事件
private void 特征训练_Click(object sender, EventArgs e)
{
Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
PictureTakeForm ptf = new PictureTakeForm();
ptf.Show();
}
private void 摄像头捕获_Click(object sender, EventArgs e)
{
Camera camera = new Camera(this);
camera.Show();
//Console.WriteLine("she xiang tou buhuo");
}
private void 日志查询_Click(object sender, EventArgs e)
{
}
private void 一键识别_Click(object sender, EventArgs e)
{
this.t灰度化();
this.t灰度均衡化();
t高斯滤波();
sobel边缘检测();
车牌定位();
c灰度化();
c二值化();
Accuracy();
MaptoCharReg();
}
/***************************************************************************/
//车辆图片处理事件
private void t灰度化()
{
if (m_Bitmap != null)
{
int tt = 0;
for (int i = 0; i < 256; i )//清掉数组gray里的数据
{
gray[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组rr里的数据
{
rr[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组gg里的数据
{
gg[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组bb里的数据
{
bb[i] = 0;
}
BitmapData bmData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;//获取或设置 Bitmap 对象的跨距宽度(也称为扫描宽度)。
System.IntPtr Scan0 = bmData.Scan0;//获取或设置位图中第一个像素数据的地址。 它也可以看成是位图中的第一个扫描行
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - m_Bitmap.Width * 3;
byte red, green, blue;
int nWidth = m_Bitmap.Width;
int nHeight = m_Bitmap.Height;
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
blue = p[0];
green = p[1];
red = p[2];
tt = p[0] = p[1] = p[2] = (byte)(.299 * red .587 * green .114 * blue);
rr[red] ;
gg[green] ;
bb[blue] ;
gray[tt] ; //统计灰度值为tt的象素点数目
p = 3;
}
p = nOffset;
}
}
m_Bitmap.UnlockBits(bmData);
flag = 1;
graydo();
}
}
private void t灰度均衡化()
{
if (m_Bitmap != null)
{
BitmapData bmData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//加入内存进行处理
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;//扫描的第一行
int tt = 0;
int[] SumGray = new int[256];
for (int i = 0; i < 256; i )
{
SumGray[i] = 0;
}
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - m_Bitmap.Width * 3;
int nHeight = m_Bitmap.Height;
int nWidth = m_Bitmap.Width;
SumGray[0] = gray[0];//灰度均衡化
for (int i = 1; i < 256; i)//灰度级频度数累加
SumGray[i] = SumGray[i - 1] gray[i];
for (int i = 0; i < 256; i) //计算调整灰度值 频率乘以灰度总级数得出该灰度变换后的灰度级
SumGray[i] = (int)(SumGray[i] * 255 / count);
for (int i = 0; i < 256; i )
{
gray[i] = 0;
}
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
tt = p[0] = p[1] = p[2] = (byte)(SumGray[p[0]]);
gray[tt] ;
p = 3;
}
p = nOffset;
}
}
m_Bitmap.UnlockBits(bmData);
flag = 1;
graydo();
}
}
private void t高斯滤波()
{
if (m_Bitmap != null)
{
BitmapData bmData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int i = 0; i < 256; i )
{
gray[i] = 0;
}
unsafe
{
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
byte* p = (byte*)(void*)Scan0;
byte* pp;
int tt;
int nOffset = stride - m_Bitmap.Width * 3;
int nWidth = m_Bitmap.Width;
int nHeight = m_Bitmap.Height;
long sum = 0;
int[,] gaussianMatrix = { { 1, 2, 3, 2, 1 }, { 2, 4, 6, 4, 2 }, { 3, 6, 7, 6, 3 }, { 2, 4, 6, 4, 2 }, { 1, 2, 3, 2, 1 } };//高斯滤波器所选的n=5模板
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if (!(x <= 1 || x >= nWidth - 2 || y <= 1 || y >= nHeight - 2))
{
pp = p;
sum = 0;
int dividend = 79;
for (int i = -2; i <= 2; i )
for (int j = -2; j <= 2; j )
{
pp = (j * 3 stride * i);
sum = pp[0] * gaussianMatrix[i 2, j 2];
if (i == 0 && j == 0)
{
if (pp[0] > 240)//如果模板中心的灰度大于240
{
sum = p[0] * 30;
dividend = 30;
}
else if (pp[0] > 230)
{
sum = pp[0] * 20;
dividend = 20;
}
else if (pp[0] > 220)
{
sum = p[0] * 15;
dividend = 15;
}
else if (pp[0] > 210)
{
sum = pp[0] * 10;
dividend = 10;
}
else if (p[0] > 200)
{
sum = pp[0] * 5;
dividend = 5;
}
}
pp = p;
}
sum = sum / dividend;
if (sum > 255)
{
sum = 255;
}
p[0] = p[1] = p[2] = (byte)(sum);
}
tt = p[0];
gray[tt] ;
p = 3;
}
p = nOffset;
}
}
flag = 1;
m_Bitmap.UnlockBits(bmData);
graydo();
}
}
//定位处理事件
private void sobel边缘检测()
{
if (m_Bitmap != null)
{
BitmapData bmData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
float valve = 67;
for (int i = 0; i < 256; i )
{
gray[i] = 0;
}
unsafe
{
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
byte* p = (byte*)(void*)Scan0;
byte* pp;
int tt;
int nOffset = stride - m_Bitmap.Width * 3;
int nWidth = m_Bitmap.Width;
int nHeight = m_Bitmap.Height;
int Sx = 0;
int Sy = 0;
// float max = 0;
double sumM = 0;
double sumCount = 0;
int[] marginalMx = { -1, 0, 1, -2, 0, 2, -1, 0, 1 }; //sobel模板
int[] marginalMy = { 1, 2, 1, 0, 0, 0, -1, -2, -1 };
int[,] dlta = new int[nHeight, nWidth];
for (int y = 0; y < nHeight; y) //sobel算子
{
for (int x = 0; x < nWidth; x)
{
if (!(x <= 0 || x >= nWidth - 1 || y <= 0 || y >= nHeight - 1))
{
pp = p;
Sx = 0;
Sy = 0;
for (int i = -1; i <= 1; i )
for (int j = -1; j <= 1; j )
{
pp = (j * 3 stride * i);
Sx = pp[0] * marginalMx[(i 1) * 3 j 1];
Sy = pp[0] * marginalMy[(i 1) * 3 j 1];
pp = p;
}
m[y, x] = (int)(Math.Sqrt(Sx * Sx Sy * Sy));
if (m[y, x] > valve / 2) //增强白点
{
if (p[0] > 240)
{
m[y, x] = valve;
}
else if (p[0] > 220)
{
m[y, x] = (float)(valve * 0.8);
}
else if (p[0] > 200)
{
m[y, x] = (float)(valve * 0.6);
}
else if (p[0] > 180)
{
m[y, x] = (float)(valve * 0.4);
}
else if (p[0] > 160)
{
m[y, x] = (float)(valve * 0.2);
}
}
float tan;
if (Sx != 0)
{
tan = Sy / Sx;
}
else tan = 10000;
if (-0.41421356 <= tan && tan < 0.41421356)//角度为-22.5度到22.5度之间
{
dlta[y, x] = 0; // m[y,x] =valve;
}
else if (0.41421356 <= tan && tan < 2.41421356)//角度为22.5度到67.5度之间
{
dlta[y, x] = 1; //m[y,x] = 0;
}
else if (tan >= 2.41421356 || tan < -2.41421356)//角度为67.5度到90度之间或-90度到-67.5度
{
dlta[y, x] = 2; // m[y,x] =valve;
}
else
{
dlta[y, x] = 3;//m[y,x] = 0;
}
}
else
m[y, x] = 0;
p = 3;
if (m[y, x] > 0)
{
sumCount ;
sumM = m[y, x];
}
}
p = nOffset;
}
p = (byte*)(void*)Scan0; //非极大值抑制和阀值
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if (m[y, x] > sumM / sumCount * 1.2)
{
p[0] = p[1] = p[2] = (byte)(m[y, x]); //m[y,x]=1;
}
else
{
m[y, x] = 0;
p[0] = p[1] = p[2] = 0;
}
if (x >= 1 && x <= nWidth - 1 && y >= 1 && y <= nHeight - 1 && m[y, x] > valve)
{
switch (dlta[y, x])
{
case 0:
if (m[y, x] >= m[y, x - 1] && m[y, x] >= m[y, x 1])//水平边缘
{
p[0] = p[1] = p[2] = 255;
}
break;
case 1:
if (m[y, x] >= m[y 1, x - 1] && m[y, x] >= m[y - 1, x 1])//正斜45度边缘
{
p[0] = p[1] = p[2] = 255;
}
break;
case 2:
if (m[y, x] >= m[y - 1, x] && m[y, x] >= m[y 1, x])//垂直边缘
{
p[0] = p[1] = p[2] = 255;
}
break;
case 3:
if (m[y, x] >= m[y 1, x 1] && m[y, x] >= m[y - 1, x - 1])//反斜45度边缘
{
p[0] = p[1] = p[2] = 255;
}
break;
}
}
if (p[0] == 255)
{
m[y, x] = 1;
}
else
{
m[y, x] = 0;
p[0] = p[1] = p[2] = 0;
}
tt = p[0];
gray[tt] ;
p = 3;
}
// p = nOffset;
}
m_Bitmap.UnlockBits(bmData);
flag = 1;
graydo();
}
}
}
private void 车牌定位()
{
// always_Bitmap = (Bitmap)Bitmap.FromFile(name, false);
this.c_Bitmap = Recoginzation.licensePlateLocation(m_Bitmap, always_Bitmap, m);
extract_Bitmap_one = c_Bitmap.Clone(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), PixelFormat.DontCare);
this.panel1.Invalidate();
this.LocatedPanel.Invalidate();
this.ExtractPanel.Invalidate();
}
//车牌处理事件
private void c灰度化()
{
if (c_Bitmap != null)
{
int tt = 0;
for (int i = 0; i < 256; i )//清掉数组gray里的数据
{
gray[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组rr里的数据
{
rr[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组gg里的数据
{
gg[i] = 0;
}
for (int i = 0; i < 256; i )//清掉数组bb里的数据
{
bb[i] = 0;
}
BitmapData bmData = c_Bitmap.LockBits(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;//获取或设置 Bitmap 对象的跨距宽度(也称为扫描宽度)。
System.IntPtr Scan0 = bmData.Scan0;//获取或设置位图中第一个像素数据的地址。 它也可以看成是位图中的第一个扫描行
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - c_Bitmap.Width * 3;
byte red, green, blue;
int nWidth = c_Bitmap.Width;
int nHeight = c_Bitmap.Height;
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
blue = p[0];
green = p[1];
red = p[2];
tt = p[0] = p[1] = p[2] = (byte)(.299 * red .587 * green .114 * blue);
rr[red] ;
gg[green] ;
bb[blue] ;
gray[tt] ; //统计灰度值为tt的象素点数目
p = 3;
}
p = nOffset;
}
}
c_Bitmap.UnlockBits(bmData);
flag = 2;
this.LocatedPanel.Invalidate();
panel1.Invalidate();
graydo();
}
}
private void c二值化()
{
if (c_Bitmap != null)
{
int Mr = 0;//灰度均值
long sum = 0;
int count = 0;
for (int i = 0; i < 256; i )//像素个数与灰度等级的乘积除以像素个数
{
sum = gray[i] * i;
count = gray[i];
}
Mr = (int)(sum / count);
int sum1 = 0;
int count1 = 0;
for (int i = 0; i <= Mr; i )
{
sum1 = gray[i] * i;
count1 = gray[i];
}
int g1 = sum1 / count1;
int sum2 = 0;
int count2 = 0;
for (int i = Mr; i <= 255; i )
{
sum2 = gray[i] * i;
count2 = gray[i];
}
int g2 = sum2 / count2;
//求阀值
int va;
if (count1 < count2)
{//白底黑字
va = Mr - count1 / count2 * Math.Abs(g1 - Mr);
}
else //黑底白字
va = Mr count2 / count1 * Math.Abs(g2 - Mr);
BitmapData bmData = c_Bitmap.LockBits(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - c_Bitmap.Width * 3;
int nWidth = c_Bitmap.Width;
int nHeight = c_Bitmap.Height;
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if (p[0] > va)
{
p[0] = p[1] = p[2] = 255;
}
else
p[0] = p[1] = p[2] = 0;
p = 3;
}
p = nOffset;
}
}
c_Bitmap.UnlockBits(bmData);
LocatedPanel.Invalidate();
}
}
/*******************************************************************************/
/***********************************其他的一些事件***********************************/
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (m_Bitmap != null)
{
Graphics g = e.Graphics;
if (m_Bitmap.Width < panel1.Width && m_Bitmap.Height < panel1.Height)
{
g.DrawImage(m_Bitmap, new Rectangle((this.panel1.Width-m_Bitmap.Width)/2, (this.panel1.Height-m_Bitmap.Height)/2, (int)(m_Bitmap.Width), (int)(m_Bitmap.Height)));
}
else
{
g.DrawImage(m_Bitmap, new Rectangle(this.panel1.AutoScrollPosition.X, this.panel1.AutoScrollPosition.Y, (int)(m_Bitmap.Width), (int)(m_Bitmap.Height)));
}
}
}
private void 文件_打开()
{
this.clearAllPanel();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Jpeg文件(*.jpg)|*.jpg|Bitmap文件(*.bmp)|*.bmp| 所有合适文件(*.bmp/*.jpg)|*.bmp/*.jpg";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;//该值指示对话框在关闭前是否还原当前目录
if (DialogResult.OK == openFileDialog.ShowDialog())
{
name = openFileDialog.FileName;
m_Bitmap = (Bitmap)Bitmap.FromFile(name, false);//使用该文件中的嵌入颜色管理信息,从指定的文件创建m_Bitmap
this.always_Bitmap = m_Bitmap.Clone(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), PixelFormat.DontCare);
this.panel1.AutoScroll = true;
this.panel1.AutoScrollMinSize = new Size((int)(m_Bitmap.Width), (int)m_Bitmap.Height);
this.label1.Text = name;
panel1.Invalidate();//使panel1内的图像重新绘制
}
}
private void graydo()//灰度直方图的调用
{
// this.flag = 1;
switch (flag)
{
case 1:
{
count = m_Bitmap.Width * m_Bitmap.Height;
gl = new float[256];
for (int i = 0; i < 256; i )
gl[i] = gray[i] / count * 8000;//灰度直方图计算公式
pen1 = Pens.Red;
panel1.Invalidate();
GraphyPanel.Invalidate();//使GraphyPanel内的图像重新绘制
break;
}
case 2:
{
count = c_Bitmap.Width * c_Bitmap.Height;
gl = new float[256];
for (int i = 0; i < 256; i )
gl[i] = gray[i] / count * 5000;//灰度直方图计算公式
pen1 = Pens.Gray;
panel1.Invalidate();
GraphyPanel.Invalidate();//使GraphyPanel内的图像重新绘制
break;
}
default:
break;
}
}
private void Accuracy()
{
if (c_Bitmap != null)
{
flag1 = 1;
BitmapData bmData = c_Bitmap.LockBits(new Rectangle(0, 0, c_Bitmap.Width, c_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//现在的c_Bitmap是车牌图片
unsafe
{
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - c_Bitmap.Width * 3;
int nWidth = c_Bitmap.Width;
int nHeight = c_Bitmap.Height;
int[] countHeight = new int[nHeight];
int[] countWidth = new int[nWidth];
int Yheight = nHeight, YBottom = 0;
for (int i = 0; i < nHeight; i )
{
countHeight[i] = 0;
}
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if ((p[0] == 0 && p[3] == 255) || (p[0] == 255 && p[3] == 0))
{
countHeight[y] ;
}
p = 3;
}
Console.WriteLine(y "*******************跳变数 " countHeight[y]);
p = nOffset;
}
//计算车牌号的上边缘
for (int y = nHeight / 2; y > 0; y--)
{
if (countHeight[y] >= 16 && countHeight[(y 1) % nHeight] >= 12)//12,6,11
{
if (Yheight > y)
{
Yheight = y;
//YBottom = y;
}//此if语句只执行了好多次
if ((Yheight - y) == 1)
{ Yheight = y - 3; Console.WriteLine("------------" Yheight); }
}
//Console.WriteLine("现在图片的顶部是:" Yheight);
}
//计算车牌号的下边缘
for (int y = nHeight / 2; y < nHeight; y )
{
if (countHeight[y] >= 12 && countHeight[(y 1) % nHeight] >= 12)//12,6,11
{
if (YBottom < y)
{
YBottom = y;
//YBottom = y;
}//此if语句只执行了一次
if ((y - YBottom) == 1)
{ YBottom = y 3; Console.WriteLine("------------" YBottom); }
}
// Console.WriteLine("NOW图片的底部是:" YBottom);
}
YBottom = 1;//主要目的是由于计算时少算了1
byte* p1 = (byte*)(void*)Scan0;
p1 = stride * (Yheight - 1);
for (int y = Yheight; y < YBottom; y)
{
for (int x = 0; x < nWidth; x)
{
if (p1[0] == 255)
countWidth[x] ;
p1 = 3;
}
p1 = nOffset;
}
int contg = 0, contd = 0, countRightEdge = 0, countLeftEdge = 0, Yl = nWidth, Yr = 0;
int[] XLeft = new int[20];
int[] XRight = new int[20];
foreach (int i in XRight)
{
XRight[i] = 0;
}
foreach (int i in XLeft)
{
XLeft[i] = 0;
}
for (int y = 1; y < YBottom - Yheight; y)
{
for (int x = 0; x < nWidth; x)
{
if (countWidth[(x 1) % nWidth] < y && countWidth[x] >= y && countWidth[Math.Abs((x - 1) % nWidth)] >= y && contg >= 2)
{
//if (countWidth[(x 1)] < y && countWidth[x] >= y && countWidth[Math.Abs((x - 1) )] >= y && contg >= 2)
//{
if (countRightEdge == 6)
{ Yr = x; }
if ((countRightEdge == 2 && (x >= XLeft[2] && XLeft[2] > 0)))
{
XRight[countRightEdge] = x;
countRightEdge ;
contd = 0;
}
else
{
if ((countRightEdge != 2))
{
if (countRightEdge == 0 && contg < 4)
{
XLeft[0] = 0;
countLeftEdge = 0;
}
if ((x >= XLeft[0] && XLeft[0] > 0))
{
XRight[countRightEdge] = x;
countRightEdge ;
contd = 0;
}
}
}
}
if (countWidth[Math.Abs((x - 1) % nWidth)] < y && countWidth[x] >= y && countWidth[(x 1) % nWidth] >= y && contd >= 2)
{
if (countLeftEdge == 0 && countWidth[(x 2) % nWidth] >= y)
{ Yl = x; }
if ((countLeftEdge == 2 && contd > 5))
{
XLeft[countLeftEdge] = x;
countLeftEdge ;
}
else
{
if ((countLeftEdge != 2))
{
XLeft[countLeftEdge] = x;
countLeftEdge ;
contg = 0;
if (countLeftEdge == 0 && countWidth[(x 2) % nWidth] < y)
{
XLeft[0] = 0;
countLeftEdge = 0;
}
}
}
}
contg ;
contd ;
}
if (countRightEdge countLeftEdge >= 14)
{
break;
}
countRightEdge = 0;
countLeftEdge = 0;
for (int i = 0; i < XRight.Length; i )
{
XRight[i] = 0;
}
for (int i = 0; i < XLeft.Length; i )
{
XLeft[i] = 0;
}
}
c_Bitmap.UnlockBits(bmData);
if ((YBottom - Yheight) > 1 && (Yr - Yl) > 1)
{
Rectangle sourceRectangle = new Rectangle(Yl, Yheight, Yr - Yl, YBottom - Yheight);
//c_Bitmap2是画线的那个图片
extract_Bitmap_two = extract_Bitmap_one.Clone(sourceRectangle, PixelFormat.DontCare);
BitmapData bmData2 = extract_Bitmap_two.LockBits(new Rectangle(0, 0, extract_Bitmap_two.Width, extract_Bitmap_two.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride2 = bmData2.Stride;
System.IntPtr Scan02 = bmData2.Scan0;
byte* p2 = (byte*)(void*)Scan02;
int nOffset2 = stride2 - extract_Bitmap_two.Width * 3;
int nWidth2 = extract_Bitmap_two.Width;
int nHeight2 = extract_Bitmap_two.Height;
for (int y = 0; y < nHeight2; y)
{
for (int x = 0; x < nWidth2; x)
{
if (x == (XRight[0] - Yl) || x == (XLeft[0] - Yl) || x == (XRight[1] - Yl) || x == (XLeft[1] - Yl) || x == (XRight[2] - Yl) || x == (XLeft[2] - Yl) || x == (XRight[3] - Yl) || x == (XLeft[3] - Yl) || x == (XRight[4] - Yl) || x == (XLeft[4] - Yl) || x == (XRight[5] - Yl) || x == (XLeft[5] - Yl) || x == (XRight[6] - Yl) || x == (XLeft[6] - Yl) || x == (XRight[7] - Yl) || x == (XLeft[7] - Yl))
{
if (x != 0)
{
p2[2] = 255; p2[0] = p2[1] = 0;
}
}
p2 = 3;
}
p2 = nOffset2;
}
extract_Bitmap_two.UnlockBits(bmData2);
this.ExtractPanel.Invalidate();
if ((YBottom - Yheight) > 1 && (XRight[1] - XLeft[1]) > 1)
{
Rectangle sourceRectangle2 = new Rectangle(XLeft[1], Yheight, XRight[1] - XLeft[1], YBottom - Yheight);
z_Bitmap1 = extract_Bitmap_one.Clone(sourceRectangle2, PixelFormat.DontCare);
z_Bitmaptwo[1] = c_Bitmap.Clone(sourceRectangle2, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[1], 9, 16);
z_Bitmaptwo[1] = objNewPic;
objNewPic.Save("d:\\1.bmp");
objNewPic = null;
FontPanel2.Invalidate();
}
if ((YBottom - Yheight) > 1 && (XRight[2] - XLeft[2]) > 1)
{
Rectangle sourceRectangle3 = new Rectangle(XLeft[2], Yheight, XRight[2] - XLeft[2], YBottom - Yheight);
z_Bitmap2 = extract_Bitmap_one.Clone(sourceRectangle3, PixelFormat.DontCare);
z_Bitmaptwo[2] = c_Bitmap.Clone(sourceRectangle3, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[2], 9, 16);
z_Bitmaptwo[2] = objNewPic;
objNewPic.Save("d:\\2.bmp");
objNewPic = null;
FontPanel3.Invalidate();
}
if ((YBottom - Yheight) > 1 && (XRight[3] - XLeft[3]) > 1)
{
Rectangle sourceRectangle4 = new Rectangle(XLeft[3], Yheight, XRight[3] - XLeft[3], YBottom - Yheight);
z_Bitmap3 = extract_Bitmap_one.Clone(sourceRectangle4, PixelFormat.DontCare);
z_Bitmaptwo[3] = c_Bitmap.Clone(sourceRectangle4, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[3], 9, 16);
z_Bitmaptwo[3] = objNewPic;
objNewPic.Save("d:\\3.bmp");
objNewPic = null;
FontPanel4.Invalidate();
}
if ((YBottom - Yheight) > 1 && (XRight[4] - XLeft[4]) > 1)
{
Rectangle sourceRectangle5 = new Rectangle(XLeft[4], Yheight, XRight[4] - XLeft[4], YBottom - Yheight);
z_Bitmap4 = extract_Bitmap_one.Clone(sourceRectangle5, PixelFormat.DontCare);
z_Bitmaptwo[4] = c_Bitmap.Clone(sourceRectangle5, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[4], 9, 16);
z_Bitmaptwo[4] = objNewPic;
objNewPic.Save("d:\\4.bmp");
objNewPic = null;
FontPanel5.Invalidate();
// z_Bitmap4.Dispose();
}
if ((YBottom - Yheight) > 1 && (XRight[5] - XLeft[5]) > 1)
{
Rectangle sourceRectangle6 = new Rectangle(XLeft[5], Yheight, XRight[5] - XLeft[5], YBottom - Yheight);
z_Bitmap5 = extract_Bitmap_one.Clone(sourceRectangle6, PixelFormat.DontCare);
z_Bitmaptwo[5] = c_Bitmap.Clone(sourceRectangle6, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[5], 9, 16);
z_Bitmaptwo[5] = objNewPic;
objNewPic.Save("d:\\5.bmp");
objNewPic = null;
FontPanel6.Invalidate();
// z_Bitmap5.Dispose();
}
if ((YBottom - Yheight) > 1 && (XRight[6] - XLeft[6]) > 1)
{
Rectangle sourceRectangle7 = new Rectangle(XLeft[6], Yheight, XRight[6] - XLeft[6], YBottom - Yheight);
z_Bitmap6 = extract_Bitmap_one.Clone(sourceRectangle7, PixelFormat.DontCare);
z_Bitmaptwo[6] = c_Bitmap.Clone(sourceRectangle7, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[6], 9, 16);
z_Bitmaptwo[6] = objNewPic;
objNewPic.Save("d:\\6.bmp");
objNewPic = null;
FontPanel7.Invalidate();
//z_Bitmap6.Dispose();
}
if ((YBottom - Yheight) > 1 && (XRight[0] - XLeft[0]) > 1)
{
Rectangle sourceRectangle0 = new Rectangle(XLeft[0], Yheight, XRight[0] - XLeft[0], YBottom - Yheight);
z_Bitmap0 = extract_Bitmap_one.Clone(sourceRectangle0, PixelFormat.DontCare);
z_Bitmaptwo[0] = c_Bitmap.Clone(sourceRectangle0, PixelFormat.DontCare);
objNewPic = new System.Drawing.Bitmap(z_Bitmaptwo[0], 9, 16);
z_Bitmaptwo[0] = objNewPic;
objNewPic.Save("d:\\0.bmp");
objNewPic = null;
FontPanel1.Invalidate();
}
}
}
}
}
private void clearAllPanel()
{
this.c_Bitmap = null;
this.extract_Bitmap_two = null;
this.z_Bitmap0 = null;
this.z_Bitmap1 = null;
this.z_Bitmap2 = null;
this.z_Bitmap3 = null;
this.z_Bitmap4 = null;
this.z_Bitmap5 = null;
this.z_Bitmap6 = null;
this.ResultLabel.Text = "";
this.label1.Text = "";
this.GraphyPanel.Invalidate();
this.LocatedPanel.Invalidate();
this.ExtractPanel.Invalidate();
this.FontPanel1.Invalidate();
this.FontPanel2.Invalidate();
this.FontPanel3.Invalidate();
this.FontPanel4.Invalidate();
this.FontPanel5.Invalidate();
this.FontPanel6.Invalidate();
this.FontPanel7.Invalidate();
}
private void GraphyPanel_Paint(object sender, PaintEventArgs e)
{
if (m_Bitmap != null)
{
Graphics g = e.Graphics;
//int p;
//p = m_Bitmap.Width * m_Bitmap.Height;
int height = this.GraphyPanel.Height;
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(0, height - 15));
g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(50, height - 15));
g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(100, height - 15));
g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(150, height - 15));
g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(200, height - 15));
g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(245, height - 15));//给直方图划定大概坐标
for (int j = 0; j < 256; j )
{
if (gl[j] > height)
gl[j] = height;
g.DrawLine(pen1, j, height - 20, j, height - gl[j] - 20);//绘制直方图
}
/* if (aline)
{
g.DrawLine(Pens.OrangeRed, xx, 0, xx, height);
}*/
}
else
{
Graphics g = this.GraphyPanel.CreateGraphics();
g.Clear(this.GraphyPanel.BackColor);
}
}
private void LocatedPanel_Paint(object sender, PaintEventArgs e)
{
if (c_Bitmap != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(c_Bitmap, 0, 0, this.LocatedPanel.Width, LocatedPanel.Height);
}
else
{
Graphics g = this.LocatedPanel.CreateGraphics();
g.Clear(this.LocatedPanel.BackColor);
}
//else
//{
// Graphics g = e.Graphics;
// g.DrawRectangle(new Pen(Color.Violet), 0, 0, LocatedPanel.Width, LocatedPanel.Height);
//}
}
private void ExtractPanel_Paint(object sender, PaintEventArgs e)
{
if (extract_Bitmap_two != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(extract_Bitmap_two, 0, 0, this.ExtractPanel.Width, ExtractPanel.Height);
}
else
{
Graphics g = this.ExtractPanel.CreateGraphics();
g.Clear(this.ExtractPanel.BackColor);
}
}
private void FontPanel1_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap0 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap0, 0, 0, FontPanel1.Width, FontPanel1.Height);
}
else
{
Graphics g = this.FontPanel1.CreateGraphics();
g.Clear(this.FontPanel1.BackColor);
}
}
private void FontPanel2_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap1 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap1, 0, 0, FontPanel2.Width, FontPanel2.Height);
}
else
{
Graphics g = this.FontPanel2.CreateGraphics();
g.Clear(this.FontPanel2.BackColor);
}
}
private void FontPanel3_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap2 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap2, 0, 0, FontPanel3.Width, FontPanel3.Height);
}
else
{
Graphics g = this.FontPanel3.CreateGraphics();
g.Clear(this.FontPanel3.BackColor);
}
}
private void FontPanel4_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap3 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap3, 0, 0, FontPanel4.Width, FontPanel4.Height);
}
else
{
Graphics g = this.FontPanel4.CreateGraphics();
g.Clear(this.FontPanel4.BackColor);
}
}
private void FontPanel5_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap4 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap4, 0, 0, FontPanel5.Width, FontPanel5.Height);
}
else
{
Graphics g = this.FontPanel5.CreateGraphics();
g.Clear(this.FontPanel5.BackColor);
}
}
private void FontPanel6_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap5 != null)
{
Graphics g = e.Graphics;
//PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
g.DrawImage(z_Bitmap5, 0, 0, FontPanel6.Width, FontPanel6.Height);
}
else
{
Graphics g = this.FontPanel6.CreateGraphics();
g.Clear(this.FontPanel6.BackColor);
}
}
private void FontPanel7_Paint(object sender, PaintEventArgs e)
{
if (z_Bitmap6 != null)
{
Graphics g = e.Graphics;
g.DrawImage(z_Bitmap6, 0, 0, FontPanel7.Width, FontPanel7.Height);
}
else
{
Graphics g = this.FontPanel7.CreateGraphics();
g.Clear(this.FontPanel7.BackColor);
}
}
public int TransformFiles(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
//DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles("*.bmp");
int i = 0, j = 0;
try
{
foreach (FileInfo f in files)
{
this.listBox1.Items.Add(dir f.ToString());
i ;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
if (path.Equals(charSourceBath))
{
this.charString = new string[i];
this.charDigitalString = new string[i];
try
{
foreach (FileInfo f in files)
{
charString[j] = (dir f.ToString());
charDigitalString[j] = Path.GetFileNameWithoutExtension(charString[j]);
//Console.WriteLine(charDigitalString[j]);
j ;
}
//Console.WriteLine(j);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
provinceString = new string[i];
provinceDigitalString = new string[i];
try
{
foreach (FileInfo f in files)
{
provinceString[j] = (dir f.ToString());
provinceDigitalString[j] = Path.GetFileNameWithoutExtension(provinceString[j]);
//Console.WriteLine(provinceString[j]);
j ;
}
// Console.WriteLine(j);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
return i;
}
private int minNumber(int[] num)//计算数组中的最小值索引
{
int minIndex = 0;
int minNum = 1000;
for (int i = 0; i < num.Length; i )
{
if (minNum > num[i])
{
minNum = num[i];
minIndex = i;
}
}
return minIndex;
}
private void MaptoCharReg()
{
int charBmpCount = this.TransformFiles(charSourceBath);
int provinceBmpCount = this.TransformFiles(provinceSourceBath);
int[] charMatch = new int[charBmpCount];//存储当前图片和资源库中图片比对后所得的像素不同的个数
int[] provinceMatch = new int[provinceBmpCount];
charFont = new Bitmap[charBmpCount];
provinceFont = new Bitmap[provinceBmpCount];//这两个数组存储的是资源库中的bitmap文件
//Console.WriteLine("一共有这么几个资源文件" (charBmpCount provinceBmpCount));
for (int i = 0; i < charBmpCount; i )
{
charMatch[i] = 0;
}
for (int i = 0; i < provinceBmpCount; i )
{
provinceMatch[i] = 0;
}
for (int i = 0; i < charBmpCount; i )
{
charFont[i] = (Bitmap)Bitmap.FromFile(charString[i], false);//使用该文件中的嵌入颜色管理信息,从指定的文件创建m_Bitmap
} //charString存储的是路径
for (int i = 0; i < provinceBmpCount; i )
{
provinceFont[i] = (Bitmap)Bitmap.FromFile(provinceString[i], false);//使用该文件中的嵌入颜色管理信息,从指定的文件创建m_Bitmap
}
int matchIndex = 0;//最终匹配索引
string[] digitalFont = new string[7];
unsafe
{
if (z_Bitmaptwo[0] != null)
{
BitmapData bmData = z_Bitmaptwo[0].LockBits(new Rectangle(0, 0, z_Bitmaptwo[0].Width, z_Bitmaptwo[0].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan = bmData.Scan0;
// byte* p = (byte*)(void*)Scan;
int nOffset = stride - z_Bitmaptwo[0].Width * 3;
int nWidth = z_Bitmaptwo[0].Width;
int nHeight = z_Bitmaptwo[0].Height;
int lv, lc = 30;
for (int i = 0; i < provinceBmpCount; i )
{
byte* p = (byte*)(void*)Scan;
BitmapData bmData1 = provinceFont[i].LockBits(new Rectangle(0, 0, provinceFont[i].Width, provinceFont[i].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride1 = bmData1.Stride;
System.IntPtr Scan1 = bmData1.Scan0;
byte* p1 = (byte*)(void*)Scan1;
int nOffset1 = stride1 - provinceFont[i].Width * 3;
int nWidth1 = provinceFont[i].Width;
int nHeight1 = provinceFont[i].Height;
int ccc0 = 0, ccc1 = 0;
lv = 0;//两个图片匹配不相同的地方
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if ((p[0] - p1[0]) != 0)
{
provinceMatch[i] ;
//Console.WriteLine(ccc0 );
}
//if (p[0] == 255)
//{ ccc0 ; }
//if (p[1] == 255)
//{ ccc1 ; }
p1 = 3;
p = 3;
}
p1 = nOffset;
p = nOffset;
}
//Console.WriteLine(provinceDigitalString[i] "不相同的像素数值" provinceMatch[i]);
//lv = lv Math.Abs(ccc0 - ccc1);
matchIndex = this.minNumber(provinceMatch);
digitalFont[0] = provinceDigitalString[matchIndex].Substring(0,1);//文件的名字和图片信息匹配,所以得到的文件名就是图片上的文字
provinceFont[i].UnlockBits(bmData1);
}
z_Bitmaptwo[0].UnlockBits(bmData);
}
if (z_Bitmaptwo[1] != null && z_Bitmaptwo[2] != null && z_Bitmaptwo[3] != null && z_Bitmaptwo[4] != null && z_Bitmaptwo[5] != null && z_Bitmaptwo[6] != null)
{
for (int j = 1; j < 7; j )
{
BitmapData bmData = z_Bitmaptwo[j].LockBits(new Rectangle(0, 0, z_Bitmaptwo[j].Width, z_Bitmaptwo[j].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan = bmData.Scan0;
// byte* p = (byte*)(void*)Scan;
int nOffset = stride - z_Bitmaptwo[j].Width * 3;
int nWidth = z_Bitmaptwo[j].Width;
int nHeight = z_Bitmaptwo[j].Height;
int lv, lc = 0;
//Console.WriteLine("======================================" j);
for (int i = 0; i < charBmpCount; i )
{
charMatch[i] = 0;
}
for (int i = 0; i < charBmpCount; i )
{
byte* p = (byte*)(void*)Scan;
BitmapData bmData1 = charFont[i].LockBits(new Rectangle(0, 0, charFont[i].Width, charFont[i].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride1 = bmData1.Stride;
System.IntPtr Scan1 = bmData1.Scan0;
byte* p1 = (byte*)(void*)Scan1;
int nOffset1 = stride1 - charFont[i].Width * 3;
int nWidth1 = charFont[i].Width;
int nHeight1 = charFont[i].Height;
int ccc0 = 0, ccc1 = 0;
lv = 0;
for (int y = 0; y < nHeight; y)
{
for (int x = 0; x < nWidth; x)
{
if ((p[0] - p1[0]) != 0)
{
charMatch[i] ;
// Console.WriteLine(ccc0 );
}
lv ;
p1 = 3;
p = 3;
}
p1 = nOffset;
p = nOffset;
}
// Console.WriteLine("图像尺寸" lv);
Console.WriteLine(charDigitalString[i] "数字中不相同的像素数值" charMatch[i]);
matchIndex = this.minNumber(charMatch);
digitalFont[j] = charDigitalString[matchIndex].Substring(0, 1);//截取文件名的第一个字符就行了
charFont[i].UnlockBits(bmData1);
}
z_Bitmaptwo[j].UnlockBits(bmData);
}
}
}
this.ResultLabel.Text = "" digitalFont[0] digitalFont[1] digitalFont[2] digitalFont[3] digitalFont[4] digitalFont[5] digitalFont[6];
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
}
private void 皮肤一ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "EmeraldColor1.ssk";
}
private void 皮肤二ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "EighteenColor2.ssk";
}
private void 皮肤三ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "GlassOrange.ssk";
}
private void 皮肤四ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "SteelBlack.ssk";
}
private void 皮肤五ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "Page.ssk";
}
private void 皮肤六ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "OneBlue.ssk";
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.文件_打开();
}
private void FontPanel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag=provinceSourceBath;
singleBitmap = z_Bitmaptwo[0];
SinglePanel.Invalidate();
}
private void FontPanel2_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[1];
SinglePanel.Invalidate();
}
private void FontPanel3_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[2];
SinglePanel.Invalidate();
}
private void FontPanel4_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[3];
SinglePanel.Invalidate();
}
private void FontPanel5_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[4];
SinglePanel.Invalidate();
}
private void FontPanel6_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[5];
SinglePanel.Invalidate();
}
private void FontPanel7_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.bathFlag = charSourceBath;
singleBitmap = z_Bitmaptwo[6];
SinglePanel.Invalidate();
}
private void SinglePanel_Paint(object sender, PaintEventArgs e)
{
if (singleBitmap != null)
{
Graphics g = e.Graphics;
g.DrawImage(singleBitmap, 0, 0, SinglePanel.Width, SinglePanel.Height);
}
else
{
Graphics g = this.SinglePanel.CreateGraphics();
g.Clear(this.SinglePanel.BackColor);
}
}
private void SingleButton_Click(object sender, EventArgs e)
{
String fontName = this.SingleTextBox.Text;
String formatName = null;
formatName = GeneralTools.getFormatName(bathFlag,fontName);
//Console.WriteLine(formatName " " bathFlag);
//this.thisBitmap.Save(licensePlateBath this.textBox1.Text ".bmp");
this.singleBitmap.Save(bathFlag formatName ".bmp");
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)