#include <stdio.h>
int maskSize0 = DIST_MASK_5;
int voronoiType = -1;
int edgeThresh = 100;
int distType0 = DIST_L1;
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 )
distanceTransform( edge, dist, distType, maskSize );
else
distanceTransform( edge, dist, labels, distType, maskSize, voronoiType );
if( voronoiType < 0 )
{
dist *= 5000;
pow(dist, 0.5, dist);
Mat dist32s, dist8u1, dist8u2;
dist32s &= Scalar::all(255);
dist32s *= -1;
dist32s += Scalar::all(255);
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);
int b =
cvRound(colors[idx][0]*scale);
int g =
cvRound(colors[idx][1]*scale);
int r =
cvRound(colors[idx][2]*scale);
}
}
}
imshow("Distance Map", dist8u );
}
static void help(const char** argv)
{
printf("\n程序演示边缘图像之间的距离变换函数的使用。\n"
"用法:\n"
"%s [image_name -- 默认图像是 stuff.jpg]\n"
"\n热键:\n"
"\tESC - 退出程序\n"
"\tC - 使用 C/Inf 度量\n"
"\tL1 - 使用 L1 度量\n"
"\tL2 - 使用 L2 度量\n"
"\t3 - 使用 3x3 掩码\n"
"\t5 - 使用 5x5 掩码\n"
"\t0 - 使用精确距离变换\n"
"\tv - 切换到 Voronoi 图模式\n"
"\tp - 切换到像素级 Voronoi 图模式\n"
"\tSPACE - 循环遍历所有模式\n\n", argv[0]);
}
const char* keys =
{
"{help h||}{@image |stuff.jpg|输入图像文件}"
};
int main(
int argc,
const char** argv )
{
help(argv);
if (parser.has("help"))
return 0;
string filename = parser.get<string>(0);
gray =
imread(samples::findFile(filename), 0);
if(gray.empty())
{
printf("无法读取图像文件:%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 )
else if( distType0 == DIST_L1 )
else if( maskSize0 == DIST_MASK_3 )
else if( maskSize0 == DIST_MASK_5 )
else if( maskSize0 == DIST_MASK_PRECISE )
voronoiType = 0;
}
}
return 0;
}
用于命令行解析。
定义 utility.hpp:820
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
将数组转换为另一种数据类型,可选缩放。
#define CV_8U
定义 interface.h:73
#define CV_32S
定义 interface.h:77
unsigned char uchar
定义 interface.h:51
#define CV_8UC3
定义 interface.h:90
int cvRound(double value)
将浮点数四舍五入到最接近的整数。
定义 fast_math.hpp:200
int waitKey(int delay=0)
等待按键按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
创建一个窗口。
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
创建一个跟踪条并将其附加到指定的窗口。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
从文件加载图像。
@ DIST_C
distance = max(|x1-x2|,|y1-y2|)
定义 imgproc.hpp:308
@ DIST_L1
distance = |x1-x2| + |y1-y2|
定义 imgproc.hpp:306
@ DIST_L2
简单的欧几里得距离
定义 imgproc.hpp:307
@ DIST_MASK_3
mask=3
定义 imgproc.hpp:317
@ DIST_MASK_PRECISE
定义 imgproc.hpp:319
@ DIST_MASK_5
mask=5
定义 imgproc.hpp:318
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
磁盘上文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102