DrawContours() public static method

draws contours in the image
public static DrawContours ( OpenCvSharp.InputOutputArray image, IEnumerable contours, int contourIdx, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, OpenCvSharp.Mat hierarchy = null, int maxLevel = Int32.MaxValue, System.Point offset = null ) : void
image OpenCvSharp.InputOutputArray Destination image.
contours IEnumerable All the input contours. Each contour is stored as a point vector.
contourIdx int Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
color Scalar Color of the contours.
thickness int Thickness of lines the contours are drawn with. If it is negative (for example, thickness=CV_FILLED ), /// the contour interiors are drawn.
lineType LineTypes Line connectivity.
hierarchy OpenCvSharp.Mat Optional information about hierarchy. It is only needed if you want to draw only some of the contours
maxLevel int Maximal level for drawn contours. If it is 0, only the specified contour is drawn. /// If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, /// all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account /// when there is hierarchy available.
offset System.Point Optional contour shift parameter. Shift all the drawn contours by the specified offset = (dx, dy)
return void
コード例 #1
0
        //OpenCVを使用して、座標を求める。
        void OpenCVTexture(Texture2D texture)
        {
            Mat newMat = Unity.TextureToMat(texture);

            //画像をCv2.Equalsで変化があるかグローバルのoldMatと比較して検知しようとしたが、できなかった。

            //Convert image to grayscale
            Mat imgGray = new Mat();

            Cv2.CvtColor(newMat, imgGray, ColorConversionCodes.BGR2GRAY);

            //Debug.Log(Cv2.Equals(imgGray, imgGray));


            // Clean up image using Gaussian Blur
            Mat imgGrayBlur = new Mat();

            Cv2.GaussianBlur(imgGray, imgGrayBlur, new Size(5, 5), 0);

            //Extract edges
            Mat cannyEdges = new Mat();

            Cv2.Canny(imgGrayBlur, cannyEdges, 10.0, 70.0);

            //Do an invert binarize the image
            Mat mask = new Mat();

            Cv2.Threshold(cannyEdges, mask, 70.0, 255.0, ThresholdTypes.BinaryInv);

            // Extract Contours
            Point[][]        contours;                                                                                                    //特徴点が格納される変数。
            HierarchyIndex[] hierarchy;                                                                                                   //特徴点の階層が格納される。
            Cv2.FindContours(cannyEdges, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, null); //特徴点を検出する。


            PointChangeNumSendUDP(contours);  //Pointが変化しなければ送信を実装しようとしている途中。
            //StartCoroutine(udpSendCoroutine(contours));


            //輪郭描画
            int width    = (int)transform.GetComponent <RectTransform>().sizeDelta.x;
            int height   = (int)transform.GetComponent <RectTransform>().sizeDelta.y;
            Mat Contours = new Mat(width, height, MatType.CV_8UC3, new Scalar(0, 0, 0)); //初期値として黒い画面を作成する。

            Cv2.DrawContours(Contours, contours, -1, new Scalar(0, 255, 0, 255), 1);     //MatにCountours(特徴点)を描画する。
            Texture2D changedTex = Unity.MatToTexture(Contours);                         //MatをTexture2Dへ変更

            GetComponent <RawImage>().texture = changedTex;                              //RaxImageにTexture2Dを書き込み。

            //MatをDisposeする。
            newMat.Dispose();
            imgGray.Dispose();
            imgGrayBlur.Dispose();
            cannyEdges.Dispose();
            mask.Dispose();
            Contours.Dispose();

            //TextureをDestryしないとメモリーリークを送りました。
            MonoBehaviour.Destroy(texture);
            if (changedTex != oldChangedTex)
            {
                MonoBehaviour.Destroy(oldChangedTex);
                oldChangedTex = changedTex;
            }
        }