thinning.cpp 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

#include "opencv2/ximgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("opencv-logo.png", IMREAD_COLOR);
14
    resize(img, img, Size(), 0.5, 0.5, INTER_LINEAR_EXACT);
15 16 17 18 19 20 21 22

    /// Threshold the input image
    Mat img_grayscale, img_binary;
    cvtColor(img, img_grayscale,COLOR_BGR2GRAY);
    threshold(img_grayscale, img_binary, 0, 255, THRESH_OTSU | THRESH_BINARY_INV);

    /// Apply thinning to get a skeleton
    Mat img_thinning_ZS, img_thinning_GH;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
23 24
    ximgproc::thinning(img_binary, img_thinning_ZS, ximgproc::THINNING_ZHANGSUEN);
    ximgproc::thinning(img_binary, img_thinning_GH, ximgproc::THINNING_GUOHALL);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    /// Make 3 channel images from thinning result
    Mat result_ZS(img.rows, img.cols, CV_8UC3), result_GH(img.rows, img.cols, CV_8UC3);

    Mat in[] = { img_thinning_ZS, img_thinning_ZS, img_thinning_ZS };
    Mat in2[] = { img_thinning_GH, img_thinning_GH, img_thinning_GH };
    int from_to[] = { 0,0, 1,1, 2,2 };
    mixChannels( in, 3, &result_ZS, 1, from_to, 3 );
    mixChannels( in2, 3, &result_GH, 1, from_to, 3 );

    /// Combine everything into a canvas
    Mat canvas(img.rows, img.cols * 3, CV_8UC3);
    img.copyTo( canvas( Rect(0, 0, img.cols, img.rows) ) );
    result_ZS.copyTo( canvas( Rect(img.cols, 0, img.cols, img.rows) ) );
    result_GH.copyTo( canvas( Rect(img.cols*2, 0, img.cols, img.rows) ) );

    /// Visualize result
    imshow("Skeleton", canvas); waitKey(0);

    return 0;
}