File: thinning.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (45 lines) | stat: -rw-r--r-- 1,551 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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);
    resize(img, img, Size(), 0.5, 0.5, INTER_LINEAR_EXACT);

    /// 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;
    ximgproc::thinning(img_binary, img_thinning_ZS, ximgproc::THINNING_ZHANGSUEN);
    ximgproc::thinning(img_binary, img_thinning_GH, ximgproc::THINNING_GUOHALL);

    /// 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;
}