中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用C#?OpenCV實現形狀匹配

發布時間:2022-09-19 09:37:05 來源:億速云 閱讀:159 作者:iii 欄目:開發技術

這篇文章主要介紹了怎么用C# OpenCV實現形狀匹配的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用C# OpenCV實現形狀匹配文章都會有所收獲,下面我們一起來看看吧。

ROI區域繪制代碼如下:

        /// <summary>
        /// 區域繪制
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="regionEx"></param>
        /// <param name="sizeratio"></param>
       public  static void drawRegion(this Graphics graphics, RegionEx regionEx,float sizeratio=1)
        {       
            if (regionEx?.Region is RectangleF)
            {
                RectangleF rect = (RectangleF)regionEx.Region;
                graphics.DrawRectangle(new Pen(regionEx.Color, regionEx.Size), rect.X / sizeratio, rect.Y / sizeratio,
                                                    rect.Width / sizeratio, rect.Height / sizeratio);
            }
            else if(regionEx?.Region is RotatedRectF)
            {
                RotatedRectF rrect = (RotatedRectF)regionEx.Region;
               
                using (var graph = new GraphicsPath())
                {
                    PointF Center = new PointF(rrect.cx / sizeratio, rrect.cy / sizeratio);
                 
                    graph.AddRectangle(new RectangleF( rrect.getrectofangleEqualZero().X / sizeratio,
                        rrect.getrectofangleEqualZero().Y / sizeratio,
                        rrect.getrectofangleEqualZero().Width / sizeratio,
                        rrect.getrectofangleEqualZero().Height / sizeratio));
                    graph.AddLine(new PointF((rrect.cx - rrect.Width / 2) / sizeratio, rrect.cy / sizeratio),
                                 new PointF((rrect.cx + rrect.Width/2) / sizeratio, rrect.cy / sizeratio));
                    /
                    RotatedRectF rotatedRectF = new RotatedRectF((rrect.cx + rrect.Width / 2) / sizeratio,
                        rrect.cy / sizeratio,20 / sizeratio, 10 / sizeratio, 0);
                    PointF[] point2Fs = rotatedRectF.getPointF();
                    graph.AddLine(new PointF((rrect.cx + rrect.Width / 2) / sizeratio,
                        rrect.cy / sizeratio), new PointF(point2Fs[0].X, point2Fs[0].Y));
                    graph.AddLine(new PointF((rrect.cx + rrect.Width / 2) / sizeratio,
                       rrect.cy / sizeratio), new PointF(point2Fs[3].X, point2Fs[3].Y));
                    /
                    var a = rrect.angle * (Math.PI / 180);
                    var n1 = (float)Math.Cos(a);
                    var n2 = (float)Math.Sin(a);
                    var n3 = -(float)Math.Sin(a);
                    var n4 = (float)Math.Cos(a);
                    var n5 = (float)(Center.X * (1 - Math.Cos(a)) + Center.Y * Math.Sin(a));
                    var n6 = (float)(Center.Y * (1 - Math.Cos(a)) - Center.X * Math.Sin(a));
                    graph.Transform(new Matrix(n1, n2, n3, n4, n5, n6));
                    graphics.DrawPath(new Pen(regionEx.Color, regionEx.Size), graph);
                  
                }
            }           
            else if (regionEx?.Region is RotatedCaliperRectF)
            {
                RotatedCaliperRectF rrect = (RotatedCaliperRectF)regionEx.Region;
 
                using (var graph = new GraphicsPath())
                {
                    PointF Center = new PointF(rrect.cx / sizeratio, rrect.cy / sizeratio);
 
                    graph.AddRectangle(new RectangleF(rrect.getrectofangleEqualZero().X / sizeratio,
                        rrect.getrectofangleEqualZero().Y / sizeratio,
                        rrect.getrectofangleEqualZero().Width / sizeratio,
                        rrect.getrectofangleEqualZero().Height / sizeratio));
                    graph.AddLine(new PointF((rrect.cx - rrect.Width / 2) / sizeratio, rrect.cy / sizeratio),
                                 new PointF((rrect.cx + rrect.Width / 2) / sizeratio, rrect.cy / sizeratio));
                    /
                    RotatedCaliperRectF rotatedRectF = new RotatedCaliperRectF((rrect.cx + rrect.Width / 2) / sizeratio,
                        rrect.cy / sizeratio, 20 / sizeratio, 10 / sizeratio, 0);
                    PointF[] point2Fs = rotatedRectF.getPointF();
                    graph.AddLine(new PointF((rrect.cx + rrect.Width / 2) / sizeratio,
                        rrect.cy / sizeratio), new PointF(point2Fs[0].X, point2Fs[0].Y));
                    graph.AddLine(new PointF((rrect.cx + rrect.Width / 2) / sizeratio,
                       rrect.cy / sizeratio), new PointF(point2Fs[3].X, point2Fs[3].Y));
                    /
                    var a = rrect.angle * (Math.PI / 180);
                    var n1 = (float)Math.Cos(a);
                    var n2 = (float)Math.Sin(a);
                    var n3 = -(float)Math.Sin(a);
                    var n4 = (float)Math.Cos(a);
                    var n5 = (float)(Center.X * (1 - Math.Cos(a)) + Center.Y * Math.Sin(a));
                    var n6 = (float)(Center.Y * (1 - Math.Cos(a)) - Center.X * Math.Sin(a));
                    graph.Transform(new Matrix(n1, n2, n3, n4, n5, n6));
                    graphics.DrawPath(new Pen(regionEx.Color, regionEx.Size), graph);
 
                }
            }
            else if (regionEx?.Region is CircleF)
            {
                CircleF circle = (CircleF)regionEx.Region;
                graphics.DrawEllipse(new Pen(regionEx.Color, regionEx.Size), (circle.Centerx - circle.Radius) / sizeratio,
                      (circle.Centery - circle.Radius) / sizeratio, 2 * circle.Radius / sizeratio, 2 * circle.Radius / sizeratio);
 
            }
            else if (regionEx?.Region is PointF)
            {
                PointF point = (PointF)regionEx.Region;
                graphics.DrawPolygon(new Pen(regionEx.Color, regionEx.Size), new PointF[] { new PointF (
                    point.X/sizeratio,point.Y/sizeratio
                    )});
            }
            else if (regionEx?.Region is PolygonF)
            {
                PolygonF polygon = (PolygonF)regionEx.Region;
                List<PointF> temlist = new List<PointF>();
                foreach (var s in polygon.Points)
                    temlist.Add(new PointF(s.X / sizeratio, s.Y / sizeratio));
                graphics.DrawPolygon(new Pen(regionEx.Color, regionEx.Size), temlist.ToArray());
            }
            else if (regionEx?.Region is LineF)
            {
                LineF line = (LineF)regionEx.Region;            
                graphics.DrawLine(new Pen(regionEx.Color, regionEx.Size), line.x1/ sizeratio, line.y1/ sizeratio,
                   line.x2/ sizeratio, line.y2/ sizeratio);
            }
            else if (regionEx?.Region is CrossF)
            {
                CrossF cross = (CrossF)regionEx.Region;
                graphics.DrawLine(new Pen(regionEx.Color, regionEx.Size), (cross.x1- cross.width/2) / sizeratio, cross.y1 / sizeratio,
                  (cross.x1 + cross.width / 2) / sizeratio, cross.y1 / sizeratio);
                graphics.DrawLine(new Pen(regionEx.Color, regionEx.Size), cross.x1 / sizeratio, (cross.y1- cross.height/2) / sizeratio,
                  cross.x1 / sizeratio, (cross.y1 + cross.height / 2) / sizeratio);
                graphics.DrawEllipse(new Pen(regionEx.Color, regionEx.Size), (cross.x1 - cross.radius) / sizeratio,
                       (cross.y1 - cross.radius) / sizeratio, 2 * cross.radius / sizeratio, 2 * cross.radius / sizeratio);
            }
            else if(regionEx?.Region is SectorF)
            {
                SectorF sectorF=(SectorF)regionEx.Region;
 
                //graphics.DrawEllipse(MyPens.assist, sectorF.x / sizeratio, sectorF.y / sizeratio,
                //  sectorF.width / sizeratio, sectorF.height / sizeratio);
 
                graphics.DrawPie(new Pen(regionEx.Color, regionEx.Size),
                    sectorF.x / sizeratio, sectorF.y / sizeratio, 
                    sectorF.width / sizeratio, sectorF.height / sizeratio, 
                    sectorF.startAngle, sectorF.sweepAngle);
            }
            else if (regionEx?.Region is Region)
            {
                Region unionRegion = (Region)regionEx?.Region;
                //RectangleF rectangleF = unionRegion.GetBounds(graphics);
               
                //Matrix matrix = new Matrix();
                //matrix.Scale(1/sizeratio, 1/sizeratio);
                //unionRegion.Transform(matrix);
 
                //RectangleF rectangleF2= unionRegion.GetBounds(graphics);
 
                graphics.FillRegion(Brushes.Orange, unionRegion);
             
            }
            else
                ;
        }

b). 模板創建

選擇穩定唯一的形狀特征,設置合適的參數,用來生成模板,此基礎版生成的特征為閉合的輪廓,后期版本會推出非閉合的多輪廓形狀匹配算法。

模板創建代碼如下:

         //創建模板
        private void btncreateModel_Click(object sender, EventArgs e)
        {
            if (GrabImg == null || GrabImg.Width <= 0)
            {
                MessageBox.Show("未獲取圖像");
                return;
            }
 
            List<RectangleF> roiList = currvisiontool.getRoiList<RectangleF>();
            if (roiList.Count <= 0)
            {
                MessageBox.Show("請設置模板創建區域{矩形}");
                return;
 
            }
            if (MessageBox.Show("確認創建新模板?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                                  == DialogResult.Yes)
            {
                CVRect cVRect = new CVRect((int)roiList[0].X, (int)roiList[0].Y, (int)roiList[0].Width, (int)roiList[0].Height);
                Mat tp = MatExtension.Crop_Mask_Mat(GrabImg, cVRect);
 
                templateContour = null;
                coutourLen = 100;
                NumMincoutourLen.Value=100;
                contourArea = 100;
                NumMinContourArea.Value=100;
                double modelx = 0, modely = 0;
 
 
                runTool = new ShapeMatchTool();
                parmaData = new ShapeMatchData();
                (parmaData as ShapeMatchData).Segthreshold = (double)NumSegthreshold.Value;
 
                modeltp = (runTool as ShapeMatchTool).CreateTemplateContours(tp,
                     (parmaData as ShapeMatchData).Segthreshold, cVRect,
                    ref templateContour,
                    ref coutourLen, ref contourArea, ref modelx, ref modely, ref modelangle);
 
                picTemplate.Image = BitmapConverter.ToBitmap(modeltp);
                if (templateContour == null)
                {
                    MessageBox.Show("模板創建失敗!");
                    return;
                }
                modelx += cVRect.X;
                modely += cVRect.Y;
                lIstModelInfo.Items.Clear();
                lIstModelInfo.Items.Add(new ListViewItem(
                    new string[] { "BaseX", modelx.ToString("f3") }));
                lIstModelInfo.Items.Add(new ListViewItem(
                  new string[] { "BaseY", modely.ToString("f3") }));
                lIstModelInfo.Items.Add(new ListViewItem(
                  new string[] { "BaseAngle", modelangle.ToString("f3") }));
                lIstModelInfo.Items.Add(new ListViewItem(
                 new string[] { "ContourLength", coutourLen.ToString("f3") }));
                lIstModelInfo.Items.Add(new ListViewItem(
                 new string[] { "ContourArea", contourArea.ToString("f3") }));
 
                modelOrigion = string.Format("{0},{1},{2}",
                      modelx.ToString("f3"),
                          modely.ToString("f3"),
                              modelangle.ToString("f3"));
 
              if(coutourLen * 0.8> (double)NumMincoutourLen.Maximum||
                    contourArea * 0.8> (double)NumMinContourArea.Maximum)
                {
                    MessageBox.Show("模板創建完成失敗,模板區域過大!");
                    return;
                }
                NumMincoutourLen.Value = (decimal)(coutourLen *0.8);
                NumMaxcoutourLen.Value = (decimal)(coutourLen *1.2);
            
                NumMinContourArea.Value = (decimal)(contourArea * 0.8);
                NumMaxContourArea.Value = (decimal)(contourArea * 1.2);
 
                NumMatchValue.Value = (decimal)0.5;
                MessageBox.Show("模板創建完成!");
            }
 
        }

c). 模板匹配

多角度輪廓匹配算法,同時通過鉅來獲取中心,和角度

  //模板匹配
        void TestModelMatch()
        {
            if (GrabImg == null || GrabImg.Width <= 0)
            {
                stuModelMatchData.runFlag = false;
                MessageBox.Show("未獲取圖像");
                return;
            }
 
            if (templateContour == null)
            {
                stuModelMatchData.runFlag = false;
                MessageBox.Show("模板不存在,請先創建模板!");
                return;
            }
            runTool = new ShapeMatchTool();
            parmaData = new ShapeMatchData();
            (parmaData as ShapeMatchData).tpContour = templateContour;
            (parmaData as ShapeMatchData).Segthreshold = (double)NumSegthreshold.Value;
            (parmaData as ShapeMatchData).MatchValue = (double)NumMatchValue.Value;
            (parmaData as ShapeMatchData).MincoutourLen = (int)NumMincoutourLen.Value;
            (parmaData as ShapeMatchData).MaxcoutourLen = (int)NumMaxcoutourLen.Value;
            (parmaData as ShapeMatchData).MinContourArea = (int)NumMinContourArea.Value;
            (parmaData as ShapeMatchData).MaxContourArea = (int)NumMaxContourArea.Value;
            (parmaData as ShapeMatchData).baseAngle = modelangle;
 
 
            ResultOfToolRun = runTool.Run<ShapeMatchData>(GrabImg, parmaData as ShapeMatchData);
 
            currvisiontool.clearAll();
            currvisiontool.dispImage(ResultOfToolRun.resultToShow);
 
            ShapeMatchResult shapeMatchResult = ResultOfToolRun as ShapeMatchResult;
 
            if (shapeMatchResult.scores.Count <= 0)
            {
                currvisiontool.DrawText(new TextEx("模板匹配失敗!") {x=1000,y=10, brush = new SolidBrush(Color.Red) });
 
                currvisiontool.AddTextBuffer(new TextEx("模板匹配失敗!") { x = 1000, y = 10, brush = new SolidBrush(Color.Red) });
 
                stuModelMatchData.runFlag = false;
                return;
            }
            currvisiontool.DrawText(new TextEx("得分:" + shapeMatchResult.scores[0].ToString("f3")));
            currvisiontool.AddTextBuffer(new TextEx("得分:" + shapeMatchResult.scores[0].ToString("f3")));
 
            currvisiontool.DrawText(new TextEx("偏轉角度:" + shapeMatchResult.rotations[0].ToString("f3")) { x = 10, y = 100 });
            currvisiontool.AddTextBuffer(new TextEx("偏轉角度:" + shapeMatchResult.rotations[0].ToString("f3")) { x = 10, y = 100 });
 
            currvisiontool.DrawText(new TextEx(string.Format("匹配點位X:{0},Y:{1}", shapeMatchResult.positions[0].X.ToString("f3"),
                shapeMatchResult.positions[0].Y.ToString("f3")))
            { x = 10, y = 200 });
            currvisiontool.AddTextBuffer(new TextEx(string.Format("匹配點位X:{0},Y:{1}", shapeMatchResult.positions[0].X.ToString("f3"),
                shapeMatchResult.positions[0].Y.ToString("f3")))
            { x = 10, y = 200 });
 
            stuModelMatchData.matchPoint = shapeMatchResult.positions[0];
            stuModelMatchData.matchOffsetAngle = shapeMatchResult.rotations[0];
            stuModelMatchData.matchScore = shapeMatchResult.scores[0];
            stuModelMatchData.runFlag = true;
 
 
        }

3. 關鍵部位代碼如下,包含模板創建,模板多角度匹配等

a)創建形狀輪廓模板核心代碼如下:

        /// <summary>
        /// 創建形狀輪廓模板
        /// </summary>
        /// <param name="img_template">模板圖像</param>
        ///  <param name="Segthreshold">分割閾值</param>
        /// <param name="templateContour">模板輪廓</param>
        /// <param name="coutourLen">模板輪廓長度</param>
        /// <param name="contourArea">模板輪廓面積</param>
        ///  <param name="modelx">模板輪廓X</param>
        ///   <param name="modely">模板輪廓Y</param>
        ///    <param name="modelangle">模板輪廓角度</param>
        /// <returns>返回繪制圖</returns>
        public Mat CreateTemplateContours(Mat img_template,double Segthreshold, CVRect boundingRect,
            ref CVPoint[] templateContour, ref double coutourLen, ref double contourArea,
            ref double modelx,ref double modely,ref double modelangle)
        {
            //灰度化
            //Mat gray_img_template = new Mat();
            //Cv2.CvtColor(img_template, gray_img_template, ColorConversionCodes.BGR2GRAY);
 
            //閾值分割
            Mat thresh_img_template = new Mat();
            Cv2.Threshold(img_template, thresh_img_template, Segthreshold, 255, ThresholdTypes.Binary);
            //開運算處理,提出白色噪點
            Mat ellipse = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(3, 3));   
            Cv2.MorphologyEx(thresh_img_template, thresh_img_template, MorphTypes.Open, ellipse);
 
            //Mat cannyMat = new Mat();
            //Cv2.Canny(thresh_img_template, cannyMat, Segthreshold, 255);
 
            //尋找邊界
            CVPoint[][] contours_template;
            //Vector<Vector<CVPoint>> contours_template=new Vector<Vector<CVPoint>>();
            //Vector<Vec4i> hierarchy=new Vector<Vec4i>();
        //    HierarchyIndex[] hierarchy;
            Cv2.FindContours(thresh_img_template, out contours_template, out _, RetrievalModes.Tree,
                ContourApproximationModes.ApproxNone);
 
            CVPoint[][] ExceptContours = ContourOperate.ExceptBoundPoints(img_template.BoundingRect(), contours_template);
            
            int count = ExceptContours.ToList<CVPoint[]>().Count;
            List<CVPoint[]> ModelContours=new List<CVPoint[]>();
        
            for (int i=0;i< count; i++)
            {
                if(Cv2.ContourArea(ExceptContours[i])>= contourArea&&
                    Cv2.ArcLength(ExceptContours[i],false)>= coutourLen)
                //if (ExceptContours[i].Length > 30)//至少30點有效
                    ModelContours.Add(ExceptContours[i]);
            }
            ModelContours = ModelContours.OrderByDescending(s => s.Length).ToList();
            //繪制邊界
            Mat dst = new Mat();
            Cv2.CvtColor(img_template, dst, ColorConversionCodes.GRAY2BGR);
            if(ModelContours.Count>0)
            {
                Cv2.DrawContours(dst, ModelContours, 0, new Scalar(0, 0, 255));
                //獲取重心點
                Moments M;
                M = Cv2.Moments(ModelContours[0]);
                double cX = (M.M10 / M.M00);
                double cY = (M.M01 / M.M00);
                
                float a = (float)(M.M20 / M.M00 - cX * cX);
                float b = (float)(M.M11 / M.M00 - cX * cY);
                float c = (float)(M.M02 / M.M00 - cY * cY);
                //計算角度(0~180)
              //  double tanAngle = Cv2.FastAtan2(2 * b, (a - c)) / 2;
 
                //計算角度2(-90~90)
             //   double ang = (Math.Atan2(2 * b, (a - c)) * 180 / Math.PI) / 2;
 
                //double ang2=  Cv2.MinAreaRect(ModelContours[0]).Angle;
 
                //if (tanAngle > 90)
                //    tanAngle -= 180;
                //當前輪廓旋轉矩
                RotatedRect currrect = Cv2.MinAreaRect(ModelContours[0]);
                //繪制旋轉矩形
                   dst.DrawRotatedRect(currrect, Scalar.Lime);
 
                //繪制目標邊界
                Cv2.DrawContours(dst, ModelContours, 0, new Scalar(0, 0, 255));
                //顯示目標中心
                dst.drawCross(new CVPoint((int)cX, (int)cY),
                       new Scalar(0, 255, 0), 10, 2);
                //
 
 
                //CVPoint[] HullP = Cv2.ConvexHull(ModelContours[0], true);//順時針方向
 
                //List<CVPoint[]> HullPList = new List<CVPoint[]>();
 
                //HullPList.Add(HullP);
 
                Cv2.Polylines(dst, HullPList, true, Scalar.Red);
 
                //Point2f cVPoint = CalBestDisP(new Point2d(cX, cY), HullP);
 
                //double ang3 = ang;
 
                //if(!(cVPoint.X==0&& cVPoint.Y == 0))
    //            {
                //    //計算角度2(-180~180)
                //    ang3 = calAngleOfLx(cX, cY, cVPoint.X, cVPoint.Y);
                //    Cv2.Line(dst, (int)cX, (int)cY, (int)cVPoint.X, (int)cVPoint.Y, Scalar.DarkOrange);
                //}
                            
                //輪廓點位
                modelx = cX;
                modely = cY;
                modelangle = currrect.Angle;
 
                //輪廓長度
                coutourLen = Cv2.ArcLength(ModelContours[0],false);
                contourArea = Cv2.ContourArea(ModelContours[0]);
                templateContour = ModelContours[0];
            }    
            else
            {
                //輪廓點位
                modelx = 0;
                modely = 0;
                modelangle = 0;
 
                //輪廓長度
                coutourLen = 0;
                contourArea = 0;
                templateContour =null;
            }
            return dst;
        }

b)形狀多角度匹配核心算法如下:

    /// <summary>
        /// 形狀匹配
        /// </summary>
        /// <param name="image">輸入圖像</param>
        /// <param name="imgTemplatecontours">模板輪廓</param>
        ///  <param name="Segthreshold">分割閾值</param>
        /// <param name="MatchValue">匹配值</param>
        /// <param name="MincoutourLen">輪廓最小長度</param>
        /// <param name="MaxcoutourLen">輪廓最大長度</param>
        /// <param name="MinContourArea">輪廓最小面積</param>
        /// <param name="MaxContourArea">輪廓最大面積</param>
        /// <param name="shapeMatchResult">匹配結果</param>
        /// <param name="isMultipleTemplates">是否使用多模板</param>
        /// <returns>返回繪制圖</returns>
        bool ShapeTemplateMatch(Mat image, CVPoint[] imgTemplatecontours, double Segthreshold,
            double MatchValue, int MincoutourLen, int MaxcoutourLen,
             double MinContourArea, double MaxContourArea,  double baseAngle,
             ref ShapeMatchResult shapeMatchResult,
             bool isMultipleTemplates=false)
        {
        
            //List<Point2d> image_coordinates = new List<Point2d>();
            //灰度化
            //Mat gray_img=new Mat();
            //Cv2.CvtColor(image, gray_img, ColorConversionCodes.BGR2GRAY);
            Mat dst = new Mat();
            Cv2.CvtColor(image, dst, ColorConversionCodes.GRAY2BGR);
            //閾值分割
            Mat thresh_img = new Mat();
            Cv2.Threshold(image, thresh_img, Segthreshold, 255, ThresholdTypes.Binary);
 
 
            #region------此處增加與模板創建時候同樣的圖像處理--------
            //開運算處理,提出白色噪點
            Mat ellipse = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(3, 3));
    
            Cv2.MorphologyEx(thresh_img, thresh_img, MorphTypes.Open, ellipse);
            #endregion
 
 
            //Mat cannyMat = new Mat();
            //Cv2.Canny(thresh_img, cannyMat, Segthreshold, 255);
 
            //尋找邊界
            CVPoint[][] contours_img;
            //HierarchyIndex[] hierarchy;
            Cv2.FindContours(thresh_img, out contours_img, out _, RetrievalModes.Tree,
                 ContourApproximationModes.ApproxNone);
            //根據形狀模板進行匹配
            int min_pos = -1;
            double min_value = MatchValue;//匹配分值
            List<CVPoint[]> points = contours_img.ToList<CVPoint[]>();
 
            //List<double> angleList = new List<double>();
            for (int i = 0; i < points.Count; i++)
            {
                //計算輪廓面積,篩選掉一些沒必要的小輪廓
                if (Cv2.ContourArea(contours_img[i]) < MinContourArea ||
                              Cv2.ContourArea(contours_img[i]) > MaxContourArea)
                    continue;
                //輪廓長度不達標            
                if (Cv2.ArcLength(contours_img[i], false) < MincoutourLen ||
                              Cv2.ArcLength(contours_img[i], false) > MaxcoutourLen)
                    continue;
 
                //得到匹配分值 ,值越小相似度越高
                double value = Cv2.MatchShapes(contours_img[i], imgTemplatecontours,
                                                           ShapeMatchModes.I3, 0.0);
                value = 1 - value;
 
                //將匹配分值與設定分值進行比較 
                if (value >= min_value)
                {
                    min_pos = i;
 
                    //將目標的得分都存在數組中 
                    shapeMatchResult.scores.Add(value);
                    //匹配到的輪廓
                    shapeMatchResult.contours.Add(contours_img[min_pos]);
                    /*----------------*/
                }
                                
            }
            /*----------------*/
            int count = shapeMatchResult.scores.Count;
            if(count<=0)
            {
                shapeMatchResult.resultToShow = dst;
                shapeMatchResult.exceptionInfo = "模板匹配失敗!";
                return false;
            }
 
            if (isMultipleTemplates)
            {
                for (int j = 0; j < count; j++)
                {
                    //繪制目標邊界
                    Cv2.DrawContours(dst, shapeMatchResult.contours, j, new Scalar(0, 0, 255));
                    //得分繪制
                    Cv2.PutText(dst,
                        string.Format("Score:{0};Angle:{1}", shapeMatchResult.scores[j].ToString("F3"),
                        shapeMatchResult.rotations[j].ToString("F3")),
                             //anglebuf[j].ToString("F3")),
                             new CVPoint(shapeMatchResult.contours[j][0].X + 10, shapeMatchResult.contours[j][0].Y - 10),
                                        HersheyFonts.HersheyDuplex, 1, Scalar.Yellow);
                    //顯示目標中心并提取坐標點
                    dst.drawCross(new CVPoint((int)shapeMatchResult.positions[j].X, (int)shapeMatchResult.positions[j].Y),
                           new Scalar(0, 255, 0), 10, 2);
                    //當前輪廓旋轉矩
                    RotatedRect currrect = Cv2.MinAreaRect(shapeMatchResult.contours[j]);
 
                    dst.DrawRotatedRect(currrect, Scalar.Lime);
                }
            }
            else
            {
                double bestScore=  shapeMatchResult.scores.Max();    //最佳得分
                int index = shapeMatchResult.scores.FindIndex(s=>s== bestScore);
              //  double bestangle = shapeMatchResult.rotations[index]; //最佳角度                
            //    Point2d bestpos = shapeMatchResult.positions[index]; //最佳點位
                CVPoint[] bestcontour= shapeMatchResult.contours[index]; //最佳輪廓            
            
                //繪制目標邊界
                Cv2.DrawContours(dst, shapeMatchResult.contours, index, new Scalar(0, 0, 255));
            
                //獲取重心點                
                Moments M = Cv2.Moments(bestcontour);
                double cX = (M.M10 / M.M00);
                double cY = (M.M01 / M.M00);
 
                float a = (float)(M.M20 / M.M00 - cX * cX);
                float b = (float)(M.M11 / M.M00 - cX * cY);
                float c = (float)(M.M02 / M.M00 - cY * cY);
                //計算角度(0~180)
               // double tanAngle = Cv2.FastAtan2(2 * b, (a - c)) / 2;
                //angleList.Add(tanAngle);
 
                //計算角度2(-90~90)
                //double ang = (Math.Atan2(2 * b, (a - c)) * 180 / Math.PI) / 2;
 
                #region----角度計算方式2---
                //-90~90度
                //由于先驗目標最小包圍矩形是長方形   
                //因此最小包圍矩形的中心和重心的向量夾角為旋轉
                RotatedRect rect_template = Cv2.MinAreaRect(imgTemplatecontours);
                RotatedRect rect_search = Cv2.MinAreaRect(bestcontour);
                //兩個旋轉矩陣是否同向
                float sign = (rect_template.Size.Width - rect_template.Size.Height) * 
                                  (rect_search.Size.Width - rect_search.Size.Height);
                float angle=0;
                if (sign > 0)
                    // 可以直接相減
                    angle = rect_search.Angle - rect_template.Angle;
                else
                    angle = (90 + rect_search.Angle) - rect_template.Angle;
 
                if (angle > 90)
                    angle -= 180;
                #endregion
 
 
                    //顯示目標中心并提取坐標點
                dst.drawCross(new CVPoint((int)cX, (int)cY),
                            new Scalar(0, 255, 0), 10, 2);
                //當前輪廓旋轉矩
                RotatedRect currrect = Cv2.MinAreaRect(bestcontour);
                //繪制旋轉矩形
                dst.DrawRotatedRect(currrect, Scalar.Lime);
           
                //CVPoint[] HullP = Cv2.ConvexHull(bestcontour, true);//順時針方向
 
                //List<CVPoint[]> HullPList = new List<CVPoint[]>();
 
                //HullPList.Add(HullP);
 
                //Cv2.Polylines(dst, HullPList, true, Scalar.Red);
 
                //Point2f cVPoint = CalBestDisP(new Point2d(cX, cY), HullP);
 
                //double ang3 = ang;
 
                //if (!(cVPoint.X == 0 && cVPoint.Y == 0))
                //{
                //    //計算角度2(-180~180)
                //    ang3 = calAngleOfLx(cX, cY, cVPoint.X, cVPoint.Y);
                //    Cv2.Line(dst, (int)cX, (int)cY, (int)cVPoint.X, (int)cVPoint.Y, Scalar.DarkOrange);
                //}
            
                //double offsetA = ang3 - baseAngle;//偏轉角
                //if (offsetA < -180)
                //    offsetA += 360;
                //else if (offsetA > 180)
                //    offsetA -= 360;
 
                    //得分繪制
                //Cv2.PutText(dst,
                //    string.Format("Score:{0};Angle:{1}", bestScore.ToString("F3"),
                //              ang3.ToString("F3")),
                //         new CVPoint(shapeMatchResult.contours[index][0].X + 10, shapeMatchResult.contours[index][0].Y - 10),
                //                    HersheyFonts.HersheyDuplex, 1, Scalar.Yellow);
 
                
                shapeMatchResult.positions.Clear();
                shapeMatchResult.rotations.Clear();
                shapeMatchResult.scores.Clear();
                shapeMatchResult.contours.Clear();
                //將目標的重心坐標都存在數組中 
                shapeMatchResult.positions.Add(new Point2d(cX, cY));//向數組中存放點的坐標
                                                                    
                shapeMatchResult.rotations.Add(angle);//將偏轉角度都存在數組中 
                                                         
                shapeMatchResult.scores.Add(bestScore);//將目標的得分都存在數組中 
                                                      
                shapeMatchResult.contours.Add(bestcontour); //匹配到的輪廓
                /*----------------*/
            }
 
            shapeMatchResult.resultToShow = dst;
            return true;
        }

關于“怎么用C# OpenCV實現形狀匹配”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用C# OpenCV實現形狀匹配”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武鸣县| 师宗县| 龙山县| 稷山县| 依兰县| 容城县| 北川| 思南县| 治县。| 巨野县| 宜兰县| 清新县| 客服| 新密市| 通城县| 太白县| 南和县| 金坛市| 吴川市| 麻阳| 龙江县| 临邑县| 宜阳县| 韶关市| 卫辉市| 旌德县| 台前县| 金门县| 通州市| 新河县| 南郑县| 辉县市| 乌拉特后旗| 乌审旗| 榆树市| 浪卡子县| 河池市| 绍兴县| 平山县| 商水县| 新巴尔虎右旗|