/// <summary>
        /// 二值化操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Binarize_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double time;
            //在代码开始计时
            Stopwatch sw = new Stopwatch();

            sw.Start();//开始计算
            //代码块
            Bitmap bmp2 = pictureBox1.Image as Bitmap;

            try
            {
                List <string> list = null;
                list = OperateXml.getAllXml();
                string yuzhi = list[0].ToString();
                ImagePro.Binarize(bmp2, yuzhi);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid usage!");
                Console.WriteLine("Usage: grey source object");
            }
            pictureBox1.Image = bmp2;
            //代码块结束
            sw.Stop();//结束计时
            //单位转换
            time        = sw.ElapsedMilliseconds / 1000.000;
            label9.Text = "共" + time.ToString() + "秒";
        }
        /// <summary>
        /// 获取其中黑色像素点所占百分比,得出损坏等级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetTargetPix_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bmp        = pictureBox1.Image as Bitmap;//获取处理后的对象
            long   blackPixel = ImagePro.CalculateBlackPixel(bmp);
            long   allPixel   = ImagePro.CalculateAllPixel(bmp);
            double Percent    = blackPixel / allPixel;

            label3.Text = string.Format("{0:f1}", Percent) + "百万分比";
            if (Percent > 40.0)
            {
                label10.Text = "5";
            }
            else if (Percent > 30.0 && Percent < 40.0)
            {
                label10.Text = "4";
            }
            else if (Percent > 20.0 && Percent < 30.0)
            {
                label10.Text = "3";
            }
            else if (Percent < 20.0 && Percent > 10.0)
            {
                label10.Text = "2";//算好的
            }
            else if (Percent < 10.0)
            {
                label10.Text = "1";      //算优良的
            }
            getConnection(label10.Text); //写入串口,传出数据
        }
Esempio n. 3
0
        /// <summary>
        /// 获取黑点的总数,并返回long值
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static long CalculateBlackPixel(Bitmap bmp)
        {
            byte[] Arr_2 = new byte[bmp.Width * bmp.Height];
            Arr_2 = ImagePro.GetImageBlackPixel(bmp);
            long TotalBlackPixel = 0;

            for (int k = 0; k < Arr_2.Length; k++)
            {
                if (Arr_2[k] == 0)
                {
                    TotalBlackPixel += 1;
                }
            }
            return(TotalBlackPixel);
        }
        /// <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() + "秒";
        }
Esempio n. 5
0
 /// <summary>
 /// 获取所有像素点,并返回一个long值
 /// </summary>
 /// <param name="bmp"></param>
 /// <returns></returns>
 public static long CalculateAllPixel(Bitmap bmp)
 {
     byte[] Arr_1 = new byte[bmp.Width * bmp.Height];
     Arr_1 = ImagePro.GetImageAllPixel(bmp);
     return(Convert.ToInt64(Arr_1.Length) / 10000);
 }
        /// <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();                                                               //主线程等待辅助线程结束
        }