#include <stdio.h>
int voronoiType = -1;
int edgeThresh = 100;
Mat gray;
static void onTrackbar( int, void* )
{
static const Scalar colors[] =
{
};
int maskSize = voronoiType >= 0 ?
DIST_MASK_5 : maskSize0;
int distType = voronoiType >= 0 ?
DIST_L2 : distType0;
Mat edge = gray >= edgeThresh, dist, labels, dist8u;
if( voronoiType < 0 )
else
if( voronoiType < 0 )
{
dist *= 5000;
Mat dist32s, dist8u1, dist8u2;
dist.convertTo(dist32s,
CV_32S, 1, 0.5);
dist32s &= Scalar::all(255);
dist32s.convertTo(dist8u1,
CV_8U, 1, 0);
dist32s *= -1;
dist32s += Scalar::all(255);
dist32s.convertTo(dist8u2,
CV_8U);
Mat planes[] = {dist8u1, dist8u2, dist8u2};
merge(planes, 3, dist8u);
}
else
{
dist8u.create(labels.size(),
CV_8UC3);
for( int i = 0; i < labels.rows; i++ )
{
const int* ll = (const int*)labels.ptr(i);
const float* dd = (const float*)dist.ptr(i);
for( int j = 0; j < labels.cols; j++ )
{
int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1;
float scale = 1.f/(1 + dd[j]*dd[j]*0.0004f);
}
}
}
imshow(
"Distance Map", dist8u );
}
static void help(const char** argv)
{
printf("\nProgram to demonstrate the use of the distance transform function between edge images.\n"
"Usage:\n"
"%s [image_name -- default image is stuff.jpg]\n"
"\nHot keys: \n"
"\tESC - quit the program\n"
"\tC - use C/Inf metric\n"
"\tL1 - use L1 metric\n"
"\tL2 - use L2 metric\n"
"\t3 - use 3x3 mask\n"
"\t5 - use 5x5 mask\n"
"\t0 - use precise distance transform\n"
"\tv - switch to Voronoi diagram mode\n"
"\tp - switch to pixel-based Voronoi diagram mode\n"
"\tSPACE - loop through all the modes\n\n", argv[0]);
}
const char* keys =
{
"{help h||}{@image |stuff.jpg|input image file}"
};
int main(
int argc,
const char** argv )
{
CommandLineParser parser(argc, argv, keys);
help(argv);
if (parser.has("help"))
return 0;
string filename = parser.get<string>(0);
if(gray.empty())
{
printf("Cannot read image file: %s\n", filename.c_str());
help(argv);
return -1;
}
createTrackbar(
"Brightness Threshold",
"Distance Map", &edgeThresh, 255, onTrackbar, 0);
for(;;)
{
onTrackbar(0, 0);
if( c == 27 )
break;
if( c == 'c' || c == 'C' || c == '1' || c == '2' ||
c == '3' || c == '5' || c == '0' )
voronoiType = -1;
if( c == 'c' || c == 'C' )
else if( c == '1' )
else if( c == '2' )
else if( c == '3' )
else if( c == '5' )
else if( c == '0' )
else if( c == 'v' )
voronoiType = 0;
else if( c == 'p' )
voronoiType = 1;
else if( c == ' ' )
{
if( voronoiType == 0 )
voronoiType = 1;
else if( voronoiType == 1 )
{
voronoiType = -1;
}
else if( distType0 ==
DIST_C )
voronoiType = 0;
}
}
return 0;
}
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
#define CV_8U
Definition: core/include/opencv2/core/hal/interface.h:73
#define CV_32S
Definition: core/include/opencv2/core/hal/interface.h:77
unsigned char uchar
Definition: core/include/opencv2/core/hal/interface.h:51
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:200
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void distanceTransform(InputArray src, OutputArray dst, OutputArray labels, int distanceType, int maskSize, int labelType=DIST_LABEL_CCOMP)
Calculates the distance to the closest zero pixel for each pixel of the source image.
@ DIST_C
distance = max(|x1-x2|,|y1-y2|)
Definition: imgproc/include/opencv2/imgproc.hpp:308
@ DIST_L1
distance = |x1-x2| + |y1-y2|
Definition: imgproc/include/opencv2/imgproc.hpp:306
@ DIST_L2
the simple euclidean distance
Definition: imgproc/include/opencv2/imgproc.hpp:307
@ DIST_MASK_3
mask=3
Definition: imgproc/include/opencv2/imgproc.hpp:317
@ DIST_MASK_PRECISE
Definition: imgproc/include/opencv2/imgproc.hpp:319
@ DIST_MASK_5
mask=5
Definition: imgproc/include/opencv2/imgproc.hpp:318
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
Definition: core/include/opencv2/core.hpp:107