/// <summary> /// 灰度化操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Gray_ToolStripMenuItem_Click(object sender, EventArgs e) { double time; //在代码开始计时 Stopwatch sw = new Stopwatch(); sw.Start();//开始计算 //代码块 Bitmap bmp1 = pictureBox1.Image as Bitmap; ImagePro.GrayScale(bmp1); pictureBox1.Image = bmp1; //代码块结束 sw.Stop();//结束计时 //单位转换 time = sw.ElapsedMilliseconds / 1000.000; label7.Text = "共" + time.ToString() + "秒"; }
/// <summary> /// 配合工业相机自动化运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AutoRun_ToolStripMenuItem_Click(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(ThreadMethod)); //也可简写为new Thread(ThreadMethod); th.Start(); //启动线程 while (OperateXml.getAllXml()[2].ToString() == "是") //判断配置文件中参数 { //执行图像处理代码 //1、读取文件 String path = OperateXml.getAllXml()[1].ToString();//读取配置中路径字符串 DirectoryInfo folder = new DirectoryInfo(path); //1、先处理 if (folder.GetFiles("*.jpg").Length >= 1) { Bitmap bmp = new Bitmap(folder.GetFiles("*.jpg")[0].FullName);//创建图像对象 try { List <string> list = null; list = OperateXml.getAllXml(); string yuzhi = list[0].ToString(); ImagePro.GrayScale(bmp); //灰度化 ImagePro.Binarize(bmp, yuzhi); //二值化 #region 计算黑度值,所占百分比 long blackPixel = ImagePro.CalculateBlackPixel(bmp); long allPixel = ImagePro.CalculateAllPixel(bmp); double Percent = blackPixel / allPixel; bmp.Dispose();//释放资源,才可以执行下一步删除操作 #endregion #region 得出等级 if (Percent > 40.0) { getConnection("5"); } else if (Percent > 30.0 && Percent < 40.0) { getConnection("4"); } else if (Percent > 20.0 && Percent < 30.0) { getConnection("3"); } else if (Percent < 20.0 && Percent > 10.0) { getConnection("2");//算好的 } else if (Percent < 10.0) { getConnection("1");//算优良的 } #endregion } catch (Exception) { Console.WriteLine("Invalid usage!"); Console.WriteLine("Usage: grey source object"); } //2、删除 folder.GetFiles("*.jpg")[0].Delete(); } else { if (MessageBox.Show("路径下没有图片!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK) { // 关闭所有的线程 this.Dispose(); this.Close(); System.Environment.Exit(0); } } //读取配置中间隔时间 Thread.Sleep(Convert.ToInt32(OperateXml.getAllXml()[3].ToString())); //等待一定时长 } th.Join(); //主线程等待辅助线程结束 }