//private void decodematrix()
        //{

        //    Image img = Image.FromFile(filepath1);
        //    Bitmap bmap1;
        //    try
        //    {
        //        bmap1 = new Bitmap(img);
        //    }
        //    catch (System.IO.IOException ioe)
        //    {
        //        MessageBox.Show(ioe.ToString());
        //        return;
        //    }
        //    if (bmap1 == null)
        //    {
        //        MessageBox.Show("Could not decode image");
        //        return;
        //    }


        //    LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
        //    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        //    Result result;
        //    try
        //    {
        //        result = new MultiFormatReader().decode(bitmap);
        //    }
        //    catch (ReaderException re)
        //    {
        //        MessageBox.Show(re.ToString());
        //        return;
        //    }

        //    MessageBox.Show(result.Text);
        //}

        public void testhist()
        {
            Mat img = new Mat();

            img = Cv2.ImRead("d:/lena1.jpg");
            //Console.WriteLine("hist");
            //using (new Window("histimg", img)) ;
            Cv2.ImShow("hs", img);
            Cv2.WaitKey();
            Rangef[] rangefs = new Rangef[]
            {
                new Rangef(0, 256),
            };
            int[] hsize   = { 255 };
            Mat   histimg = new Mat();
            Mat   output  = new Mat();

            Mat[] mats     = new Mat[] { img };
            int[] channels = new int[] { 1 };
            Cv2.CvtColor(img, output, ColorConversionCodes.RGB2GRAY);
            Cv2.CalcHist(mats, channels, output, histimg, 1, hsize, rangefs, true, false);
            for (int i = 0; i < 256; i++)//画直方图
            {
                // Cv2.Line(HistImage, new Point(binImage.Width/256 * (i - 1), binImage.Height - Math.Round(binImage.At<float>(i - 1))), new Point(binImage.Width/256 * (i - 1), binImage.Height - Math.Round(binImage.At<float>(i))), new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);

                // int len = (int)((binImage.Get<float>(i)) * output.Rows);//单个箱子的长度,
                //
                //Cv2.Line(histimg, new Point(img.Width/256*(i-1), img.Height - Math.Round(img.At<float>(i - 1))), new Point(img.Width / 256 * (i - 1), img.Height - Math.Round(img.At<float>(i))), Scalar.Black, 2);//把线画出来
            }
            // Cv2.ImShow("hist", histimg);
            //Cv2.WaitKey();
        }
Пример #2
0
 public MatrixHistogram(Matrix m, int nBins)
 {
     m._mat.MinMaxIdx(out _min, out _max);
     _nBins    = nBins;
     _binWidth = (float)((_max + 1 - _min) / _nBins);
     _range    = new Rangef((float)_min, (float)_max + 1);
 }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Mat      panda    = new Mat(textBox6.Text.ToString(), ImreadModes.Grayscale); //读取为灰度图
                Mat[]    mats     = new Mat[] { panda };                                      //一张图片,初始化为panda
                Mat      hist     = new Mat();                                                //用来接收直方图
                int[]    channels = new int[] { 0 };                                          //一个通道,初始化为通道0
                int[]    histsize = new int[] { 256 };                                        //一个通道,初始化为256箱子
                Rangef[] range    = new Rangef[1];                                            //一个通道,值范围
                range[0].Start = 0.0F;                                                        //从0开始(含)
                range[0].End   = 256.0F;                                                      //到256结束(不含)
                Mat mask = new Mat();                                                         //不做掩码
                Cv2.CalcHist(mats, channels, mask, hist, 1, histsize, range);                 //计算灰度图,dim为1 1维

                Console.WriteLine(hist.Rows + "行" + hist.Cols + "列");                         //把输出的行列打印出来


                Cv2.ImShow("直方图", panda);
                Cv2.ImWrite("ZFT.jpg", panda);
                curBitmap     = (Bitmap)Image.FromFile("ZFT.jpg");
                this.pB.Image = curBitmap;
                pB.Refresh();
                this.groupBox3.Visible = false;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); }
        }
Пример #4
0
        public void RangefParametricValueVerification()
        {
            Rangef range = new Rangef(44.0f, 300.0f);

            Assert.IsWithin(0.0f, range.ParametricValue(44.0f), 0.00001f);
            Assert.IsWithin(1.0f, range.ParametricValue(300.0f), 0.00001f);
            Assert.IsWithin(0.5f, range.ParametricValue(172.0f), 0.00001f);
        }
Пример #5
0
 public GuiRange(string name,
                 IGuiStyle style,
                 IGuiSize size)
     : base(name, size, style)
 {
     mBounds = new Rangef(0.0f, 1.0f);
     Percent = 0.0f;
 }
Пример #6
0
        public DVH(double maxDose, double voxelCC, string doseUnit)
        {
            var topScale = High10((float)maxDose);

            _voxelCC  = voxelCC;
            _nBins    = (int)(topScale * 1000);
            _binWidth = topScale / _nBins;
            _range    = new Rangef(0, topScale);
            _doseUnit = doseUnit;
        }
Пример #7
0
        public void RangefLerpVerification()
        {
            Rangef range  = new Rangef(0.0f, 2.0f);
            Rangef range2 = new Rangef(0.0f, 10.0f);

            Assert.IsWithin(1.0f, range.Lerp(0.5f), 0.00001f);
            Assert.IsWithin(-1.0f, range.Lerp(-0.5f), 0.00001f);

            Assert.IsWithin(10.0f, range2.Lerp(1.0f), 0.00001f);
            Assert.IsWithin(20.0f, range2.Lerp(2.0f), 0.00001f);
        }
Пример #8
0
        public void RangefParseVerification()
        {
            string rangeString  = "-12098.43 234987.4293";
            string rangeString2 = "0";

            Rangef range1 = Rangef.Parse(rangeString);
            Rangef range2 = Rangef.Parse(rangeString2);

            Assert.IsWithin(-12098.43f, range1.Low, 0.01f);
            Assert.IsWithin(234987.4293f, range1.High, 0.1f);

            Assert.AreEqual(range2.Low, range2.High);
        }
Пример #9
0
 public GuiRange(string name,
                 IGuiStyle style,
                 IGuiSize size,
                 Rangef bounds,
                 float position)
     : base(name, size, style)
 {
     if (bounds == null)
     {
         throw new ArgumentNullException("position");
     }
     mBounds = bounds;
     Percent = position;
 }
Пример #10
0
        public double CompareImageByHist(Mat img, Mat refImg, bool showImage = false)
        {
            Mat imgHsv    = new Mat();
            Mat refImgHsv = new Mat();

            Cv2.CvtColor(img, imgHsv, ColorConversionCodes.RGB2HSV);
            Cv2.CvtColor(refImg, refImgHsv, ColorConversionCodes.RGB2HSV);

            Mat[] imgHsvs    = Cv2.Split(imgHsv);
            Mat[] refImgHsvs = Cv2.Split(refImgHsv);

            int bin1 = 50;
            int bin2 = 60;

            int[] bins = { bin1, bin2 };

            int[] channels = { 0, 1 };

            Rangef[] ranges = new Rangef[]
            {
                new Rangef(0, 180),
                new Rangef(0, 256)
            };

            Mat imgHist    = new Mat(img.Size(), MatType.CV_32FC2);
            Mat refImgHist = new Mat(img.Size(), MatType.CV_32FC2);

            Cv2.CalcHist(imgHsvs, channels, new Mat(), imgHist, 2, bins, ranges, true, false);
            Cv2.Normalize(imgHist, imgHist, 1, 0, NormTypes.MinMax, -1, null);

            Cv2.CalcHist(refImgHsvs, channels, new Mat(), refImgHist, 2, bins, ranges, true, false);
            Cv2.Normalize(refImgHist, refImgHist, 1, 0, NormTypes.MinMax, -1, null);

            double ratio = Cv2.CompareHist(imgHist, refImgHist, HistCompMethods.KLDiv);

            if (showImage == true)
            {
                Mat img1 = img.Clone();
                Cv2.PutText(img1, ratio.ToString(), new Point(50, 50), HersheyFonts.HersheyPlain, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);
                Cv2.ImShow("CompareHistTestVSRef", img1);
                Cv2.WaitKey();
                Cv2.DestroyWindow("CompareHistTestVSRef");
            }
            return(ratio);
        }
Пример #11
0
        /// <summary>
        /// 通过直方图比较
        /// </summary>
        /// <param name="sourceImage"></param>
        /// <param name="targetImage"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public static double CompareImageByHistogram(string sourceImage, string targetImage, HistCompMethods method = HistCompMethods.KLDiv)
        {
            using (Mat source = new Mat(sourceImage, ImreadModes.AnyColor | ImreadModes.AnyDepth))
                using (Mat target = new Mat(targetImage, ImreadModes.AnyColor | ImreadModes.AnyDepth))
                {
                    //1:从BGR空间转换到HSV色彩空间
                    Mat src1HSV = new Mat();
                    Mat src2HSV = new Mat();

                    Cv2.CvtColor(source, src1HSV, ColorConversionCodes.BGR2HSV);
                    Cv2.CvtColor(target, src2HSV, ColorConversionCodes.BGR2HSV);
                    //Mat[] mats = new Mat[] { baseHSV, src1HSV, src2HSV };

                    Mat[] mats2 = Cv2.Split(src1HSV);
                    Mat[] mats3 = Cv2.Split(src2HSV);

                    //计算直方图并归一化数据
                    int      bin1     = 50;          //灰度等级
                    int      bin2     = 60;          //灰度等级
                    int[]    histSiz  = { bin1, bin2 };
                    int[]    channels = { 0, 1 };    //图像通道数
                    Rangef[] rangefs  = new Rangef[] //梯度值范围
                    {
                        new Rangef(0, 180),
                        new Rangef(0, 256)
                    };

                    Mat Img2 = new Mat(source.Size(), MatType.CV_32FC2);
                    Mat Img3 = new Mat(target.Size(), MatType.CV_32FC2);

                    Cv2.CalcHist(mats2, channels, new Mat(), Img2, 2, histSiz, rangefs, true, false);
                    Cv2.Normalize(Img2, Img2, 0, 1, NormTypes.MinMax, -1, null);

                    Cv2.CalcHist(mats3, channels, new Mat(), Img3, 2, histSiz, rangefs, true, false);
                    Cv2.Normalize(Img3, Img3, 0, 1, NormTypes.MinMax, -1, null);

                    //比较直方图
                    double s1coms2 = Cv2.CompareHist(Img2, Img3, method);
                    return(s1coms2);
                }
        }
Пример #12
0
        public void matchTestCompareHist()
        {
            Mat img       = new Mat(Images.picTest, ImreadModes.Color);
            Mat refImg    = new Mat(Images.picSetRef, ImreadModes.Color);
            Mat imgHsv    = new Mat();
            Mat refImgHsv = new Mat();

            Cv2.CvtColor(img, imgHsv, ColorConversionCodes.RGB2HSV);
            Cv2.CvtColor(refImg, refImgHsv, ColorConversionCodes.RGB2HSV);

            Mat[] imgHsvs    = Cv2.Split(imgHsv);
            Mat[] refImgHsvs = Cv2.Split(refImgHsv);

            int bin1 = 50;
            int bin2 = 60;

            int[] bins = { bin1, bin2 };

            int[] channels = { 0, 1 };


            Rangef[] ranges = new Rangef[]
            {
                new Rangef(0, 180),
                new Rangef(0, 256)
            };

            Mat imgHist    = new Mat(img.Size(), MatType.CV_32FC2);
            Mat refImgHist = new Mat(img.Size(), MatType.CV_32FC2);

            Cv2.CalcHist(imgHsvs, channels, new Mat(), imgHist, 2, bins, ranges, true, false);
            Cv2.Normalize(imgHist, imgHist, 1, 0, NormTypes.MinMax, -1, null);

            Cv2.CalcHist(refImgHsvs, channels, new Mat(), refImgHist, 2, bins, ranges, true, false);
            Cv2.Normalize(refImgHist, refImgHist, 1, 0, NormTypes.MinMax, -1, null);



            double ratio = Cv2.CompareHist(refImgHist, imgHist, HistCompMethods.Bhattacharyya);
        }
Пример #13
0
        public Mat opencv_method_run(Mat input, Mat output)
        {
            try
            {
                //opencv_method_index index = new opencv_method_index();
                #region switch
                Scalar scalargreen = new Scalar(0x00, 0xFF, 0x00); //绿色 same as 255
                Scalar scalarred   = new Scalar(0x00, 0x00, 0xff); //red
                Scalar scalar      = new Scalar(0, 255, 255);
                Mat    binImage    = new Mat(output.Width, output.Height, MatType.CV_8UC1);
                switch (listBox1.SelectedItem.ToString())
                {
                case "颜色空间转换":
                {
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    break;
                }

                case "OSTU":
                {
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    Cv2.Threshold(output, binImage, 10, 250, ThresholdTypes.Otsu);
                    return(binImage);
                    //break;
                }

                case "Hist":
                {
                    Rangef[] rangefs = new Rangef[]
                    {
                        new Rangef(0, 256),
                    };
                    int[] hsize    = { 255 };
                    Mat   histimg  = new Mat();
                    Mat[] mats     = new Mat[] { output };
                    int[] channels = new int[] { 1 };
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    Cv2.CalcHist(mats, channels, output, histimg, 1, hsize, rangefs, true, false);
                    using (new Window("histImage", WindowMode.Normal, histimg));
                    //Console.WritleLine的后面加上
                    //Mat HistImage = new Mat(MatType.CV_8UC3, new Scalar(0, 0, 0));
                    Mat HistImage = new Mat(binImage.Width, binImage.Height, MatType.CV_8UC1, new Scalar(0, 0, 0));
                    for (int i = 0; i < 256; i++)        //画直方图
                    {
                        // Cv2.Line(HistImage, new Point(binImage.Width/256 * (i - 1), binImage.Height - Math.Round(binImage.At<float>(i - 1))), new Point(binImage.Width/256 * (i - 1), binImage.Height - Math.Round(binImage.At<float>(i))), new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);

                        // int len = (int)((binImage.Get<float>(i)) * output.Rows);//单个箱子的长度,
                        //
                        Cv2.Line(histimg, new Point(i, 0), new Point(0, i), Scalar.Black, 2);     //把线画出来
                    }
                    //Cv2.ImShow("s",HistImage);
                    //using (new Window("histImage", WindowMode.Normal, HistImage))
                    return(histimg);
                    //break;
                }

                case "固定阈值化":
                {
                    Cv2.Threshold(input, output, 150, 200, ThresholdTypes.Binary);
                    break;
                }

                case "边缘检测CANNY":
                {
                    Cv2.Canny(input, output, 45, 200);
                    break;
                }

                case "霍夫圆变换":
                    #region hough circle
                {
                    //LineSegmentPoint[] lines;
                    CircleSegment[]  circles;
                    OpenCvSharp.Size size = new OpenCvSharp.Size(output.Width, output.Height);
                    Mat image_out3        = new Mat(size, MatType.CV_8UC3);
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    //3: dp:累加器分辨率与图像分辨率的反比。默认=1

                    /*4:minDist: 检测到的圆的中心之间的最小距离。(最短距离 - 可以分辨是两个圆的,否则认为是同心圆 - src_gray.rows / 8)
                     * 5:param1: 第一个方法特定的参数。[默认值是100] canny边缘检测阈值低
                     * 6:param2:  第二个方法特定于参数。[默认值是100] 中心点累加器阈值 – 候选圆心
                     * 7:minRadius: 最小半径
                     * 8:maxRadius: 最大半径*/
                    circles = Cv2.HoughCircles(output, HoughMethods.Gradient, 1, 20, 100, 30, 1, 5000);
                    Mat dst = new Mat();
                    input.CopyTo(dst);
                    for (int i = 0; i < circles.Length; i++)
                    {
                        //圆
                        Cv2.Circle(dst, (int)circles[i].Center.X, (int)circles[i].Center.Y, (int)circles[i].Radius, scalargreen, 2, LineTypes.AntiAlias);
                        //加强圆心显示
                        Cv2.Circle(dst, (int)circles[i].Center.X, (int)circles[i].Center.Y, 3, scalarred, 2, LineTypes.Link8);
                    }
                    //using (new Window("OutputImage", WindowMode.AutoSize, dst)) ;
                    //Cv2.WaitKey();
                    //break;
                    return(dst);
                }

                    #endregion
                    #region hough linesp
                case "霍夫累计概率变换":
                {
                    // Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);

                    /*
                     * 1; image: 输入图像 (只能输入单通道图像)
                     *      2; rho:   累加器的距离分辨率(以像素为单位) 生成极坐标时候的像素扫描步长
                     *      3; theta: 累加器的角度分辨率(以弧度为单位)生成极坐标时候的角度步长,一般取值CV_PI/180 ==1度
                     *      4; threshold: 累加器阈值参数。只有那些足够的行才会返回 投票(>阈值);设置认为几个像素连载一起才能被看做是直线。
                     *      5; minLineLength: 最小线长度,设置最小线段是有几个像素组成。
                     *      6;maxLineGap: 同一条线上的点之间连接它们的最大允许间隙。(默认情况下是0):设置你认为像素之间隔多少个间隙也能认为是直线
                     */
                    Cv2.Canny(input, output, 45, 200);
                    LineSegmentPoint[] linePoint;
                    linePoint = Cv2.HoughLinesP(output, 1, 2, 10, 1);
                    for (int i = 0; i < linePoint.Length; i++)
                    {
                        Point p1 = linePoint[i].P1;
                        Point p2 = linePoint[i].P2;
                        Cv2.Line(output, p1, p2, scalar, 4, LineTypes.AntiAlias);
                    }

                    using (new Window("DST", WindowMode.AutoSize, output))
                        return(output);
                }

                    #endregion
                case "自适应阈值":
                {
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    Cv2.AdaptiveThreshold(~output, binImage, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 15, -2);
                    return(binImage);
                }

                case "漫水填充":
                {
                    // Mat binImage = new Mat();
                    Cv2.CvtColor(input, output, ColorConversionCodes.RGB2GRAY);
                    Cv2.AdaptiveThreshold(~output, binImage, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 15, -2);
                    return(binImage);
                }

                default:
                    //Console.WriteLine("default");
                    //FormMain form = new FormMain();
                    MessageBox.Show("请选择处理功能");
                    break;
                }
                #endregion
                return(output);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "未先选择处理方法或者处理异常");
                return(input);
            }


            #endregion
        }
Пример #14
0
 public static float Remap(this float value, Rangef from, Rangef to)
 {
     return((value - from.min) / from.Length * to.Length + to.min);
 }
Пример #15
0
        public void RenderGraph()
        {
            viewportRange = new Rangef(0f - viewportWidth + viewportOffset, 1f + viewportWidth + viewportOffset);

            float viewportFactor = (float)GraphContext.ActualHeight / viewportRange.Length;

            //Draw Grid_SubX
            if (gridLines_subX != null)
            {
                for (int i = 0; i < gridLines_subX.Length; ++i)
                {
                    GraphContext.Children.Remove(gridLines_subX[i]);
                }
            }
            float gridCountF         = (viewportRange.Length / GridSpace);
            int   gridCount          = (int)gridCountF;
            float gridCountRemainder = gridCountF - gridCount;

            if (gridCount % 2 != 0)
            {
                gridCountRemainder += 1f;
            }
            float viewportOffsetRemainder = viewportOffset - (int)viewportOffset;

            if (gridCountF != 0f)
            {
                gridLines_subX = new Line[gridCount + 1];
                for (int i = 0; i < gridLines_subX.Length; ++i)
                {
                    Line line = gridLines_subX[i] = new Line();
                    line.StrokeThickness = GridThickness;
                    line.Stroke          = GridSubColor;
                    GraphContext.Children.Add(line);

                    float halfIndex = i - gridCountF * 0.5f;
                    float y         = ((((halfIndex + gridCountRemainder * 0.5f) / gridCountF)) * (float)GraphContext.ActualHeight) + (float)GraphContext.ActualHeight * 0.5f;

                    line.X1 = 0;
                    line.X2 = GraphContext.ActualWidth;
                    line.Y1 = y;
                    line.Y2 = y;
                }
            }
            //Draw Grid_SubY
            for (int i = 0; i < gridLines_subY.Length; ++i)
            {
                Line  line  = gridLines_subY[i];
                float lineX = ((float)i / gridLines_subY.Length) * (float)GraphContext.ActualWidth;

                line.X1 = lineX;
                line.X2 = lineX;
                line.Y1 = 0;
                line.Y2 = GraphContext.ActualHeight;
            }

            //Draw Grid_Main
            float[] guideYPoints = new float[] {
                0f,
                1f,
            };
            for (int i = 0; i < gridLines_main.Length; ++i)
            {
                Line line = gridLines_main[i];
                Canvas.SetZIndex(line, 1);
                float y     = guideYPoints[i];
                float lineY = (float)(GraphContext.ActualHeight - (y - viewportRange.min) * viewportFactor);
                line.X1 = 0f;
                line.X2 = GraphContext.ActualWidth;
                line.Y1 = lineY;
                line.Y2 = lineY;
            }

            //Draw Line
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double linePieceWidth = GraphContext.ActualWidth / LineResolution;

            float nextY = CalcY(0f);

            for (int i = 0; i < LineResolution; ++i)
            {
                Line line = previewLines[i];
                Canvas.SetZIndex(line, 2);

                float nextTime = (float)(i + 1) / (LineResolution);

                float y = nextY;
                nextY = CalcY(nextTime);

                Vector2 start = new Vector2(0f, y);
                Vector2 end   = new Vector2((float)linePieceWidth, nextY);

                float offset = i * (float)linePieceWidth;
                line.X1 = start.x + offset;
                line.X2 = end.x + offset;
                line.Y1 = GraphContext.ActualHeight - (start.y - viewportRange.min) * viewportFactor;
                line.Y2 = GraphContext.ActualHeight - (end.y - viewportRange.min) * viewportFactor;
            }

            //Draw AnimGrid
            for (int i = 0; i < gridLines_main.Length; ++i)
            {
                Line line = animLines[i];

                float x = i == 0 ? PreviewBallMargin : (float)PreviewAnimContext.ActualWidth - (float)PreviewBallMargin;
                line.X1 = x;
                line.X2 = x;
                line.Y1 = 0f;
                line.Y2 = (float)PreviewAnimContext.ActualHeight;
            }

            watch.Stop();

            LogText();
        }
        public static bool IsUnique(Mat face)
        {
            if (_faces.Count == 0)
            {
                var color1 = ColorsExtractor.ExtractMiddleColor(face);
                color1 = ColorsExtractor.ClosestColorRgb(color1);

                _faces.Add(face);
                _facesMiddleColors.Add(color1);
                return(true);
            }

            /// Using 50 bins for hue and 60 for saturation
            int h_bins = 50;
            int s_bins = 60;

            int[] histSize = new int[2] {
                h_bins, s_bins
            };

            // hue varies from 0 to 179, saturation from 0 to 255
            var h_ranges = new Rangef(0, 180);
            var s_ranges = new Rangef(0, 256);

            var ranges = new Rangef[] { h_ranges, s_ranges };

            // Use the o-th and 1-st channels
            int[] channels = new int[2] {
                0, 1
            };

            var faceHsv = face.CvtColor(ColorConversionCodes.BGR2HSV);

            var faceHistogram = new Mat();

            Cv2.CalcHist(new Mat[] { faceHsv }, channels, new Mat(), faceHistogram, 2, histSize, ranges, true, false);
            Cv2.Normalize(faceHistogram, faceHistogram, 0, 1, NormTypes.MinMax, -1, new Mat());

            var color = ColorsExtractor.ExtractMiddleColor(face);

            foreach (var f in _faces)
            {
                var fHsv = f.CvtColor(ColorConversionCodes.BGR2HSV);

                var fHistogram = new Mat();
                Cv2.CalcHist(new Mat[] { fHsv }, channels, new Mat(), fHistogram, 2, histSize, ranges, true, false);
                Cv2.Normalize(fHistogram, fHistogram, 0, 1, NormTypes.MinMax, -1, new Mat());

                double correl = Cv2.CompareHist(faceHistogram, fHistogram, HistCompMethods.Correl);

                //if (correl > 0.5) return false;
                bool contains = _facesMiddleColors.Contains(color);
                if (correl > 0.4 && contains)
                {
                    return(false);
                }
                //if (correl > 0.65) return false;
            }

            color = ColorsExtractor.ExtractMiddleColor(face);

            _faces.Add(face);
            _facesMiddleColors.Add(color);
            return(true);
        }
Пример #17
0
        private static void HistogarmToComparison(string path1, string path2, string path3)
        {
            using (Mat baseMat = new Mat(path1, ImreadModes.AnyColor | ImreadModes.AnyDepth))
                using (Mat src1 = new Mat(path2, ImreadModes.AnyColor | ImreadModes.AnyDepth))
                    using (Mat src2 = new Mat(path3, ImreadModes.AnyColor | ImreadModes.AnyDepth))
                    {
                        //1:从BGR空间转换到HSV色彩空间


                        Mat baseHSV = new Mat();
                        Mat src1HSV = new Mat();
                        Mat src2HSV = new Mat();


                        Cv2.CvtColor(baseMat, baseHSV, ColorConversionCodes.BGR2HSV);
                        Cv2.CvtColor(src1, src1HSV, ColorConversionCodes.BGR2HSV);
                        Cv2.CvtColor(src2, src2HSV, ColorConversionCodes.BGR2HSV);
                        //Mat[] mats = new Mat[] { baseHSV, src1HSV, src2HSV };
                        Mat[] mats1 = Cv2.Split(baseHSV);
                        Mat[] mats2 = Cv2.Split(src1HSV);
                        Mat[] mats3 = Cv2.Split(src2HSV);

                        //计算直方图并归一化数据

                        int   bin1    = 50; //灰度等级
                        int   bin2    = 60; //灰度等级
                        int[] histSiz = { bin1, bin2 };

                        int[]    channels = { 0, 1 };    //图像通道数
                        Rangef[] rangefs  = new Rangef[] //梯度值范围
                        {
                            new Rangef(0, 180),
                            new Rangef(0, 256)
                        };

                        Mat             Img1   = new Mat(baseMat.Size(), MatType.CV_32FC2);
                        Mat             Img2   = new Mat(src1.Size(), MatType.CV_32FC2);
                        Mat             Img3   = new Mat(src2.Size(), MatType.CV_32FC2);
                        HistCompMethods method = HistCompMethods.KLDiv;


                        Cv2.CalcHist(mats1, channels, new Mat(), Img1, 2, histSiz, rangefs, true, false);
                        Cv2.Normalize(Img1, Img1, 0, 1, NormTypes.MinMax, -1, null);

                        Cv2.CalcHist(mats2, channels, new Mat(), Img2, 2, histSiz, rangefs, true, false);
                        Cv2.Normalize(Img2, Img2, 0, 1, NormTypes.MinMax, -1, null);

                        Cv2.CalcHist(mats3, channels, new Mat(), Img3, 2, histSiz, rangefs, true, false);
                        Cv2.Normalize(Img3, Img3, 0, 1, NormTypes.MinMax, -1, null);

                        //比较直方图
                        double bcomb   = Cv2.CompareHist(Img1, Img1, method);
                        double s1coms2 = Cv2.CompareHist(Img2, Img3, method);
                        double bcoms1  = Cv2.CompareHist(Img1, Img2, method);
                        double bcoms2  = Cv2.CompareHist(Img1, Img3, method);

                        Mat mats1Ands2 = new Mat();
                        src2.CopyTo(mats1Ands2);

                        Cv2.PutText(baseMat, bcomb.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);

                        Cv2.PutText(src1, bcoms1.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);

                        Cv2.PutText(src2, bcoms2.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);

                        Cv2.PutText(mats1Ands2, s1coms2.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);

                        using (new Window("Img1 :Img1", WindowMode.Normal, baseMat))
                            using (new Window("Img1 :Img2", WindowMode.Normal, src1))
                                using (new Window("Img1 :Img3", WindowMode.Normal, src2))
                                {
                                    Cv2.WaitKey(0);
                                }
                    }
        }
Пример #18
0
        /// 直方图计算
        public void HistogramCalculation()
        {
            Mat img = new Mat(Images.picSetRef, ImreadModes.Color);
            Mat src = new Mat();

            Cv2.CvtColor(img, src, ColorConversionCodes.RGB2HSV);
            //using (Mat src = new Mat(Images.picSetRef, ImreadModes.Color))
            using (Mat histogram = new Mat())
            {
                //计算直方图
                Mat[] mats   = Cv2.Split(src); //分割图像(把三通道分割为3个单通道)
                Mat   hist_B = new Mat();
                Mat   hist_G = new Mat();
                Mat   hist_R = new Mat();

                int[] channels0 = { 0 };
                int[] channels1 = { 1 };
                int[] channels2 = { 2 };
                int[] histSize  = { 256 };


                Rangef[] rangefs = new Rangef[]
                {
                    new Rangef(0, 256),
                };


                //     computes the joint dense histogram for a set of images.
                //      计算一组图像的联合密集直方图。
                Cv2.CalcHist(mats, channels0, new Mat(), hist_B, 1, histSize, rangefs, true, false);
                Cv2.CalcHist(mats, channels1, new Mat(), hist_G, 1, histSize, rangefs, true, false);
                Cv2.CalcHist(mats, channels2, new Mat(), hist_R, 1, histSize, rangefs, true, false);

                int high      = 400;
                int width     = 512;
                int bin_w     = width / 256;                                                //每个bins的宽度  画布的宽度除以bins的个数
                Mat histImage = new Mat(width, high, MatType.CV_8UC3, new Scalar(0, 0, 0)); //定义一个Mat对象,相当于一个画布

                //归一化,像素值有可能数据量很大,压缩一下。是范围在定义画布的范围内。
                Cv2.Normalize(hist_B, hist_B, 0, histImage.Rows, NormTypes.MinMax, -1, null);
                Cv2.Normalize(hist_G, hist_G, 0, histImage.Rows, NormTypes.MinMax, -1, null);
                Cv2.Normalize(hist_R, hist_R, 0, histImage.Rows, NormTypes.MinMax, -1, null);

                //绘制直方图
                for (int i = 1; i < 256; i++)//遍历直方图的级数
                {
                    //B 画线,一条线有两个点组成。首先确定每个点的坐标(x,y) .遍历从1开始。0 ~ 1 两个点组成一条线,依次类推。
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_B.At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_B.At <float>(i))), new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);

                    //G
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_G.At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_G.At <float>(i))), new Scalar(0, 255, 0), 1, LineTypes.AntiAlias);

                    //R
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_R.At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_R.At <float>(i))), new Scalar(0, 0, 255), 1, LineTypes.AntiAlias);
                }
                using (new Window("SRC", WindowMode.Normal, src))
                    using (new Window("histImage", WindowMode.Normal, histImage))
                    {
                        Cv2.WaitKey(0);
                    }
            }
        }
Пример #19
0
            public StationInfo(XmlNode stationNode, GameObject assetsPrototypeRoot)
            {
                // Type (Mandatory)
                XmlNode typeNode = stationNode.SelectSingleNode("@type");

                if (typeNode != null)
                {
                    mType = typeNode.InnerText;
                    if (mType != "Hair" &&
                        mType != "Makeup" &&
                        mType != "Sewing" &&
                        mType != "Holding")
                    {
                        throw new Exception("Unknown Fashion Game Station Type (" + mType + ")");
                    }
                }
                else
                {
                    throw new Exception("No type attribute found on Station node. Cannot create station info.");
                }

                // Asset (Mandatory)
                XmlNode assetNode = stationNode.SelectSingleNode("@asset");

                if (assetNode != null)
                {
                    mAssetPrototype = GameObjectUtility.GetNamedChild(assetNode.InnerText, assetsPrototypeRoot);
                }
                else
                {
                    throw new Exception("No asset attribute found on Station node. Cannot create station info.");
                }

                // Time (Optional)
                XmlNode timeNode = stationNode.SelectSingleNode("@time");

                if (timeNode != null)
                {
                    mTime = Rangef.Parse(timeNode.InnerText).RandomValue();
                }
                else
                {
                    mTime = 0.0f;
                }

                // Icon (Mandatory)
                XmlNode imagePathNode = stationNode.SelectSingleNode("Icon/@path");

                if (imagePathNode != null)
                {
                    mImagePath = ProtocolUtility.ResolvePath(imagePathNode.InnerText);
                }
                else
                {
                    mImagePath = null;
                }

                // Gui Offset (Optional)
                XmlNode guiOffsetNode = stationNode.SelectSingleNode("@guiOffset");

                if (guiOffsetNode != null)
                {
                    mGuiOffset = SerializationUtility.ToVector3(guiOffsetNode.InnerText);
                }
                else
                {
                    mGuiOffset = Vector3.zero;
                }

                // Sounds (Optional, Multiple Supported)
                XmlNodeList soundNodes = stationNode.SelectNodes("ActivationSound/@path");

                foreach (XmlNode soundNode in soundNodes)
                {
                    mSoundPaths.Add(soundNode.InnerText);
                }

                // WorkingAnimation (Optional)
                XmlNode workingAnimationNode = stationNode.SelectSingleNode("WorkingAnimation/@path");

                if (workingAnimationNode != null)
                {
                    mWorkingAnimationPath = workingAnimationNode.InnerText;
                }
                else
                {
                    mWorkingAnimationPath = null;
                }

                // IdleAnimation (Optional)
                XmlNode idleAnimationNode = stationNode.SelectSingleNode("IdleAnimation/@path");

                if (idleAnimationNode != null)
                {
                    mIdleAnimationPath = idleAnimationNode.InnerText;
                }
                else
                {
                    mIdleAnimationPath = null;
                }
            }
Пример #20
0
        public Mat[] HistogramCalculation(Mat img, bool showImage = false)
        {
            //Mat img = new Mat(Images.picSetRef, ImreadModes.Color);
            Mat src = new Mat();

            Cv2.CvtColor(img, src, ColorConversionCodes.RGB2HSV);

            using (Mat histogram = new Mat())
            {
                //计算直方图
                Mat[] mats = Cv2.Split(src); //分割图像(把三通道分割为3个单通道)
                //Mat hist_H = new Mat();
                //Mat hist_S = new Mat();
                //Mat hist_V = new Mat();
                Mat[] hist_HSV = { new Mat(), new Mat(), new Mat() };

                int[] channels0 = { 0 };
                int[] channels1 = { 1 };
                int[] channels2 = { 2 };
                int[] histSize  = { 16 };   //256

                Rangef[] rangefh = new Rangef[]
                {
                    new Rangef(0, 180),
                };
                Rangef[] rangefsv = new Rangef[]
                {
                    new Rangef(0, 256),
                };


                //     computes the joint dense histogram for a set of images.
                //      计算一组图像的联合密集直方图。
                Cv2.CalcHist(mats, channels0, new Mat(), hist_HSV[0], 1, histSize, rangefh, true, false);
                Cv2.CalcHist(mats, channels1, new Mat(), hist_HSV[1], 1, histSize, rangefsv, true, false);
                Cv2.CalcHist(mats, channels2, new Mat(), hist_HSV[2], 1, histSize, rangefsv, true, false);

                int high      = 400;
                int width     = 512;
                int bin_w     = width / 16;                                                 //每个bins的宽度  画布的宽度除以bins的个数  //256
                Mat histImage = new Mat(width, high, MatType.CV_8UC3, new Scalar(0, 0, 0)); //定义一个Mat对象,相当于一个画布
                //using (new Window("SRC", src))
                //using (new Window("histImage", histImage))
                //{
                //    Cv2.WaitKey(0);
                //    Cv2.DestroyWindow("SRC");
                //    Cv2.DestroyWindow("histImage");
                //}

                //归一化,像素值有可能数据量很大,压缩一下。是范围在定义画布的范围内。
                Cv2.Normalize(hist_HSV[0], hist_HSV[0], 0, histImage.Rows, NormTypes.MinMax, -1, null);
                Cv2.Normalize(hist_HSV[1], hist_HSV[1], 0, histImage.Rows, NormTypes.MinMax, -1, null);
                Cv2.Normalize(hist_HSV[2], hist_HSV[2], 0, histImage.Rows, NormTypes.MinMax, -1, null);

                //绘制直方图
                for (int i = 1; i < 16; i++)//遍历直方图的级数 //256
                {
                    //B 画线,一条线有两个点组成。首先确定每个点的坐标(x,y) .遍历从1开始。0 ~ 1 两个点组成一条线,依次类推。
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[0].At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[0].At <float>(i))), new Scalar(255, 0, 0), 1, LineTypes.AntiAlias);

                    //G
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[1].At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[1].At <float>(i))), new Scalar(0, 255, 0), 1, LineTypes.AntiAlias);

                    //R
                    Cv2.Line(histImage, new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[2].At <float>(i - 1))), new Point(bin_w * (i - 1), high - Math.Round(hist_HSV[2].At <float>(i))), new Scalar(0, 0, 255), 1, LineTypes.AntiAlias);
                }
                if (showImage == true)
                {
                    using (new Window("SRC", src))   //WindowMode.Normal
                        using (new Window("histImage", histImage))
                        {
                            Cv2.WaitKey(0);
                            Cv2.DestroyWindow("SRC");
                            Cv2.DestroyWindow("histImage");
                        }
                }

                return(hist_HSV);
            }
        }
Пример #21
0
 public GuiRange(GuiRange copy)
     : base(copy.Name, copy.GuiSize, copy.Style)
 {
     mBounds  = copy.mBounds;
     mPercent = copy.mPercent;
 }