C# OpenCV labeling Segmentation

2021. 1. 12. 09:00OpenCV/OpenCV C#

반응형

Segmentation

세그먼트(Segment)란 서로 다른 두 점을 연결하는 가장 짧은 선을 의미한다.

세그먼테이션(Segmentation)이란 이미지에서 각각의 픽셀들을 분류해 그룹화하는 것을 의미한다.

예제 코드 (이진화)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Blob;

namespace ImageSegmentationLabeling
{
    class Program
    {
        static void Main(string[] args)
        {
            Mat src = new Mat("../../sample.png");
            Mat gray = new Mat();
            Mat binary = new Mat();

            Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(gray, binary, 150, 255, ThresholdTypes.Binary);

            Cv2.ImShow("src", src);
            Cv2.ImShow("dst", binary);
            Cv2.WaitKey(0);
        }
    }
}

 

결과

6

출처

https://076923.github.io/posts/C-opencv4-11/

 

Labeling

영상이나 이미지에서 인접한 화소들을 묶어 하나의 객체로 판단하는 방식이다.

예제 코드

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Blob;

namespace ImageSegmentationLabeling
{
    class Program
    {
        static void Main(string[] args)
        {
            Mat src = new Mat("../../sample.png"); 
            Mat bin = new Mat(); 

            Cv2.CvtColor(src, bin, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(bin, bin, 0, 255, ThresholdTypes.Binary);

            Cv2.ImShow("src", src); 

            Mat result = new Mat(src.Size(), MatType.CV_8UC3); 
            CvBlobs blobs = new CvBlobs(); 
            blobs.Label(bin); 
            blobs.RenderBlobs(src, result); 
            foreach (var item in blobs) 
            { 
                CvBlob b = item.Value; Cv2.Circle(result, b.Contour.StartingPoint, 4, Scalar.Red, 2, LineTypes.AntiAlias);
                Cv2.PutText(result, b.Label.ToString(), new Point(b.Centroid.X, b.Centroid.Y),
                    HersheyFonts.HersheyComplex, 1, Scalar.Yellow, 2, LineTypes.AntiAlias);
            }
            Cv2.ImShow("result", result); Cv2.WaitKey(0);

        }
    }
}

 

결과

5

출처

코드

https://076923.github.io/posts/C-opencv4-16/

https://shalchoong.tistory.com/54

이미지

https://076923.github.io/posts/C-opencv-32/

반응형

'OpenCV > OpenCV C#' 카테고리의 다른 글

C# OpenCV labeling  (0) 2021.01.19
C# OpenCV Transformation  (0) 2021.01.17
C# OpenCV histogram gray color BGR 그리기  (0) 2021.01.09
C# OpenCV 이미지 전처리  (0) 2021.01.06
C# OpenCV 설치하기 (OpenCvSharp4 최신버전 설치)  (1) 2021.01.05