【图像处理】OpenCV系列三十八 --- arcLength函
2019-05-21 本文已影响14人
307656af5a04
1、函数原型
double arcLength(InputArray curve,
bool closed)
2、函数功能
该函数计算曲线长度或闭合轮廓周长;
3、参数详解
(1) 第一个参数,InputArray curve,一般是由图像的轮廓点组成的点集;
(2) 第二个参数,bool closed,表示输出的多边形是否封闭;true表示封闭,false表示不封闭;
4、实验实例
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
// 函数声明
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
// 读入原图像, 返回3通道图像数据
src = imread( "y.png", 1 );
if(src.empty())
{
printf("src image error!");
return -1;
}
// 把原图像转化成灰度图像并进行平滑
cvtColor( src, src_gray, COLOR_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
// 创建新窗口
char* source_window = "Source";
namedWindow( source_window, WINDOW_AUTOSIZE );
imshow( source_window, src );
// 创建滑动条
createTrackbar( " Canny thresh:", "Source",
&thresh, max_thresh, thresh_callback );
// 初始调用回调函数
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
// 回调函数
void thresh_callback(int, void* )
{
Mat canny_output;
// 保存图像的轮廓
vector<vector<Point> > contours;
// 保存轮廓之间的层级关系
vector<Vec4i> hierarchy;
// 使用Canndy检测边缘
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
// 找到轮廓
findContours( canny_output, contours, hierarchy,
RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
// 创建需要绘制轮廓的图像
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
// 随机颜色
Scalar color = Scalar( rng.uniform(0, 255),
rng.uniform(0,255), rng.uniform(0,255) );
// 绘制
drawContours( drawing, contours, i, color,
2, 8, hierarchy, 0, Point() );
}
// 显示到窗口中
namedWindow( "Contours", WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
printf("\t Info:Contour Length \n");
for( int i = 0; i< contours.size(); i++ )
{
// 计算的轮廓周长
printf(" * Contour[%d] - arcLength: %.2f \n",
i,arcLength( contours[i], true ) );
}
}
5、实验结果
原图 效果图 image.png我是奕双,现在已经毕业将近两年了,从大学开始学编程,期间学习了C语言编程,C++语言编程,Win32编程,MFC编程,毕业之后进入一家图像处理相关领域的公司,掌握了用OpenCV对图像进行处理,如果大家对相关领域感兴趣的话,可以关注我,我这边会为大家进行解答哦!如果大家需要相关学习资料的话,可以私聊我哦!