C++ OpenCV Segmentation and Labelging

2021. 1. 11. 09:00OpenCV/OpenCV C++

반응형

1. image Segmentation

이미지 선명화를 위해 filter2D 사용

이진 이미지의 파생 표현을 얻기 위해 distanceTransform 사용

OpenCV 함수 cv :: watershed 를 사용하여 이미지의 개체를 배경에서 분리

실습 코드

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
    // Load the image
    Mat src = imread("coin.jpg");
    if (src.empty())
    {
        cout << "Could not open or find the image!\n" << endl;
        cout << "Usage: " << argv[0] << " <Input image>" << endl;
        return -1;
    }

    // Show source image
    imshow("Source Image", src);

    // 나중에 추출하는데 도움이되므로 배경을 흰색엣 검은 색으로 변경
    for (int i = 0; i < src.rows; i++) {
        for (int j = 0; j < src.cols; j++) {
            if (src.at<Vec3b>(i, j) == Vec3b(255, 255, 255))
            {
                src.at<Vec3b>(i, j)[0] = 0;
                src.at<Vec3b>(i, j)[1] = 0;
                src.at<Vec3b>(i, j)[2] = 0;
            }
        }
    }
    // Show output image
    imshow("Black Background Image", src);

    // 이미지를 선명하게하는 데 사용할 커널을 만든다.
    Mat kernel = (Mat_<float>(3, 3) <<
        1, 1, 1,
        1, -8, 1,
        1, 1, 1); // an approximation of second derivative, a quite strong kernel

// do the laplacian filtering as it is
// well, we need to convert everything in something more deeper then CV_8U
// because the kernel has some negative values,
// and we can expect in general to have a Laplacian image with negative values
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
// so the possible negative number will be truncated
    Mat imgLaplacian;
    filter2D(src, imgLaplacian, CV_32F, kernel);
    Mat sharp;
    src.convertTo(sharp, CV_32F);
    Mat imgResult = sharp - imgLaplacian;
    // convert back to 8bits gray scale
    imgResult.convertTo(imgResult, CV_8UC3);
    imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
    // imshow( "Laplace Filtered Image", imgLaplacian );
    imshow("New Sharped Image", imgResult);

    // Create binary image from source image
    Mat bw;
    cvtColor(imgResult, bw, COLOR_BGR2GRAY);
    threshold(bw, bw, 40, 255, THRESH_BINARY | THRESH_OTSU);
    imshow("Binary Image", bw);

    // Perform the distance transform algorithm
    Mat dist;
    distanceTransform(bw, dist, DIST_L2, 3);
    // Normalize the distance image for range = {0.0, 1.0}
    // so we can visualize and threshold it
    normalize(dist, dist, 0, 1.0, NORM_MINMAX);
    imshow("Distance Transform Image", dist);

    // Threshold to obtain the peaks
    // This will be the markers for the foreground objects
    threshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
    // Dilate a bit the dist image
    Mat kernel1 = Mat::ones(3, 3, CV_8U);
    dilate(dist, dist, kernel1);
    imshow("Peaks", dist);

    // Create the CV_8U version of the distance image
    // It is needed for findContours()
    Mat dist_8u;
    dist.convertTo(dist_8u, CV_8U);
    // Find total markers
    vector<vector<Point> > contours;
    findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    // Create the marker image for the watershed algorithm
    Mat markers = Mat::zeros(dist.size(), CV_32S);
    // Draw the foreground markers
    for (size_t i = 0; i < contours.size(); i++)
    {
        drawContours(markers, contours, static_cast<int>(i), Scalar(static_cast<int>(i) + 1), -1);
    }
    // Draw the background marker
    circle(markers, Point(5, 5), 3, Scalar(255), -1);
    imshow("Markers", markers * 10000);

    // Perform the watershed algorithm
    watershed(imgResult, markers);
    Mat mark;
    markers.convertTo(mark, CV_8U);
    bitwise_not(mark, mark);
    imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
    // image looks like at that point
    // Generate random colors
    vector<Vec3b> colors;
    for (size_t i = 0; i < contours.size(); i++)
    {
        int b = theRNG().uniform(0, 256);
        int g = theRNG().uniform(0, 256);
        int r = theRNG().uniform(0, 256);
        colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
    }
    // Create the result image
    Mat dst = Mat::zeros(markers.size(), CV_8UC3);
    // Fill labeled objects with random colors
    for (int i = 0; i < markers.rows; i++)
    {
        for (int j = 0; j < markers.cols; j++)
        {
            int index = markers.at<int>(i, j);
            if (index > 0 && index <= static_cast<int>(contours.size()))
            {
                dst.at<Vec3b>(i, j) = colors[index - 1];
            }
        }
    }
    // Visualize the final image
    imshow("Final Result", dst);
    waitKey();
    return 0;
}

 

결과

2

출처

코드

https://docs.opencv.org/3.4.12/d2/dbd/tutorial_distance_transform.html

이미지

http://www.ssacstat.com/base/cs/cs_05.php?com_board_basic=read_form&com_board_idx=543&topmenu=5&left=5&&com_board_search_code=&com_board_search_value1=&com_board_search_value2=&com_board_page=33&

 

2. Labeling

레이블링( labeling ) 이란?

인접한 같은 값을 갖는 픽셀끼리 하나의 그룹으로 묶어주는 작업이다.

쉽게 말해서 이진화 이미지에서 경계를 이룬 영역에 이름(숫자)을 부여하는 작업이다.

실습 코드

#include <opencv2/opencv.hpp>

#include <windows.h>

using namespace cv;
using namespace std;

int main(int ac, char** av)
{
    Mat img = imread("bacteria.tif");

    Mat img_resize;
    resize(img, img_resize, Size(img.cols * 3, img.rows * 3));

    Mat img_gray;
    cvtColor(img_resize, img_gray, COLOR_BGR2GRAY);

    Mat img_threshold;
    threshold(img_gray, img_threshold, 100, 255, THRESH_BINARY_INV);

    Mat img_labels, stats, centroids;
    int numOfLables = connectedComponentsWithStats(img_threshold, img_labels, stats, centroids, 8, CV_32S);

    // 레이블링 결과에 사각형 그리고, 넘버 표시하기
    for (int j = 1; j < numOfLables; j++) {
        int area = stats.at<int>(j, CC_STAT_AREA);
        int left = stats.at<int>(j, CC_STAT_LEFT);
        int top = stats.at<int>(j, CC_STAT_TOP);
        int width = stats.at<int>(j, CC_STAT_WIDTH);
        int height = stats.at<int>(j, CC_STAT_HEIGHT);


        rectangle(img_resize, Point(left, top), Point(left + width, top + height),
            Scalar(0, 0, 255), 1);

        putText(img_resize, to_string(j), Point(left + 20, top + 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 1);
    }


    imshow("img_resize", img_resize);

    cout << "numOfLables : " << numOfLables - 1 << endl;    // 최종 넘버링에서 1을 빼줘야 함
    waitKey(0);



    return 0;
}

 

결과

3

출처

https://diyver.tistory.com/139?category=911404

반응형

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

C++ OpenCV Labeling  (0) 2021.01.20
C++ OpenCV Transformation  (0) 2021.01.18
C++ OpenCV 컬러 흑백 이미지 Histogram(히스토그램)  (0) 2021.01.08
C++ OpenCV 이미지 전처리  (0) 2021.01.04
C++ OpenCV라이브러리 설치 방법  (0) 2020.12.31