Merge pull request #874 from StevenPuttemans:master
commit
fde8a6abf9
4 changed files with 160 additions and 27 deletions
@ -0,0 +1,44 @@ |
||||
#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); |
||||
|
||||
/// 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, THINNING_ZHANGSUEN);
|
||||
//ximgproc::thinning(img_binary, img_thinning_GH, 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; |
||||
} |
@ -0,0 +1,54 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#include "test_precomp.hpp" |
||||
|
||||
using namespace cv; |
||||
using namespace cv::ximgproc; |
||||
|
||||
namespace { |
||||
|
||||
static int createTestImage(Mat& src) |
||||
{ |
||||
src = Mat::zeros(Size(256, 256), CV_8UC1); |
||||
for (int x = 50; x < src.cols - 50; x += 50) |
||||
{ |
||||
cv::circle(src, Point(x, x/2), 30 + x/2, Scalar(255), 5); |
||||
} |
||||
int src_pixels = countNonZero(src); |
||||
EXPECT_GT(src_pixels, 0); |
||||
return src_pixels; |
||||
} |
||||
|
||||
TEST(ximpgroc_Thinning, simple_ZHANGSUEN) |
||||
{ |
||||
Mat src; |
||||
int src_pixels = createTestImage(src); |
||||
|
||||
Mat dst; |
||||
thinning(src, dst, THINNING_ZHANGSUEN); |
||||
int dst_pixels = countNonZero(dst); |
||||
EXPECT_LE(dst_pixels, src_pixels); |
||||
|
||||
#if 0 |
||||
imshow("src", src); imshow("dst", dst); waitKey(); |
||||
#endif |
||||
} |
||||
|
||||
TEST(ximpgroc_Thinning, simple_GUOHALL) |
||||
{ |
||||
Mat src; |
||||
int src_pixels = createTestImage(src); |
||||
|
||||
Mat dst; |
||||
thinning(src, dst, THINNING_GUOHALL); |
||||
int dst_pixels = countNonZero(dst); |
||||
EXPECT_LE(dst_pixels, src_pixels); |
||||
|
||||
#if 0 |
||||
imshow("src", src); imshow("dst", dst); waitKey(); |
||||
#endif |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue