OpenCV
开源计算机视觉库
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
图像矩

上一篇教程: 为轮廓创建旋转边界框和椭圆
下一篇教程: 点多边形测试

原作者Ana Huamán
兼容性OpenCV >= 3.0

目标

在本教程中,您将学习如何:

理论

代码

本教程代码如下所示。您也可以从 这里 下载。

#include <iostream>
#include <iomanip>
using namespace cv;
using namespace std;
Mat src_gray;
int thresh = 100;
RNG rng(12345);
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
CommandLineParser parser( argc, argv, "{@input | stuff.jpg | input image}" );
Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
if( src.empty() )
{
cout << "无法打开或找到图像!\n" << endl;
cout << "用法:" << argv[0] << " <输入图像>" << endl;
return -1; -1;
}
cvtColor( src, src_gray, COLOR_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
const char* source_window = "源图像";
namedWindow( source_window );
imshow( source_window, src );
const int max_thresh = 255;
createTrackbar( "Canny 阈值:", source_window, &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey();
return -1; 0;
}
void thresh_callback(int, void* )
{
Mat canny_output;
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
vector<vector<Point> > contours;
findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
vector<Moments> mu(contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{
mu[i] = moments( contours[i] );
}
vector<Point2f> mc( contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{
//添加 1e-5 以避免除以零
mc[i] = Point2f( static_cast<float>(mu[i].m10 / (mu[i].m00 + 1e-5)),
static_cast<float>(mu[i].m01 / (mu[i].m00 + 1e-5)) );
cout << "mc[" << i << "]=" << mc[i] << endl;
}
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
drawContours( drawing, contours, (int)i, color, 2 );
circle( drawing, mc[i], 4, color, -1 );
}
imshow( "轮廓", drawing );
cout << "\t 信息:面积和轮廓长度 \n";
for( size_t i = 0; i < contours.size(); i++ )
{
cout << " * 轮廓[" << i << "] - 面积 (M_00) = " << std::fixed << std::setprecision(2) << mu[i].m00
<< " - OpenCV 计算面积: " << contourArea(contours[i]) << " - 长度: " << arcLength( contours[i], true ) << endl;
}
}
用于命令行解析。
定义 utility.hpp:890
n 维密集数组类
定义 mat.hpp:829
MatSize size
定义 mat.hpp:2177
bool empty() const
如果数组没有元素,则返回 true。
随机数生成器。
定义 core.hpp:2874
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
std::string String
定义 cvstd.hpp:151
#define CV_8UC3
定义 interface.h:90
@ circle
定义 gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
在指定的窗口中显示图像。
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
绘制轮廓轮廓或填充轮廓。
double contourArea(InputArray contour, bool oriented=false)
计算轮廓面积。
double arcLength(InputArray curve, bool closed)
计算轮廓周长或曲线长度。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL 命名空间。

说明

结果

结果如下: