Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            clearText();
            string path = @"D:\Play Data\毕业设计\测试数据\CSDN";
            string savepath = @"D:\Play Data\毕业设计\测试数据\CSDN-out";

            Stopwatch timer = new Stopwatch();
            foreach (var filename in Directory.GetFiles(path)) {
                string file = Path.GetFileNameWithoutExtension(filename);

                timer.Reset();
                timer.Start();
                Bitmap bmp = new Bitmap(filename);
                byte[] rgb = ImageProcessor.Bmp2Bytes(bmp);
                byte[] grey = new WeightedAverageGreyscale().Greyscale(rgb);
                byte[] b2 = new MaxVarianceBinarization().Binarization(grey, bmp.Width, bmp.Height);

                int it = 0;
                byte[] dn = new AnnealKMeansClusterDenoise().Denoise(b2, bmp.Width, bmp.Height, 1, out it);
                var img = ImageProcessor.Greyscale2Bmp(dn, bmp.Width, bmp.Height);
                timer.Stop();

                appendLine("文件:{0},用时:{1}ms, 降噪迭代次数:{2}", file, timer.ElapsedMilliseconds, it);

                img.Save(Path.Combine(savepath, file + ".out.bmp"));
            }
        }
Esempio n. 2
0
        private static void ProcessImage()
        {
            string dirname = "cnbeta";
            string path = @"D:\Play Data\毕业设计\测试数据\" + dirname;
            string savepath = @"D:\Play Data\毕业设计\测试数据\" + dirname + "-out";

            Stopwatch timer = new Stopwatch();
            using (StreamWriter sw = new StreamWriter(@"D:\Play Data\毕业设计\测试数据\" + dirname + ".txt")) {
                foreach (var filename in Directory.GetFiles(path)) {
                    string file = Path.GetFileNameWithoutExtension(filename);
                    timer.Reset();
                    timer.Start();

                    Image<Bgr, Byte> bmp = new Image<Bgr, byte>(filename);
                    Image<Gray, Byte> gray = bmp.Convert<Gray, Byte>();
                    double threshold = new MaxVarianceBinarization().CalculateThreshold(gray);
                    Image<Gray, Byte> bi = gray.ThresholdBinaryInv(new Gray(threshold), new Gray(255));
                    int it;
                    //Stopwatch timer = Stopwatch.StartNew();
                    Image<Gray, Byte> dn = new AnnealKMeansClusterDenoise().Denoise(bi, 1, out it);
                    timer.Stop();
                    //appendLine("降噪用时{0}ms,迭代次数{1}。", timer.ElapsedMilliseconds, it);
                    Console.WriteLine("文件:{0},用时:{1}ms, 降噪迭代次数:{2}", file, timer.ElapsedMilliseconds, it);
                    dn.Save(Path.Combine(savepath, file + ".out.bmp"));
                }
            }
        }
Esempio n. 3
0
        private void gao()
        {
            clearText();
            string filename = label1.Text;
            string path = Path.GetDirectoryName(filename);
            string file = Path.GetFileNameWithoutExtension(filename);
            string savepath = Path.Combine(path, "out");
            appendLine(filename);

            Image<Bgr, Byte> bmp = new Image<Bgr, byte>(filename);

            Image<Gray, Byte> gray = bmp.Convert<Gray, Byte>();

            double threshold = new MaxVarianceBinarization().CalculateThreshold(gray);
            appendLine("阈值:{0}", threshold);

            //Image<Gray, Byte> bi = gray.ThresholdBinaryInv(new Gray(threshold), new Gray(255));
            // Image<Gray, Byte> dn = bi.SmoothMedian(3);
            int it;
            //Stopwatch timer = Stopwatch.StartNew();
            //Image<Gray, Byte> dn = new AnnealKMeansClusterDenoise().Denoise(bi, 1, out it);
            //timer.Stop();
            //appendLine("降噪用时{0}ms,迭代次数{1}。", timer.ElapsedMilliseconds, it);

            //var con = dn.FindContours();
            //bmp.Draw(con, new Bgr(0, 0, 255), new Bgr(255, 0, 0), 1, 1);

            //var proj = Projecting.VerticalProject(dn);
            //var proj = bi.SmoothMedian(3);

            //var proj = dn - dn.Erode(1);//dn.Dilate(1) - bi;

            //var proj = gray.MorphologyEx(see, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_OPEN, 1);
            //var proj =
            //showImage(imageBox1, bi);
            //showImage(imageBox2, dn);
            //showImage(imageBox1, gray);

            //showImage(imageBox1, gray);

            timer.Restart();
            var mask = gray.SmoothMask();
            showImage(imageBox1, mask);
            appendLine("掩模:{0}ms", timer.Stop());

            timer.Restart();
            double thr = new MaxVarianceBinarization().CalculateThreshold(mask);
            var thrd = mask.ThresholdBinaryInv(new Gray(thr), new Gray(255));
            showImage(imageBox2, thrd);
            appendLine("二值:{0}ms", timer.Stop());

            timer.Restart();
            var smooth = thrd.SmoothMedian(7);
            showImage(imageBox3, smooth);
            appendLine("中值滤波:{0}ms", timer.Stop());

            timer.Restart();
            var dm = new AnnealKMeansClusterDenoise().Denoise(thrd, 0, out it).SmoothMedian(5);
            showImage(imageBox4, dm);
            appendLine("区域联通:{0}ms", timer.Stop());

            #region 灰度分级连通
            //int level = 16;
            //var gray4 = mask.ToGrayN(level);
            //showImage(imageBox1, gray4);

            //timer.Restart();
            //var conns = gray4.Connect(level);
            //appendLine("分级:{0}ms", timer.Stop());

            //var mins = Utils.FindLocalMins(conns.Select(c => c.Domains.Count).ToArray());
            //var list = new List<ConnectLevel>();
            //for (int i = 1; i < mins.Length; ++i) {
            //    var cl = conns[mins[i]];
            //    if (cl.Domains.Count == 1) continue;
            //    if (cl.MeanArea < 10) continue;
            //    if (cl.Variance > 20000) continue;
            //    //if (mins[i] - mins[i - 1] == 1) {
            //    //    var lastcl = conns[mins[i] - 1];
            //    //    if (lastcl.Domains.Count != cl.Domains.Count)
            //    //        list.Add(cl);
            //    //} else {
            //    //    list.Add(cl);
            //    //}
            //    list.Add(cl);
            //}
            //list.ForEach(
            //    c => appendLine("Lv{0},C{1},MA{2:F2},VA{3:F4}", c.Level, c.Domains.Count, c.MeanArea, c.Variance));
            //timer.Restart();
            //var bestlevel = list.OrderByDescending(c => c.MeanArea).ThenBy(c => c.Domains.Count).First();
            //var colored = gray4.ColorLevel(bestlevel);
            //var see = new StructuringElementEx(4, 4, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_ELLIPSE);
            //var bg = colored.Erode(10).Dilate(10);
            //showImage(imageBox2, colored - bg);
            //appendLine("染色处理{0}ms", timer.Stop());

            #endregion

            //double t2=new MaxVarianceBinarization().CalculateThreshold(mask);
            //var proj = mask.ThresholdBinaryInv(new Gray(t2), new Gray(255));
            //var akmcd = new AnnealKMeansClusterDenoise();
            //var t3 = new MaxVarianceBinarization().CalculateThreshold(mask);
            //var bi2 = mask.ThresholdBinaryInv(new Gray(t3), new Gray(255));
            //showImage(imageBox2, bi2);

            //var dn2 = akmcd.Denoise(bi2, 1, out it);
            //showImage(imageBox3, dn2);

            //var contour = ContourFinder.ColorContours(dn2, new Gray(0), new Gray(255));
            //showImage(imageBox5, contour);

            //var con = dn2.FindContours();
            //var con2 = dn2.Convert<Bgr, Byte>();
            //con2.Draw(con, new Bgr(0, 0, 255), new Bgr(0, 255, 0), 1, 1);
            //showImage(imageBox6, con2);

            //p1 = akmcd.Denoise(p1, 0, out it);
            //p2 = akmcd.Denoise(p2, 0, out it);
            //var p3 = p1.Xor(p2);
            //var p4 = p1 - p3;
            //var p5 = p2 - p3;
            //var p6 = p1 - p2;

            //var p4 = akmcd.Denoise(p1, 0, out it);
            //var p5 = akmcd.Denoise(p2, 0, out it);
            //var p6 = akmcd.Denoise(p3, 0, out it);

            //StructuringElementEx se = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);

            //dn = dn.MorphologyEx(se, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_OPEN, 1);

            //var canny = dn.Canny(new Gray(threshold), new Gray(threshold * 0.6));

            //showImage(imageBox1, bmp);
            //showImage(imageBox2, gray);
            //showImage(imageBox3, bi);
            //showImage(imageBox4, dn);
            //showImage(imageBox5, p1);
            //showImage(imageBox6, p2);
            //showImage(imageBox4, p4);
            //showImage(imageBox5, p5);
            //showImage(imageBox6, p6);
        }