📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace zuobiao
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button2;
Bitmap image=null;
private System.Windows.Forms.Panel panel1;
CRectTracker m_tracker=null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
m_tracker=new CRectTracker();
m_tracker.m_nStyle = CRectTracker.StyleFlags.dottedLine|CRectTracker.StyleFlags.resizeInside;
m_tracker.m_rect.X = 10;
m_tracker.m_rect.Y = 10;
m_tracker.m_rect.Width = 91;
m_tracker.m_rect.Height = 91;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(8, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1280, 1024);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintHandler);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CReportView_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CReportView_MouseDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 1;
this.button1.Text = "图像读入";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(0, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 2;
this.button2.Text = "坐标计算";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(104, 40);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(900, 600);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 430);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 读入图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog a = new OpenFileDialog();
a.Filter = "Bmp File(*.bmp)|*.bmp";
if (a.ShowDialog() == DialogResult.OK)
{
image = new Bitmap(a.FileName);
pictureBox1.Image = image;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
circle(); //计算99个圆的圆心
MessageBox.Show("本次圆心提取结束!");
}
/// <summary>
/// 计算99个圆的圆心,最后数据存放在x,y数组中
/// </summary>
/// <returns></returns>
public unsafe bool circle()
{
int x1,y1,width,heigh;
x1=m_tracker.m_rect.X;
y1=m_tracker.m_rect.Y;
width=m_tracker.m_rect.Width;
heigh=m_tracker.m_rect.Height;
double [,]buff1=new double [image.Height,image.Width]; //用于存储读入图像的灰度值
BitmapData bmData1=image.LockBits(new Rectangle(0,0,image.Width,image.Height),ImageLockMode.ReadWrite,PixelFormat.Format8bppIndexed);
byte *lpByte0=(byte*)(bmData1.Scan0);
//把要处理的图像数据放到一个数组中
for(int i=0;i<image.Height;i++)
{
for(int j=0;j<image.Width;j++)
{
buff1[i,j]=lpByte0[0];
lpByte0++;
}
}
cord1.cord1classClass st=new cord1.cord1classClass ();
object m=null;
object n=null;
double [,] cooL=new double [102,2];//计算出来的圆的个数,本来是99个,设为102个的原因是怕噪声的干扰
double [] x=new double [100];//99个圆的x坐标
double [] y=new double [100];//99个圆的y坐标
st.compute_coord(2,ref m,ref n,buff1,x1,y1,width,heigh);
cooL=(double[,])m;
for(int i=1;i<=99;i++)//注意:这里存放x,y坐标的数组x[0],y[0]为0,这是由matlab的数组决定的
{
x[i]=cooL[i,1];
y[i]=cooL[i,2];
}
image.UnlockBits(bmData1);
return true;
}
private void PaintHandler(Object sender, PaintEventArgs e)
{
Graphics gs=e.Graphics;
m_tracker.OnDraw(gs);
}
private void CReportView_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons .Left)
{
Rectangle rectSave=new Rectangle();
Point pt=new Point(e.X,e.Y);
m_tracker.GetTrueRect(ref rectSave);
if (m_tracker.HitTest(pt) < 0)
{
// just to demonstrate CRectTracker::TrackRubberBand
CRectTracker tracker=new CRectTracker();
if (tracker.TrackRubberBand(this.pictureBox1, pt, false))
{
// see if rubber band intersects with the doc's tracker
tracker.NormalizeRect(ref tracker.m_rect); // so intersect rect works
if (tracker.m_rect.IntersectsWith(m_tracker.m_rect))
{
// if so, put resize handles on it (ie. select it)
if ((m_tracker.m_nStyle & CRectTracker.StyleFlags.resizeInside)!=0)
{
// swap from resize inside to resize outside for effect
m_tracker.m_nStyle &= ~CRectTracker.StyleFlags.resizeInside;
m_tracker.m_nStyle |= CRectTracker.StyleFlags.resizeOutside;
}
else
{
// just use inside resize handles on first time
m_tracker.m_nStyle &= ~CRectTracker.StyleFlags.resizeOutside;
m_tracker.m_nStyle |= CRectTracker.StyleFlags.resizeInside;
}
}
this.pictureBox1.Invalidate();
}
}
else if (m_tracker.Track(this.pictureBox1, pt, false,null))
{
this.pictureBox1.Invalidate();
}
}
}
private void CReportView_MouseMove(object sender, MouseEventArgs e)
{
Point mousept=new Point(e.X,e.Y);
if(!m_tracker.SetCursor(this.pictureBox1,0,mousept))
this.Cursor=Cursors.Arrow;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -