Пример #1
0
        //Find outer defect
        static void FindContour_and_outer_defect(Mat img, List <Point[]> contours_final, ref int nLabels, out int [,] stats, string mode)
        {
            // variable
            OpenCvSharp.Point[][] temp = new Point[1][];
            //0: 內圈 ; 1: 外圈
            OpenCvSharp.Point[] contour_now;
            if (mode == "inner")
            {
                contour_now = contours_final[0];
            }
            else
            {
                contour_now = contours_final[1];
            }
            // Convex hull


            var ellipsecontour = Cv2.FitEllipse(contour_now);

            Mat convex_mask_img = Mat.Zeros(img.Size(), MatType.CV_8UC1);

            Cv2.Ellipse(convex_mask_img, ellipsecontour, 255, -1);


            // Contour
            temp[0] = contour_now;
            Mat contour_mask_img = Mat.Zeros(img.Size(), MatType.CV_8UC1);

            Cv2.DrawContours(contour_mask_img, temp, -1, 255, -1);


            Mat diff_image = contour_mask_img ^ convex_mask_img;


            //Opening
            Mat kernel = Mat.Ones(4, 4, MatType.CV_8UC1);//改變凹角大小

            diff_image = diff_image.MorphologyEx(MorphTypes.Open, kernel);


            //=========================吃掉邊界=======================================
            //temp[0] = contour_now;
            //Cv2.DrawContours(diff_image, temp, -1, 0, 3);
            //================================================================
            convex_mask_img.SaveImage("./" + mode + "convex" + ".jpg");
            contour_mask_img.SaveImage("./" + mode + "contour" + ".jpg");
            diff_image.SaveImage("./" + mode + "mask" + ".jpg");
            //Connected Component
            var labelMat     = new MatOfInt();
            var statsMat     = new MatOfInt();// Row: number of labels Column: 5
            var centroidsMat = new MatOfDouble();

            nLabels = Cv2.ConnectedComponentsWithStats(diff_image, labelMat, statsMat, centroidsMat);

            var labels = labelMat.ToRectangularArray();

            stats = statsMat.ToRectangularArray();
            var centroids = centroidsMat.ToRectangularArray();
        }
Пример #2
0
        /// <summary>
        /// computes the connected components labeled image of boolean image. 
        /// image with 4 or 8 way connectivity - returns N, the total number of labels [0, N-1] where 0 
        /// represents the background label. ltype specifies the output label image type, an important 
        /// consideration based on the total number of labels or alternatively the total number of 
        /// pixels in the source image.
        /// </summary>
        /// <param name="image">the image to be labeled</param>
        /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
        /// <returns></returns>
        public static ConnectedComponents ConnectedComponentsEx(
            InputArray image, PixelConnectivity connectivity = PixelConnectivity.Connectivity8)
        {
            using (var labelsMat = new MatOfInt())
            using (var statsMat = new MatOfInt())
            using (var centroidsMat = new MatOfDouble())
            {
                int nLabels = ConnectedComponentsWithStats(
                    image, labelsMat, statsMat, centroidsMat, connectivity, MatType.CV_32S);
                var labels = labelsMat.ToRectangularArray();
                var stats = statsMat.ToRectangularArray();
                var centroids = centroidsMat.ToRectangularArray();

                var blobs = new ConnectedComponents.Blob[nLabels];
                for (int i = 0; i < nLabels; i++)
                {
                    blobs[i] = new ConnectedComponents.Blob
                    {
                        Label = i,
                        Left = stats[i, 0],
                        Top = stats[i, 1],
                        Width = stats[i, 2],
                        Height = stats[i, 3],
                        Area = stats[i, 4],
                        Centroid = new Point2d(centroids[i, 0], centroids[i, 1]),
                    };
                }
                return new ConnectedComponents(blobs, labels, nLabels);
            }
        }
Пример #3
0
 /// <summary>
 /// computes the connected components labeled image of boolean image. 
 /// image with 4 or 8 way connectivity - returns N, the total number of labels [0, N-1] where 0 
 /// represents the background label. ltype specifies the output label image type, an important 
 /// consideration based on the total number of labels or alternatively the total number of 
 /// pixels in the source image.
 /// </summary>
 /// <param name="image">the image to be labeled</param>
 /// <param name="labels">destination labeled rectangular array</param>
 /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <returns>The number of labels</returns>
 public static int ConnectedComponents(InputArray image, out int[,] labels, PixelConnectivity connectivity)
 {
     using (var labelsMat = new MatOfInt())
     {
         int result = ConnectedComponents(image, labelsMat, connectivity, MatType.CV_32S);
         labels = labelsMat.ToRectangularArray();
         return result;
     }
 }