private void pictureBox_MouseDown(object sender, MouseEventArgs e) { // fm.Refresh(); Point pt = new Point(e.X, e.Y); List<featherRgnstruct> backupfeathureRegion = new List<featherRgnstruct>(); if (e.Button == MouseButtons.Left && isSreenShot) { startpoint = pt; startdraw = true; //isDraw = true; } else if (e.Button == MouseButtons.Left && isaddFeather) { Rectangle rec = new Rectangle(pt.X, pt.Y, featherSize.Width, featherSize.Height); featherRgnstruct tmpfeather = new featherRgnstruct(); tmpfeather.featherNum = defaultValue; tmpfeather.rec = rec; Graphics g = (sender as PictureBox).CreateGraphics(); if (FeatherdataGrid.Rows.Count < 31) { g.DrawRectangle(Pens.Blue, new Rectangle(rec.X - (featherSize.Width-1) / 2 , rec.Y - (featherSize.Height-1) / 2 , featherSize.Width, featherSize.Height)); showfeature.Add(tmpfeather); tmpfeather.rec.X = (int)(tmpfeather.rec.X * Math.Pow(2, -SizeChangecount)); tmpfeather.rec.Y = (int)(tmpfeather.rec.Y * Math.Pow(2, -SizeChangecount)); FeatherdataGrid.Rows.Add(tmpfeather.rec.X, tmpfeather.rec.Y, tmpfeather.featherNum); featherRegion.Add(tmpfeather); } else { //特征点上限不超过30 MessageBox.Show("绘制的特征点数超过限制"); } } else if (e.Button == MouseButtons.Left && isMeasureLen) { startMeasure = true; measureStartPoint = new Point((int)(e.X*Math.Pow(2,-SizeChangecount )), (int)(e.Y*Math.Pow(2,-SizeChangecount ))); } else if (e.Button == MouseButtons.Right)//&&!isaddFeather&&!isSreenShot) { } }
private void AutoFeatherButton_Click(object sender, EventArgs e) { BinaryTool.Enabled = true; PicFromCamera.Enabled = true; PicFromLocality.Enabled = true; CutImage2.Enabled = true; DeleteFeather.Enabled = true; MainUndobutton.Enabled = true; CancleCut.Enabled = true; OpenFeather.Enabled = true; AddPoint.Enabled = true; if (int.Parse(AutoFeathertextBox1.Text) > 20) { AutoFeathertextBox1.Text = "20"; } else if (int.Parse(AutoFeathertextBox1.Text) <= 0) { AutoFeathertextBox1.Text = "0"; } defaultRanNum = int.Parse(AutoFeathertextBox1.Text); featherRegion.Clear(); FeatherdataGrid.Rows.Clear(); showfeature.Clear(); //清空数据 SizeChangecount = 0; //将图片变为原始大小 if (bmp != null) { picturebox.Size = bmp.Size; if (Mainpanel.Width > bmp.Width && Mainpanel.Height > bmp.Height) { picturebox.Location = new Point((Mainpanel.Width - bmp.Size.Width) / 2, (Mainpanel.Height - bmp.Size.Height) / 2); } else if (Mainpanel.Width > bmp.Width) { picturebox.Location = new Point((Mainpanel.Width - bmp.Size.Width) / 2, 0); } else if (Mainpanel.Height > bmp.Height) { picturebox.Location = new Point(0, (Mainpanel.Height - bmp.Size.Height) / 2); } else { picturebox.Location = new Point(0, 0); } } switch (AutoAddFeathercomboBox.Text) { case "genetic algorithm": { ConfigGen configgen = new ConfigGen(savePath,programPath); //configgen.Show(); if (configgen.ShowDialog() == DialogResult.OK) { try { string[] data = configgen.data; // string DebugPath = StringOperateHelp.LeftOfRightmostOf(savePath, '\\'); if (File.Exists(programPath +PublicVariable.NegativePointsFileName)) { FileStream fs = new FileStream(programPath + PublicVariable.NegativePointsFileName, FileMode.Open); StreamReader streamreader = new StreamReader(fs); data = streamreader.ReadLine().Split(','); for (int i = 0; i < pointNum; i++) { featherRgnstruct tmp = new featherRgnstruct(); tmp.rec = new Rectangle(int.Parse(data[i * 3+1])+leftX, int.Parse(data[i * 3])+leftY, 3, 3); tmp.featherNum = int.Parse(data[i * 3+2]); featherRegion.Add(tmp); showfeature.Add(tmp); FeatherdataGrid.Rows.Add(tmp.rec.X,tmp.rec.Y,tmp.featherNum); } picturebox.Invalidate(); } } catch { } } picturebox.Invalidate(); } break; case "random": { Random random = new Random(); for (int i = 0; i < defaultRanNum; i++) { featherRgnstruct tmp = new featherRgnstruct(); tmp.rec = new Rectangle(random.Next(rightX-leftX)+leftX, random.Next(rightY-leftY)+leftY, 3, 3); tmp.featherNum = 1; featherRegion.Add(tmp); showfeature.Add(tmp); FeatherdataGrid.Rows.Add(tmp.rec.X, tmp.rec.Y, 1); } picturebox.Invalidate(); } break; } BasegroupBox.Controls.Clear(); BasegroupBox.Text = null; }
//当改变表格内数据时 private void FeatherdataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) { int flag = 0; int feathernum = featherRegion.Count; featherRegion.Clear(); showfeature.Clear(); while (flag < feathernum) { featherRgnstruct tmp = new featherRgnstruct(); int pointX = int.Parse(FeatherdataGrid.Rows[flag].Cells[0].Value.ToString()); int pointY = int.Parse(FeatherdataGrid.Rows[flag].Cells[1].Value.ToString()); tmp.rec = new Rectangle(pointX, pointY, 3, 3); tmp.featherNum = int.Parse(FeatherdataGrid.Rows[flag].Cells[2].Value.ToString()); featherRegion.Add(tmp); tmp.rec=new Rectangle((int)(pointX*Math.Pow(2,SizeChangecount)),(int)(pointY*Math.Pow(2,SizeChangecount)),3,3); showfeature.Add(tmp); flag++; } picturebox.Invalidate(); }
//打开feather.h文档 private void Openfeather() { try { string streamreader = File.ReadAllText(savePath + "\\" + "feather.h"); string dealtdata = CommonOperation.StringOperateHelp.RightOf(streamreader, '{'); dealtdata = CommonOperation.StringOperateHelp.LeftOf(dealtdata, '}'); string[] featherdata = dealtdata.Split(','); for (int i = 0; i < 1038; i++) { OriginalData[i] = int.Parse(featherdata[i]); } } catch { } leftX = OriginalData[11]; leftY = OriginalData[10]; rightX = OriginalData[13]; rightY = OriginalData[12]; DrawBorder = true; //显示边框 for (int i = 0; i < 255; i += 4) { if (OriginalData[i + 14] != 0 || OriginalData[i + 15] != 0 || OriginalData[i + 16] != 0 || OriginalData[i + 17] != 0) { featherRgnstruct featherstruct = new featherRgnstruct(); featherstruct.rec = new Rectangle(OriginalData[i + 15], OriginalData[i + 14], 3, 3); featherstruct.featherNum = OriginalData[i + 17]; featherRegion.Add(featherstruct); FeatherdataGrid.Rows.Add(featherstruct.rec.X, featherstruct.rec.Y, featherstruct.featherNum); showfeature.Add(featherstruct); } else { break; } } picturebox.Invalidate(); }
//右键表格删除当前行 private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { try { featherRgnstruct flagfeather = new featherRgnstruct(); flagfeather.rec = new Rectangle((int)(int.Parse(FeatherdataGrid.CurrentRow.Cells[0].Value.ToString())), int.Parse(FeatherdataGrid.CurrentRow.Cells[1].Value.ToString()), 3, 3); flagfeather.featherNum = int.Parse(FeatherdataGrid.CurrentRow.Cells[2].Value.ToString()); List<featherRgnstruct> tmpfeatherList = new List<featherRgnstruct>(); foreach (featherRgnstruct tmp in showfeature) { if (flagfeather.Equals(tmp)) { } else { tmpfeatherList.Add(tmp); } } showfeature = tmpfeatherList; List<featherRgnstruct> tmpfeatherList2 = new List<featherRgnstruct>(); foreach (featherRgnstruct tmp in featherRegion) { featherRgnstruct numberborder = new featherRgnstruct(); numberborder.rec = new Rectangle((int)(tmp.rec.X * Math.Pow(2, SizeChangecount)), (int)(tmp.rec.Y * Math.Pow(2, SizeChangecount)),3,3); numberborder.featherNum = tmp.featherNum; if (flagfeather.Equals(numberborder)) { } else { tmpfeatherList2.Add(tmp); } } featherRegion = tmpfeatherList2; showfeature = tmpfeatherList; FeatherdataGrid.Rows.Remove(FeatherdataGrid.CurrentRow); picturebox.Invalidate(); } catch (Exception ex) { } }
private void BinaryTool_Click(object sender, EventArgs e) { BasegroupBox.Controls.Clear(); List<Bitmap> threshodbmp = new List<Bitmap>(); foreach (Bitmap tmp in btList) { int threshod = AutoThroshod(tmp); threshodbmp.Add(BinaryImage(threshod,tmp)); } PasteNumber(threshodbmp); //picturebox.Image = BinaryImage(threshod, (Bitmap)picturebox.Image); BinaryTool.Enabled = false; NextStep.Enabled = true; AddFeather.Enabled = true; AutoFeather.Enabled = true; showfeature.Clear(); featherRegion.Clear(); FeatherdataGrid.Rows.Clear(); //while() for (int i = 0; i < NumberPoints*10; i++) { if(templetFlag.feathers.featherPoints[i].featherNum!=0) { featherRegion.Add(templetFlag.feathers.featherPoints[i]); FeatherdataGrid.Rows.Add(templetFlag.feathers.featherPoints[i].rec.X, templetFlag.feathers.featherPoints[i].rec.Y, templetFlag.feathers.featherPoints[i].featherNum ); featherRgnstruct tmp=new featherRgnstruct(); tmp.rec=new Rectangle((int)(templetFlag.feathers.featherPoints[i].rec.X*Math.Pow(2,SizeChangecount)), (int)(templetFlag.feathers.featherPoints[i].rec.Y*Math.Pow(2,SizeChangecount)), (int) (templetFlag.feathers.featherPoints[i].rec.Width*Math.Pow(2,SizeChangecount)), (int) (templetFlag.feathers.featherPoints[i].rec.Height*Math.Pow(2,SizeChangecount))); tmp.classifyNUM = templetFlag.feathers.featherPoints[i].classifyNUM; tmp.featherNum = templetFlag.feathers.featherPoints[i].featherNum; showfeature.Add(tmp); } } string data = null; StreamWriter streamwriter = streamwriter = File.CreateText(savePath + "\\NumberBmp\\" + CurrentTempletName.ToString() + "\\" + "AutoFeatherConfig.txt"); data += "0," + ExistNumber.Count; foreach(int tmp in ExistNumber) { data+=","+tmp.ToString(); } streamwriter.Write(data); streamwriter.Close(); }
private void SetTempletbutton1_Click(object sender, EventArgs e) { if (SetTemplettextBox1.Text != null) { if (!Directory.Exists(savePath + "\\NumberBmp" + "\\" + SetTemplettextBox1.Text)) { Directory.CreateDirectory(savePath + "\\NumberBmp" + "\\" + SetTemplettextBox1.Text); Templet templet = new Templet(); templet.templetName = SetTemplettextBox1.Text; CurrentTempletName = SetTemplettextBox1.Text; SetTemplettextBox2.Text = null; SetTemplettextBox3.Text = null; //if(SetTemplettextBox3.) templet.templetSize = new Size(0, 0); templet.feathers.featherPoints = new featherRgnstruct[NumberPoints*10]; for (int i = 0; i < NumberPoints*10; i++) { templet.feathers.featherPoints[i].featherNum = 0; templet.feathers.featherPoints[i].rec = new Rectangle(0, 0, 0, 0); } templetFlag = templet; } else { DialogResult result= MessageBox.Show("该名字模板已存在,是否覆盖原有数据","提示",MessageBoxButtons.OKCancel); if (result.Equals(DialogResult.OK)) { if (borderRegion != null) { borderRegion.Clear(); showborder.Clear(); BorderdataGrid.Rows.Clear(); } Directory.Delete(savePath + "\\NumberBmp" +"\\"+ SetTemplettextBox1.Text, true); Directory.CreateDirectory(savePath + "\\NumberBmp" + "\\" + SetTemplettextBox1.Text); Templet templet = new Templet(); templet.templetName = SetTemplettextBox1.Text; //if(SetTemplettextBox3.) SetTemplettextBox2.Text = ""; SetTemplettextBox3.Text = ""; templet.templetPoint = new Point(0, 0); templet.templetSize = new Size(0, 0); templet.feathers.featherPoints = new featherRgnstruct[NumberPoints*10]; for (int i = 0; i < NumberPoints*10; i++) { templet.feathers.featherPoints[i].featherNum = 0; templet.feathers.featherPoints[i].rec = new Rectangle(0, 0, 0, 0); } CurrentTempletName = SetTemplettextBox1.Text; templetFlag = templet; } else if (result.Equals(DialogResult.Cancel)) { CurrentTempletName = SetTemplettextBox1.Text; templetFlag.templetName = SetTemplettextBox1.Text; if (File.Exists(savePath + "\\NumberBmp" + "\\" + SetTemplettextBox1.Text + "\\" + "templet.txt")) { string streamreader = File.ReadAllText(savePath + "\\NumberBmp" + "\\" + SetTemplettextBox1.Text + "\\" + "templet.txt"); string[] Templetdata = streamreader.Split(','); templetFlag.templetPoint = new Point(int.Parse(Templetdata[1]), int.Parse(Templetdata[2])); templetFlag.templetSize = new System.Drawing.Size(int.Parse(Templetdata[3]), int.Parse(Templetdata[4])); SetTemplettextBox2.Text = Templetdata[1] + "," + Templetdata[2]; //将文件里存的模板大小位置信息给到截取模板文本框内 SetTemplettextBox3.Text = Templetdata[3] + "," + Templetdata[4]; isShowTemplateBorder = true; //绘制模板框 for (int i = 0; i < NumberPoints*10; i++) { featherRgnstruct tmp = new featherRgnstruct(); tmp.rec = new Rectangle(int.Parse(Templetdata[5 + 4 * i]), int.Parse(Templetdata[6 + 4 * i]), 3, 3); tmp.Pixel = int.Parse(Templetdata[7 + 4 * i]); tmp.featherNum = int.Parse(Templetdata[8 + 4 * i]); if (tmp.featherNum != 0) { tmp.classifyNUM = i / NumberPoints; } templetFlag.feathers.featherPoints[i] = tmp; } NextStep.Enabled = true; SetTemplettextBox2.Text = Templetdata[1].ToString() + "," + Templetdata[2].ToString(); SetTemplettextBox3.Text = Templetdata[3].ToString() + "," + Templetdata[4].ToString(); } else { } } } picturebox.Invalidate(); DrawTempleBorder.Enabled = true; SetTemplettextBox3.Enabled = true; SetTemplettextBox2.Enabled = true; } else { MessageBox.Show("请输入模板名"); } }
private void Process_exit() { featherRegion.Clear();//清空所有特征点数据 showfeature.Clear(); FeatherdataGrid.Rows.Clear(); for (int i = 0; i < NumberPoints*10; i++) //初始化模板变量 { templetFlag.feathers.featherPoints[i].featherNum = 0; templetFlag.feathers.featherPoints[i].rec = new Rectangle(0, 0, 0, 0); } if (File.Exists(savePath + "\\NumberBmp\\" + CurrentTempletName.ToString() + "\\points.txt")) //删除原先生成的特征点文件 { try { string[] allData = File.ReadAllLines(savePath + "\\NumberBmp\\" + CurrentTempletName.ToString() + "\\points.txt"); for (int i = 0; i < allData.Count(); i++) { string[] everyNumber = allData[i].Split(','); for (int j = 0; j < everyNumber.Count() - 1; j += 3) { if (int.Parse(everyNumber[j]) != 0 && int.Parse(everyNumber[j+1]) != 0) { featherRgnstruct tmp = new featherRgnstruct(); tmp.rec = new Rectangle(int.Parse(everyNumber[j]) + i * picturebox.Size.Width / ExistNumber.Count, int.Parse(everyNumber[j + 1]), 3, 3); tmp.featherNum = int.Parse(everyNumber[j + 2]); featherRegion.Add(tmp); //将文件内读入的特征点显示到Panel和表格上 showfeature.Add(tmp); FeatherdataGrid.Rows.Add(int.Parse(everyNumber[j]) + i * picturebox.Size.Width / ExistNumber.Count, everyNumber[j + 1], everyNumber[j + 2]); } } } UpdataTemplet(); //将生成的特征点数据更新到TempletFlag变量 } catch { MessageBox.Show("自动获取失败"); } } }
private void pictureBox_MouseDown(object sender, MouseEventArgs e) { Point pt = new Point(e.X, e.Y); if (e.Button == MouseButtons.Left && isSreenShot) { startpoint = pt; startdraw = true; } else if (e.Button == MouseButtons.Left && isaddFeather) //点击添加特征点状态 { Rectangle rec = new Rectangle(pt.X, pt.Y, featherSize.Width, featherSize.Height); featherRgnstruct tmpfeather = new featherRgnstruct(); tmpfeather.featherNum = defaultValue; tmpfeather.rec = rec; Graphics g = (sender as PictureBox).CreateGraphics(); if (FeatherdataGrid.Rows.Count < NumberPoints*10)//特征点上限不超过300个 { g.DrawRectangle(Pens.Blue, new Rectangle(rec.X - (featherSize.Width - 1) / 2, rec.Y - (featherSize.Height - 1) / 2, featherSize.Width, featherSize.Height)); FeatherdataGrid.Rows.Add(tmpfeather.rec.X, tmpfeather.rec.Y, tmpfeather.featherNum); showfeature.Add(tmpfeather); tmpfeather.rec.X = (int)(tmpfeather.rec.X * Math.Pow(2, -SizeChangecount)); tmpfeather.rec.Y = (int)(tmpfeather.rec.Y * Math.Pow(2, -SizeChangecount)); featherRegion.Add(tmpfeather); //UpdataTemplet(); } g.Dispose(); } else if (e.Button == MouseButtons.Left && isaddNumborder) //点击添加识别区域 { if (BorderdataGrid.Rows.Count < 10) { Rectangle rec = new Rectangle(pt, new Size((int)(templetFlag.templetSize.Width * Math.Pow(2, SizeChangecount)), (int)(templetFlag.templetSize.Height * Math.Pow(2, SizeChangecount)))); Graphics g = (sender as PictureBox).CreateGraphics(); g.DrawRectangle(Pens.Blue, rec); Numberborder tmpnumberborder = new Numberborder(); tmpnumberborder.start = new Point((int)(pt.X * Math.Pow(2, -SizeChangecount)), (int)(pt.Y * Math.Pow(2, -SizeChangecount))); tmpnumberborder.size = templetFlag.templetSize; //tmpnumberborder.FeatherName = CurrentTempletName; borderRegion.Add(tmpnumberborder); BorderdataGrid.Rows.Add(tmpnumberborder.start.X, tmpnumberborder.start.Y, CurrentTempletName); tmpnumberborder.start = pt; tmpnumberborder.size = rec.Size; showborder.Add(tmpnumberborder); //识别区域模板 g.Dispose(); } } else if (e.Button == MouseButtons.Left && isMeasureLen) { startMeasure = true; measureStartPoint = new Point((int)(e.X * Math.Pow(2, -SizeChangecount)), (int)(e.Y * Math.Pow(2, -SizeChangecount))); } else if (e.Button == MouseButtons.Right) { #region 右击改变特征点的值 #endregion } if (isDrawTemplate) //进入绘制模板边框状态 { templetLoc = new Point(e.X, e.Y); //记录模板开始的点 SetTemplettextBox2.Text = e.X.ToString() + "," + e.Y.ToString(); CurrentTemplet = new Point(e.X, e.Y); startDrawTemplet = true; //正在进行模板绘制 } picturebox.Invalidate(); }