实例介绍
【实例简介】程序能读取一般JPG、BMP等格式的图像、在窗口中显示整幅图像并支持图像缩放、任意点坐标显示,图像中标注了特征点(十)和同名像点(十),并列表显示各同名像点在左片和右片的像素坐标位置
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Media; //添加引用 using System.IO; using System.Drawing.Imaging; namespace SRTP { public partial class Form1_导入_分类 : Form { public Form1_导入_分类() { InitializeComponent(); } int[,] tezhengzhi;//存储找到的特征点行、列、灰度、RGB int[,] tezheng; //存储找到的特征点的点号、行、列、灰度、RGB double[,] tezhengmuban; Bitmap bmpt; /// <summary> /// 定义一组图片变量 /// </summary> byte[] picturebytes; /// <summary> /// 点击按钮控件,执行图片导入功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> string filepath = "RGB.txt"; /// <summary> /// 将矩阵将结果集成到字符串 /// </summary> /// <param name="a"></param> /// <returns></returns> public static string matrix2str(int [,] a,int m,int n) { string ss=""; for (int i = 0; i < a.GetLength(0)/m; i = 4) { for (int j = 0; j < a.GetLength(1)/n; j = 4) { string str_temp = String.Format("{0,5}", a[i, j]); ss = str_temp; } ss = "\n"; } return ss; } /// <summary> /// 保存数据data到文件,返回值为保存的文件名 /// </summary> public static string SaveToFile(String FileName, string data) { StreamWriter write = new StreamWriter(FileName); string str_temp = String.Format("{0, 5}", data); write.Write(str_temp); write.WriteLine("\n"); write.Close(); return FileName; } private void Form1_导入_分类_Load(object sender, EventArgs e) { //添加背景音乐 System.Media.SoundPlayer music = new System.Media.SoundPlayer(System.IO.Directory.GetCurrentDirectory() "\\茶太.wav");//new SoundPlayer(System.IO.Directory.GetCurrentDirectory() "\\茶太.wav"); music.PlayLooping(); //鼠标滚轮控制图像缩放 pictureBox1.MouseWheel = new MouseEventHandler(pictureBox1_MouseWheel); pictureBox2.MouseWheel = new MouseEventHandler(pictureBox2_MouseWheel); } //鼠标滚轮控制图像缩放 void pictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Delta > 0) MessageBox.Show("滚动事件已被捕捉---图片已放大!"); else MessageBox.Show("滚动事件已被捕捉---图片已缩小!"); System.Drawing.Size t = pictureBox1.Size; t.Width = e.Delta * 2; t.Height = e.Delta * 2; pictureBox1.Width = t.Width; pictureBox1.Height = t.Height; } void pictureBox2_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Delta > 0) MessageBox.Show("滚动事件已被捕捉---图片已放大!"); else MessageBox.Show("滚动事件已被捕捉---图片已缩小!"); System.Drawing.Size t = pictureBox1.Size; t.Width = e.Delta * 2; t.Height = e.Delta * 2; pictureBox2.Width = t.Width; pictureBox2.Height = t.Height; } private void comboBox1_MouseEnter(object sender, EventArgs e) { this.pictureBox1.Focus(); } private void 导入图片ToolStripMenuItem_Click(object sender, EventArgs e) { this.label1.Visible = true; this.panel1.Visible = true; this.richTextBox1.Visible = false; this.pictureBox1.Visible = true; //获取打开文件返回值 if (openFileDialog1.ShowDialog() == DialogResult.OK) { string fullpath = openFileDialog1.FileName;//文件路径 FileStream fs = new FileStream(fullpath, FileMode.Open);//文件指针,io流 picturebytes = new byte[fs.Length]; BinaryReader br = new BinaryReader(fs);//将元素表示为二进制 picturebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//基本数据类型转换 MemoryStream ms = new MemoryStream(picturebytes);//基于指定的字节数组初始化 bmpt = new Bitmap(ms);//图片路径 pictureBox1.Image = bmpt; this.toolStripSplitButton2.Enabled = true; } else { MessageBox.Show("图片打开失败!"); } } private void 保存为文本ToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "(*.txt)|*.txt|" "(*.*)|*.*"; string str = this.richTextBox1.Text; if (saveDlg.ShowDialog() == DialogResult.OK) { this.richTextBox1.SaveFile(saveDlg.FileName, RichTextBoxStreamType.PlainText);//保存文本 // 提示用户:文件保存的位置和文件名 MessageBox.Show("文件已成功保存到" saveDlg.FileName); } else MessageBox.Show("文件保存失败!"); } private void 图片另存为ToolStripMenuItem_Click(object sender, EventArgs e) { if (bmpt == null) { MessageBox.Show("请先导入图片!", "错误提示"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { pictureBox1.Image.Save(sfd.FileName); MessageBox.Show("图片成功保存到" sfd.FileName "!", "提示"); sfd.Dispose(); } } private void 转到图片ToolStripMenuItem_Click(object sender, EventArgs e) { this.richTextBox1.Visible = false; this.pictureBox1.Visible = true; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 提取特征点ToolStripMenuItem_Click_1(object sender, EventArgs e) { /* FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); fs.Close(); br.Close(); double scale = 0.1; Image orginalimg, originalIMG; using (Stream picStream = new MemoryStream(bytes)) { orginalimg = (Bitmap)Image.FromStream(picStream); } originalIMG = orginalimg.GetThumbnailImage(Convert.ToInt32(orginalimg.Size.Width * scale), Convert.ToInt32(orginalimg.Size.Height * scale), null, IntPtr.Zero); */ Image myImage = (Image)bmpt; //Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);//从指定的文件创建image pictureBox1.Image = myImage; //显示打开的图片 this.dataGridView1.Visible = true; this.groupBox1.Visible = true; toolStripProgressBar1.Visible = true; //进度条可视 toolStripProgressBar1.Maximum = 7; //设置进度条最大长度值 toolStripProgressBar1.Value = 0; //设置进度条当前值 toolStripProgressBar1.Step = 1; //设置进度条步长 toolStripProgressBar1.Value = toolStripProgressBar1.Step; //进度条前进 int Var_H = pictureBox1.Image.Height; //获取图象的高度 int Var_W = pictureBox1.Image.Width; //获取图象的宽度 Bitmap Var_bmp = (Bitmap)pictureBox1.Image; //根据图象的大小创建Bitmap对象 double[,] huiduzhi = new double[Var_W, Var_H]; //用于存储各点灰度值 for (int i = 0; i < Var_W; i ) { for (int j = 0; j < Var_H; j ) { Color tem_color = Var_bmp.GetPixel(i, j); //获取当前像素的颜色值 huiduzhi[i, j] = tem_color.R * 0.299 tem_color.G * 0.587 tem_color.B * 0.114; //各点灰度值 } } toolStripProgressBar1.Value = toolStripProgressBar1.Step; double[,] xingquzhi = new double[Var_W, Var_H]; //用于存储各点兴趣值 for (int i = 2; i < Var_W - 2; i ) { for (int j = 2; j < Var_H - 2; j ) { double V1 = 0; for (int m = 0; m < 4; m ) { V1 = V1 Math.Pow(huiduzhi[i - 2 m, j] - huiduzhi[i - 1 m, j], 2); //计算V1方向相邻像素灰度差平方和 } double V2 = 0; for (int m = 0; m < 4; m ) { V2 = V2 Math.Pow(huiduzhi[i - 2 m, j - 2 m] - huiduzhi[i - 1 m, j - 1 m], 2); //计算V2方向相邻像素灰度差平方和 } double V3 = 0; for (int m = 0; m < 4; m ) { V3 = V3 Math.Pow(huiduzhi[i, j - 2 m] - huiduzhi[i, j - 1 m], 2); //计算V3方向相邻像素灰度差平方和 } double V4 = 0; for (int m = 0; m < 4; m ) { V4 = V4 Math.Pow(huiduzhi[i - 2 m, j 2 - m] - huiduzhi[i - 1 m, j 1 - m], 2); //计算V4方向相邻像素灰度差平方和 } xingquzhi[i, j] = Math.Min(Math.Min(Math.Min(V1, V2), V3), V4); //从V1、V2、V3、V4中取最小值作为该点兴趣值 } } toolStripProgressBar1.Value = toolStripProgressBar1.Step; double sum = 0; for (int i = 0; i < Var_W; i ) { for (int j = 0; j < Var_H; j ) { sum = xingquzhi[i, j]; } } double pingjunzhi = sum / (Var_W * Var_H); //兴趣点的平均值 toolStripProgressBar1.Value = toolStripProgressBar1.Step; int houxuan = 0; //统计候选特征点数目 int c = 101; //获取准确特征点数目 double zeng = 4; while (c > 100) { c = 0; double yuzhi = pingjunzhi * zeng; //double yuzhi = pingjunzhi * zeng; //设定阈值 double[,] jianding = new double[Var_W, Var_H]; for (int i = 0; i < Var_W; i ) { for (int j = 0; j < Var_H; j ) { if (xingquzhi[i, j] <= yuzhi) { jianding[i, j] = 0; //选取兴趣值大于阈值的点作为特征候选点,其他点兴趣值归零 } else { jianding[i, j] = 1; houxuan ; } } } int[,] tezhengzhi1 = new int[houxuan, 7]; //建立一个数组能容纳所有点皆为特征点的像素坐标矩阵 int yuzhimuban = 5; //定义阈值模板 int mubanbanchuang = 2; for (int i = mubanbanchuang; i < Var_W - mubanbanchuang; i = i yuzhimuban) { for (int j = mubanbanchuang; j < Var_H - mubanbanchuang; j = j yuzhimuban) { double MAX = 0; //假定5*5模板最大值起始值为第一个元素值 int a = 0; //设a为最大值行 int b = 0; //设b为最大值列 for (int m = 0; m < yuzhimuban; m ) { for (int n = 0; n < yuzhimuban; n ) { if (jianding[i - mubanbanchuang m, j - mubanbanchuang n] == 1) { if (MAX < xingquzhi[i - mubanbanchuang m, j - mubanbanchuang n]) { MAX = xingquzhi[i - mubanbanchuang m, j - mubanbanchuang n]; //获取5*5模板中最大值 a = i - mubanbanchuang m; //获取最大值列 b = j - mubanbanchuang n; //获取最大值行 } } else { a = 0; b = 0; } } } if ((a != 0) && (b != 0)) { tezhengzhi1[c, 0] = 0; tezhengzhi1[c, 1] = a; //存储特征点列 tezhengzhi1[c, 2] = b; //存储特征点行 Color tem_color = Var_bmp.GetPixel(a, b); //获取当前像素的颜色值 tezhengzhi1[c, 3] = Convert.ToInt16(tem_color.R * 0.299 tem_color.G * 0.587 tem_color.B * 0.114); tezhengzhi1[c, 4] = Var_bmp.GetPixel(a, b).R; tezhengzhi1[c, 5] = Var_bmp.GetPixel(a, b).G; tezhengzhi1[c, 6] = Var_bmp.GetPixel(a, b).B; c ; //每有一个既不为0也不重复的最大值特征点数目加一 } } } tezhengzhi = tezhengzhi1; zeng = 1; } toolStripProgressBar1.Value = toolStripProgressBar1.Step; toolStripProgressBar1.Value = toolStripProgressBar1.Step; int[,] tezhengdian2 = new int[c, 7]; //定义一个数组存储特征点信息 for (int i = 0; i < c; i ) { tezhengdian2[i, 0] = i 1; for (int j = 1; j < 7; j )//for (int j = 0; j < 2; j ) { tezhengdian2[i, j] = tezhengzhi[i, j]; } } tezheng = tezhengdian2; Image img = pictureBox1.Image; //将pictureBox1中图像存储入另一个变量 Bitmap bmp = new Bitmap(img.Width, img.Height); //创建Bitmap对象 Graphics g = Graphics.FromImage(bmp); //创建Graphics对象 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量双三次插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //设置绘制到此 Graphics 的合成图像的呈现质量 g.DrawImage(img, 0, 0, img.Width, img.Height); //以img为原本重新于(0,0)点绘制 g.Dispose(); //释放资源 for (int i = 0; i < c; i ) //c为特征候选点数目 { Graphics myGraphics = Graphics.FromImage(bmp); //创建Graphics对象 myGraphics.DrawLine(new Pen(Color.Red, 1), new Point(tezhengdian2[i, 1], tezhengdian2[i, 2] - 5), new Point(tezhengdian2[i, 1], tezhengdian2[i, 2] 5)); //画出竖直方向直线 myGraphics.DrawLine(new Pen(Color.Red, 1), new Point(tezhengdian2[i, 1] - 5, tezhengdian2[i, 2]), new Point(tezhengdian2[i, 1] 5, tezhengdian2[i, 2])); //画出水平方向直线 myGraphics.DrawString((i 1).ToString(), new Font("宋体", 10), Brushes.Yellow, new System.Drawing.Point(tezhengdian2[i, 1], tezhengdian2[i, 2] 5)); //标记特征点号 myGraphics.Dispose(); //释放资源 pictureBox1.Image = bmp; //显示含有“ ”的图 } toolStripProgressBar1.Value = toolStripProgressBar1.Step; //新建一个datatable用于保存读入的数据 DataTable dt = new DataTable(); //给datatable添加7个列 dt.Columns.Add("点号", typeof(int)); dt.Columns.Add("列", typeof(String)); dt.Columns.Add("行", typeof(String)); dt.Columns.Add("灰度值", typeof(int)); dt.Columns.Add("R", typeof(int)); dt.Columns.Add("G", typeof(int)); dt.Columns.Add("B", typeof(int)); for (int i = 0; i < c; i ) { DataRow dr = dt.NewRow(); for (int h = 0; h < 7; h ) { dr[h] = tezhengdian2[i, h]; } dt.Rows.Add(dr); //将这行数据加入到datatable中 } this.dataGridView1.DataSource = dt; foreach (DataGridViewColumn item in dataGridView1.Columns) { item.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; }//将datatable绑定到datagridview上显示结果 dataGridView1.AllowUserToAddRows = false; groupBox1.Text = "特征点像素坐标" "(" c.ToString() "个" ")"; toolStripProgressBar1.Visible = false; //隐藏进度条 MessageBox.Show("共有" c.ToString() "个特征点!"); double[,] tezhengmuban2 = new double[c, 25]; for (int i = 0; i < c; i ) { int t = 0; for (int m = 0; m < 5; m ) { for (int n = 0; n < 5; n ) { tezhengmuban2[i, t] = huiduzhi[tezhengdian2[i, 1] - 2 m, tezhengdian2[i, 2] - 2 n]; //存储5*5模板灰度 t ; } } } tezhengmuban = tezhengmuban2; } /// <summary> /// 为表格添加行号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { try { //添加行号 SolidBrush v_SolidBrush = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor); int v_LineNo = 0; v_LineNo = e.RowIndex 1; string v_Line = v_LineNo.ToString(); e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X 15, e.RowBounds.Location.Y 5); } catch (Exception ex) { MessageBox.Show("添加行号时发生错误,错误信息:" ex.Message, "操作失败"); } } private void 自适应显示ToolStripMenuItem_Click(object sender, EventArgs e) { this.richTextBox1.Visible = true; this.dataGridView1.Visible = false; if (this .pictureBox1 .Image == null) { MessageBox.Show("请先导入图片!"); return; } // int[,] alp = new int[bmpt.Width, bmpt.Height]; int[,] red = new int[bmpt.Width, bmpt.Height]; int[,] gre = new int[bmpt.Width, bmpt.Height]; int[,] blu = new int[bmpt.Width, bmpt.Height]; int[,] gy = new int[bmpt.Width, bmpt.Height]; if (this.pictureBox1.Image!= null)//bmpt { Color curColor = new Color(); int gray; for (int i = 0; i < bmpt.Width; i ) { for (int j = 0; j < bmpt.Height; j ) { curColor = bmpt.GetPixel(i, j); gray = (int)(0.299 * curColor.R 0.587 * curColor.G * 0.114 * curColor.B); bmpt.SetPixel(i, j, curColor); //alp[i, j] = curColor.A; gy[i, j] = gray; red[i, j] = curColor.R; gre[i, j] = curColor.G; blu[i, j] = curColor.B; } } int m = 0; int n = 0; if (bmpt.Width >= 0 && bmpt.Width < 100) m = 5; if (bmpt.Width >= 100 && bmpt.Width < 1000) m = 10; if (bmpt.Width >= 1000) m = (int)Math.Sqrt(bmpt.Width); if (bmpt.Height >= 0 && bmpt.Height < 100) n = 5; if (bmpt.Height >= 100 && bmpt.Height < 1000) n = 10; if (bmpt.Height >= 1000) n = (int)Math.Sqrt(bmpt.Height); string result = "图片大小为" Convert.ToString(bmpt.Width) "x" 4 * bmpt.Height; string allstr = result; result = "\n仅显示宽像素的前1/" Convert.ToString(m) "、高像素的前1/" 4 * n "信息!"; string str = "\r\n该图片的 灰度 分量:\r\n"; result = str; allstr = str; result = matrix2str(gy, m, n); allstr = matrix2str(gy,1, 1);//allstr = matrix2str(gy, m / 2, n / 2); str = "\r\n该图片的 R 分量:\r\n"; result = str; allstr = str; str = matrix2str(red, m, n); allstr = matrix2str(red, 1, 1);//m / 2, n / 2); result = str; str = "\r\n该图片的 G 分量:\r\n"; result = str; allstr = str; result = matrix2str(gre, m, n); allstr = matrix2str(gre, 1, 1); //m / 2, n / 2); str = "\r\n该图片的 B 分量:\r\n"; result = str; allstr = str; //string allstr4 = str; result = matrix2str(blu, m, n); allstr = matrix2str(blu, 1, 1); //m / 2, n / 2); SaveToFile(filepath, allstr);//B值保存到文本 //richtextbox读取文本并显示 this.richTextBox1.Text = result; } } private void 完整信息ToolStripMenuItem_Click(object sender, EventArgs e) { const string FileName = @"RGB.txt"; try { using (StreamReader m_streamReader = new StreamReader(FileName)) { this.richTextBox1.Text = m_streamReader.ReadToEnd(); m_streamReader.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void 导入匹配影像ToolStripMenuItem_Click(object sender, EventArgs e) { this.panel2.Visible = true; this.pictureBox2.Visible = true; //设置文件的类型 openFileDialog2.Filter = "*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf"; if (openFileDialog2.ShowDialog() == DialogResult.OK) //打开文件对话框 { //根据文件的路径创建Image对象 Image myImage2 = System.Drawing.Image.FromFile(openFileDialog2.FileName); pictureBox2.Image = myImage2; //显示打开的图片 this.影像匹配ToolStripMenuItem.Enabled = true; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { toolStripStatusLabel2.Text = e.X.ToString();//PointToClient toolStripStatusLabel4.Text = e.Y.ToString(); //toolStripStatusLabel4.Text = (e.Y - PointToScreen(pictureBox1.Location).Y).ToString(); } private void pictureBox1_MouseEnter(object sender, EventArgs e) { this.pictureBox1.Focus(); } private void pictureBox2_MouseMove(object sender, MouseEventArgs e) { try { toolStripStatusLabel2.Text = e.X.ToString(); //显示X坐标 toolStripStatusLabel4.Text = e.Y.ToString(); //显示y坐标 } catch { } } private void pictureBox2_MouseEnter(object sender, EventArgs e) { this.pictureBox2.Focus(); } /// <summary> /// 影像匹配 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 影像匹配ToolStripMenuItem_Click(object sender, EventArgs e) { this.groupBox2.Visible = true; this.dataGridView2.Visible = true; Image myImage2 = System.Drawing.Image.FromFile(openFileDialog2.FileName); pictureBox2.Image = myImage2; //显示打开的图片 this.panel2.Visible = true; this.dataGridView2.Visible = true; toolStripProgressBar1.Visible = true; //进度条可视 toolStripProgressBar1.Maximum = 4; //设置进度条最大长度值 toolStripProgressBar1.Value = 0; //设置进度条当前值 toolStripProgressBar1.Step = 1; //设置进度条步长 toolStripProgressBar1.Value = toolStripProgressBar1.Step; //进度条前进 int Var_H = pictureBox2.Image.Height; //获取图象的高度 int Var_W = pictureBox2.Image.Width; //获取图象的宽度 Bitmap Var_bmp = (Bitmap)pictureBox2.Image; //根据图象的大小创建Bitmap对象 double[,] huiduzhi = new double[Var_W, Var_H]; //用于存储各点灰度值 for (int i = 0; i < Var_W; i ) { for (int j = 0; j < Var_H; j ) { Color tem_color = Var_bmp.GetPixel(i, j); //获取当前像素的颜色值 huiduzhi[i, j] = tem_color.R * 0.299 tem_color.G * 0.587 tem_color.B * 0.114; //各点灰度值 } } if (tezheng == null) { MessageBox.Show("请先进行特征点提取!之后执行影像匹配操作!"); } toolStripProgressBar1.Value = toolStripProgressBar1.Step; double[,] zuobiao = new double[tezheng.GetLength(0), 5]; //存储特征点对应坐标与系数 for (int i = 0; i < zuobiao.GetLength(0); i ) { zuobiao[i, 0] = tezheng[i, 1];// zuobiao[i, 0] = tezheng[i, 0];//存储行号 zuobiao[i, 1] = tezheng[i, 2];// zuobiao[i, 1] = tezheng[i, 1];//存储列号 } int dianshu = 0; //存储相关系数大于0.99的点数 for (int c = 0; c < tezhengmuban.GetLength(0); c ) { double maxxishu = 0; //最大系数 int a = 0; //存储系数最大点列 int b = 0; //存储系数最大点行 for (int i = 2; i < Var_W - 2; i ) { for (int j = 2; j < Var_H - 2; j ) { double chenghe = 0; //各点灰度值相乘总和 int t = 0; for (int m = 0; m < 5; m ) { for (int n = 0; n < 5; n ) { chenghe = tezhengmuban[c, t] * huiduzhi[i - 2 m, j - 2 n];//模板按列相乘 t ; } } double tezhenghe = 0; //特征点灰度值和 double tezhengji = 0; //特征点灰度值平方和 for (int m = 0; m < 25; m ) { tezhenghe = tezhengmuban[c, m]; tezhengji = Math.Pow(tezhengmuban[c, m], 2); } double xiangsuhe = 0; //像素点灰度值和 double xiangsuji = 0; //像素点灰度值平方和 for (int m = 0; m < 5; m ) { for (int n = 0; n < 5; n ) { xiangsuhe = huiduzhi[i - 2 m, j - 2 n]; xiangsuji = Math.Pow(huiduzhi[i - 2 m, j - 2 n], 2); } } double xishu = 0; xishu = (chenghe - tezhenghe * xiangsuhe / 25) / Math.Sqrt((tezhengji - Math.Pow(tezhenghe, 2) / 25) * (xiangsuji - Math.Pow(xiangsuhe, 2) / 25)); if (maxxishu < xishu) { maxxishu = xishu; a = i; b = j; } } } if ((maxxishu > 0.66) && (maxxishu <= 1))//取最大的相关系数 { zuobiao[c, 2] = a; zuobiao[c, 3] = b; zuobiao[c, 4] = maxxishu; dianshu ; } } toolStripProgressBar1.Value = toolStripProgressBar1.Step; double[,] zuobiao2 = new double[dianshu, 5]; //明确存储特征点对应坐标与相关系数 int de = 0; for (int i = 0; i < zuobiao.GetLength(0); i ) { if (zuobiao[i, 4] != 0) { for (int j = 0; j < 5; j ) { zuobiao2[de, j] = zuobiao[i, j]; } de ; } } Image img = pictureBox2.Image; //将pictureBox2中图像存储入另一个变量 Bitmap bmp = new Bitmap(img.Width, img.Height); //创建Bitmap对象 Graphics g = Graphics.FromImage(bmp); //创建Graphics对象 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量双三次插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //设置绘制到此 Graphics 的合成图像的呈现质量 g.DrawImage(img, 0, 0, img.Width, img.Height); //以img为原本重新于(0,0)点绘制 g.Dispose(); //释放资源 for (int i = 0; i < dianshu; i ) { Graphics myGraphics = Graphics.FromImage(bmp); //创建Graphics对象 myGraphics.DrawLine(new Pen(Color.Red, 1), new Point(Convert.ToInt32(zuobiao2[i, 2]), Convert.ToInt32(zuobiao2[i, 3] - 5)), new Point(Convert.ToInt32(zuobiao2[i, 2]), Convert.ToInt32(zuobiao2[i, 3] 5))); //画出竖直方向直线 myGraphics.DrawLine(new Pen(Color.Red, 1), new Point(Convert.ToInt32(zuobiao2[i, 2] - 5), Convert.ToInt32(zuobiao2[i, 3])), new Point(Convert.ToInt32(zuobiao2[i, 2]) 5, Convert.ToInt32(zuobiao2[i, 3]))); //画出水平方向直线 myGraphics.DrawString(i.ToString(), new Font("宋体", 10), new SolidBrush(Color.Yellow), Convert.ToInt32(zuobiao2[i, 2]), Convert.ToInt32(zuobiao2[i, 3])); myGraphics.Dispose(); //释放资源 pictureBox2.Image = bmp; //显示含有“ ”的图 } toolStripProgressBar1.Value = toolStripProgressBar1.Step; //新建一个datatable用于保存读入的数据 DataTable dt2 = new DataTable(); //给datatable添加二个列 dt2.Columns.Add("列(左)", typeof(String)); dt2.Columns.Add("行(左)", typeof(String)); dt2.Columns.Add("列(右)", typeof(String)); dt2.Columns.Add("行(右)", typeof(String)); dt2.Columns.Add("相关系数", typeof(String)); for (int i = 0; i < zuobiao2.GetLength(0); i ) { DataRow dr2 = dt2.NewRow(); dr2[0] = zuobiao2[i, 0]; //将列(左)数据赋给表 dr2[1] = zuobiao2[i, 1]; //将行(左)数据赋给表 dr2[2] = zuobiao2[i, 2]; dr2[3] = zuobiao2[i, 3]; dr2[4] = zuobiao2[i, 4].ToString("F6"); dt2.Rows.Add(dr2); //将这行数据加入到datatable中 } this.dataGridView2.DataSource = dt2; //将datatable绑定到datagridview上显示结果 dataGridView2.AllowUserToAddRows = false; groupBox1.Text = groupBox1.Text "(" dianshu.ToString() "个" ")"; toolStripProgressBar1.Visible = false; //隐藏进度条 MessageBox.Show("影像匹配点坐标有" Convert.ToString(dianshu) "个!"); } /// <summary> /// 为表格添加行号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { try { //添加行号 SolidBrush v_SolidBrush = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor); int v_LineNo = 0; v_LineNo = e.RowIndex 1; string v_Line = v_LineNo.ToString(); e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X 15, e.RowBounds.Location.Y 5); } catch (Exception ex) { MessageBox.Show("添加行号时发生错误,错误信息:" ex.Message, "操作失败"); } } private void 保存特征点图片ToolStripMenuItem_Click(object sender, EventArgs e) { if (tezhengzhi == null) { MessageBox.Show("您还未提取特征点!", "错误提示"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { pictureBox1.Image.Save(sfd.FileName); MessageBox.Show("图片成功保存到" sfd.FileName "!", "提示"); sfd.Dispose(); } } private void 保存图片ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.dataGridView2.DataSource == null) { MessageBox.Show("您还未提取特征点!", "错误提示"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { pictureBox2.Image.Save(sfd.FileName); MessageBox.Show("图片成功保存到" sfd.FileName "!", "提示"); sfd.Dispose(); } } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)