1 #include <opencv2/core/utility.hpp>
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/highgui.hpp"
10 int maskSize0 = DIST_MASK_5;
13 int distType0 = DIST_L1;
15 // The output and temporary images
18 // threshold trackbar callback
19 static void onTrackbar( int, void* )
21 static const Scalar colors[] =
34 int maskSize = voronoiType >= 0 ? DIST_MASK_5 : maskSize0;
35 int distType = voronoiType >= 0 ? DIST_L2 : distType0;
37 Mat edge = gray >= edgeThresh, dist, labels, dist8u;
40 distanceTransform( edge, dist, distType, maskSize );
42 distanceTransform( edge, dist, labels, distType, maskSize, voronoiType );
46 // begin "painting" the distance transform result
50 Mat dist32s, dist8u1, dist8u2;
52 dist.convertTo(dist32s, CV_32S, 1, 0.5);
53 dist32s &= Scalar::all(255);
55 dist32s.convertTo(dist8u1, CV_8U, 1, 0);
58 dist32s += Scalar::all(255);
59 dist32s.convertTo(dist8u2, CV_8U);
61 Mat planes[] = {dist8u1, dist8u2, dist8u2};
62 merge(planes, 3, dist8u);
66 dist8u.create(labels.size(), CV_8UC3);
67 for( int i = 0; i < labels.rows; i++ )
69 const int* ll = (const int*)labels.ptr(i);
70 const float* dd = (const float*)dist.ptr(i);
71 uchar* d = (uchar*)dist8u.ptr(i);
72 for( int j = 0; j < labels.cols; j++ )
74 int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1;
75 float scale = 1.f/(1 + dd[j]*dd[j]*0.0004f);
76 int b = cvRound(colors[idx][0]*scale);
77 int g = cvRound(colors[idx][1]*scale);
78 int r = cvRound(colors[idx][2]*scale);
86 imshow("Distance Map", dist8u );
91 printf("\nProgram to demonstrate the use of the distance transform function between edge images.\n"
93 "./distrans [image_name -- default image is stuff.jpg]\n"
95 "\tESC - quit the program\n"
96 "\tC - use C/Inf metric\n"
97 "\tL1 - use L1 metric\n"
98 "\tL2 - use L2 metric\n"
99 "\t3 - use 3x3 mask\n"
100 "\t5 - use 5x5 mask\n"
101 "\t0 - use precise distance transform\n"
102 "\tv - switch to Voronoi diagram mode\n"
103 "\tp - switch to pixel-based Voronoi diagram mode\n"
104 "\tSPACE - loop through all the modes\n\n");
109 "{@image |stuff.jpg|input image file}"
112 int main( int argc, const char** argv )
115 CommandLineParser parser(argc, argv, keys);
116 string filename = parser.get<string>(0);
117 gray = imread(filename.c_str(), 0);
120 printf("Cannot read image file: %s\n", filename.c_str());
125 namedWindow("Distance Map", 1);
126 createTrackbar("Brightness Threshold", "Distance Map", &edgeThresh, 255, onTrackbar, 0);
130 // Call to update the view
133 int c = waitKey(0) & 255;
138 if( c == 'c' || c == 'C' || c == '1' || c == '2' ||
139 c == '3' || c == '5' || c == '0' )
142 if( c == 'c' || c == 'C' )
149 maskSize0 = DIST_MASK_3;
151 maskSize0 = DIST_MASK_5;
153 maskSize0 = DIST_MASK_PRECISE;
160 if( voronoiType == 0 )
162 else if( voronoiType == 1 )
165 maskSize0 = DIST_MASK_3;
168 else if( distType0 == DIST_C )
170 else if( distType0 == DIST_L1 )
172 else if( maskSize0 == DIST_MASK_3 )
173 maskSize0 = DIST_MASK_5;
174 else if( maskSize0 == DIST_MASK_5 )
175 maskSize0 = DIST_MASK_PRECISE;
176 else if( maskSize0 == DIST_MASK_PRECISE )