标签:++ 标题 std input draw poi chain open 信息
1 矩的计算:moments()函数
Moments moments(InputArray array,bool binaryImage=false)
2 就是轮廓面积:contourArea()函数
double contourArea(InputArray contour,bool oriented=false)
3 计算轮廓长度:arcLength()函数
double arcLength(InputArray curve,bool closed)
//例子
using namespace cv;
using namespace std;
//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
Mat g_srcImage; Mat g_grayImage;
int g_nThresh = 100;
int g_nMaxThresh = 255;
RNG g_rng(12345);
Mat g_cannyMat_output;
vector<vector
vector
//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void* );
static void ShowHelpText( );
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
//【0】改变console字体颜色
system("color 9F");
ShowHelpText();
// 读入原图像, 返回3通道图像数据
g_srcImage = imread( "1.jpg", 1 );
// 把原图像转化成灰度图像并进行平滑
cvtColor( g_srcImage, g_grayImage, COLOR_BGR2GRAY );
blur( g_grayImage, g_grayImage, Size(3,3) );
// 创建新窗口
namedWindow( WINDOW_NAME1, WINDOW_AUTOSIZE );
imshow( WINDOW_NAME1, g_srcImage );
//创建滚动条并进行初始化
createTrackbar( " 阈值", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ThreshChange );
on_ThreshChange( 0, 0 );
waitKey(0);
return(0);
}
//-----------------------------------【on_ThreshChange( )函数】-------------------------------
// 描述:回调函数
//-----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void* )
{
// 使用Canndy检测边缘
Canny( g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2, 3 );
// 找到轮廓
findContours( g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
// 计算矩
vector<Moments> mu(g_vContours.size() );
for(unsigned int i = 0; i < g_vContours.size(); i++ )
{ mu[i] = moments( g_vContours[i], false ); }
// 计算中心矩
vector<Point2f> mc( g_vContours.size() );
for( unsigned int i = 0; i < g_vContours.size(); i++ )
{ mc[i] = Point2f( static_cast<float>(mu[i].m10/mu[i].m00), static_cast<float>(mu[i].m01/mu[i].m00 )); }
// 绘制轮廓
Mat drawing = Mat::zeros( g_cannyMat_output.size(), CV_8UC3 );
for( unsigned int i = 0; i< g_vContours.size(); i++ )
{
Scalar color = Scalar( g_rng.uniform(0, 255), g_rng.uniform(0,255), g_rng.uniform(0,255) );//随机生成颜色值
drawContours( drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point() );//绘制外层和内层轮廓
circle( drawing, mc[i], 4, color, -1, 8, 0 );;//绘制圆
}
// 显示到窗口中
namedWindow( WINDOW_NAME2, WINDOW_AUTOSIZE );
imshow( WINDOW_NAME2, drawing );
// 通过m00计算轮廓面积并且和OpenCV函数比较
printf("\t 输出内容: 面积和轮廓长度\n");
for(unsigned int i = 0; i< g_vContours.size(); i++ )
{
printf(" >通过m00计算出轮廓[%d]的面积: (M_00) = %.2f \n OpenCV函数计算出的面积=%.2f , 长度: %.2f \n\n", i, mu[i].m00, contourArea(g_vContours[i]), arcLength( g_vContours[i], true ) );
Scalar color = Scalar( g_rng.uniform(0, 255), g_rng.uniform(0,255), g_rng.uniform(0,255) );
drawContours( drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point() );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
}
//-----------------------------------【ShowHelpText( )函数】-----------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第76个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION );
printf("\n\n ----------------------------------------------------------------------------\n");
}
标签:++ 标题 std input draw poi chain open 信息
原文地址:https://www.cnblogs.com/shuguomeifuguo/p/12005177.html