示例#1
0
 /// <summary>
 /// Initializes line iterator
 /// </summary>
 /// <param name="image">Image to sample the line from.  </param>
 /// <param name="pt1">First ending point of the line segment. </param>
 /// <param name="pt2">Second ending point of the line segment. </param>
 /// <param name="connectivity">The scanned line connectivity, 4 or 8. </param>
 /// <param name="leftToRight">The flag, indicating whether the line should be always scanned from the left-most point to the right-most out of pt1 and pt2 (leftToRight=true), or it is scanned in the specified order, from pt1 to pt2 (leftToRight=false). </param>
 /// <returns>The function cvInitLineIterator initializes the line iterator and returns the number of pixels between two end points. Both points must be inside the image. After the iterator has been initialized, all the points on the raster line that connects the two ending points may be retrieved by successive calls of NextLinePoint point. The points on the line are calculated one by one using 4-connected or 8-connected Bresenham algorithm.</returns>
 private void Initialize(CvArr image, CvPoint pt1, CvPoint pt2, PixelConnectivity connectivity, bool leftToRight)
 {
     this.image        = image;
     this.pt1          = pt1;
     this.pt2          = pt2;
     this.connectivity = connectivity;
     this.leftToRight  = leftToRight;
     this.count        = NativeMethods.cvInitLineIterator(image.CvPtr, pt1, pt2, this.CvPtr, connectivity, leftToRight);
 }
示例#2
0
        /// <summary>
        /// ラインイテレータを初期化する
        /// </summary>
        /// <param name="image">対象画像</param>
        /// <param name="pt1">線分の一つ目の端点</param>
        /// <param name="pt2">線分のニつ目の端点</param>
        /// <param name="connectivity">走査した線分の接続性.4または8</param>
        /// <param name="left_to_right">pt1とpt2とは無関係に線分をいつも左から右に走査する(true)か, pt1からpt2への決まった方向で走査するか(false)を指定するフラグ. </param>
        /// <returns></returns>
#else
        /// <summary>
        /// Initializes line iterator
        /// </summary>
        /// <param name="image">Image to sample the line from.  </param>
        /// <param name="pt1">First ending point of the line segment. </param>
        /// <param name="pt2">Second ending point of the line segment. </param>
        /// <param name="connectivity">The scanned line connectivity, 4 or 8. </param>
        /// <param name="left_to_right">The flag, indicating whether the line should be always scanned from the left-most point to the right-most out of pt1 and pt2 (left_to_right=true), or it is scanned in the specified order, from pt1 to pt2 (left_to_right=false). </param>
        /// <returns>The function cvInitLineIterator initializes the line iterator and returns the number of pixels between two end points. Both points must be inside the image. After the iterator has been initialized, all the points on the raster line that connects the two ending points may be retrieved by successive calls of NextLinePoint point. The points on the line are calculated one by one using 4-connected or 8-connected Bresenham algorithm.</returns>
#endif
        public CvLineIterator(CvArr image, CvPoint pt1, CvPoint pt2, PixelConnectivity connectivity, bool left_to_right)
            : this()
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            Initialize(image, pt1, pt2, connectivity, left_to_right);
        }
示例#3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="img"></param>
 /// <param name="pt1"></param>
 /// <param name="pt2"></param>
 /// <param name="connectivity"></param>
 /// <param name="leftToRight"></param>
 /// <returns></returns>
 public LineIterator(
     Mat img,
     Point pt1,
     Point pt2,
     PixelConnectivity connectivity = PixelConnectivity.Connectivity8,
     bool leftToRight = false)
 {
     this.img          = img ?? throw new ArgumentNullException(nameof(img));
     this.pt1          = pt1;
     this.pt2          = pt2;
     this.connectivity = connectivity;
     this.leftToRight  = leftToRight;
 }
        public void Run(PixelConnectivity connectivity, ConnectedComponentsAlgorithmsTypes algorithmType)
        {
            using var src    = Image("alphabet.png", ImreadModes.Grayscale);
            using var binary = new Mat();
            Cv2.Threshold(src, binary, 128, 255, ThresholdTypes.BinaryInv);
            ShowImagesWhenDebugMode(src, binary);

            var cc = Cv2.ConnectedComponentsEx(binary, connectivity, algorithmType);

            Assert.Equal(27, cc.LabelCount);
            Assert.NotEmpty(cc.Labels.GetBuffer());
            Assert.Equal(src.Rows, cc.Labels.GetLength(0));
            Assert.Equal(src.Cols, cc.Labels.GetLength(1));

            using var render = new Mat();
            cc.RenderBlobs(render);
            ShowImagesWhenDebugMode(render);
        }
示例#5
0
        /// <summary>
        /// ラインイテレータを初期化する
        /// </summary>
        /// <param name="image">対象画像</param>
        /// <param name="pt1">線分の一つ目の端点</param>
        /// <param name="pt2">線分のニつ目の端点</param>
        /// <param name="connectivity">走査した線分の接続性.4または8</param>
        /// <returns></returns>
#else
        /// <summary>
        /// Initializes line iterator
        /// </summary>
        /// <param name="image">Image to sample the line from.  </param>
        /// <param name="pt1">First ending point of the line segment. </param>
        /// <param name="pt2">Second ending point of the line segment. </param>
        /// <param name="connectivity">The scanned line connectivity, 4 or 8. </param>
        /// <returns>The function cvInitLineIterator initializes the line iterator and returns the number of pixels between two end points. Both points must be inside the image. After the iterator has been initialized, all the points on the raster line that connects the two ending points may be retrieved by successive calls of NextLinePoint point. The points on the line are calculated one by one using 4-connected or 8-connected Bresenham algorithm.</returns>
#endif
        public CvLineIterator(CvArr image, CvPoint pt1, CvPoint pt2, PixelConnectivity connectivity)
            : this(image, pt1, pt2, connectivity, false)
        {
        }
示例#6
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);
            }
        }
示例#7
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 image</param>
        /// <param name="stats">statistics output for each label, including the background label, 
        /// see below for available statistics. Statistics are accessed via stats(label, COLUMN) 
        /// where COLUMN is one of cv::ConnectedComponentsTypes</param>
        /// <param name="centroids">floating point centroid (x,y) output for each label, 
        /// including the background label</param>
        /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
        /// <param name="ltype">output image label type. Currently CV_32S and CV_16U are supported.</param>
        /// <returns></returns>
        public static int ConnectedComponentsWithStats(
            InputArray image, OutputArray labels,
            OutputArray stats, OutputArray centroids,
            PixelConnectivity connectivity,
            MatType ltype)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));
            if (labels == null)
                throw new ArgumentNullException(nameof(labels));
            if (stats == null)
                throw new ArgumentNullException(nameof(stats));
            if (centroids == null)
                throw new ArgumentNullException(nameof(centroids));
            image.ThrowIfDisposed();
            labels.ThrowIfNotReady();
            stats.ThrowIfNotReady();
            centroids.ThrowIfNotReady();

            int result = NativeMethods.imgproc_connectedComponentsWithStats(
                image.CvPtr, labels.CvPtr, stats.CvPtr, centroids.CvPtr, (int) connectivity, ltype);

            GC.KeepAlive(image);
            labels.Fix();
            stats.Fix();
            centroids.Fix();
            return result;
        }
示例#8
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 image</param>
 /// <param name="stats">statistics output for each label, including the background label, 
 /// see below for available statistics. Statistics are accessed via stats(label, COLUMN) 
 /// where COLUMN is one of cv::ConnectedComponentsTypes</param>
 /// <param name="centroids">floating point centroid (x,y) output for each label, 
 /// including the background label</param>
 /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <returns></returns>
 public static int ConnectedComponentsWithStats(
     InputArray image, OutputArray labels,
     OutputArray stats, OutputArray centroids,
     PixelConnectivity connectivity = PixelConnectivity.Connectivity8)
 {
     return ConnectedComponentsWithStats(image, labels, stats, centroids, connectivity, MatType.CV_32S);
 }
示例#9
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;
     }
 }
示例#10
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="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <returns></returns>
 public ConnectedComponents ConnectedComponentsEx(PixelConnectivity connectivity = PixelConnectivity.Connectivity8)
 {
     return Cv2.ConnectedComponentsEx(this, connectivity);
 }
示例#11
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="labels">destination labeled image</param>
 /// <param name="stats">statistics output for each label, including the background label, 
 /// see below for available statistics. Statistics are accessed via stats(label, COLUMN) 
 /// where COLUMN is one of cv::ConnectedComponentsTypes</param>
 /// <param name="centroids">floating point centroid (x,y) output for each label, 
 /// including the background label</param>
 /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <param name="ltype">output image label type. Currently CV_32S and CV_16U are supported.</param>
 /// <returns></returns>
 public int ConnectedComponentsWithStats(
     OutputArray labels, OutputArray stats, OutputArray centroids,
     PixelConnectivity connectivity, MatType ltype)
 {
     return Cv2.ConnectedComponentsWithStats(this, labels, stats, centroids, connectivity, ltype);
 }
示例#12
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="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 int ConnectedComponents(out int[,] labels, PixelConnectivity connectivity)
 {
     return Cv2.ConnectedComponents(this, out labels, connectivity);
 }
示例#13
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="labels">destination labeled image</param>
 /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <param name="ltype">output image label type. Currently CV_32S and CV_16U are supported.</param>
 /// <returns>The number of labels</returns>
 public int ConnectedComponents(OutputArray labels,
     PixelConnectivity connectivity, MatType ltype)
 {
     return Cv2.ConnectedComponents(this, labels, connectivity, ltype);
 }
示例#14
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="labels">destination labeled image</param>
 /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
 /// <returns>The number of labels</returns>
 public int ConnectedComponents(OutputArray labels,
     PixelConnectivity connectivity = PixelConnectivity.Connectivity8)
 {
     return ConnectedComponents(labels, connectivity, MatType.CV_32S);
 }