arcLength函数

1、arcLength函数

函数的作用

主要是计算图像轮廓的周长、

2、函数调用形式

C++: double arcLength(InputArray curve, bool closed)

参数详解:

InputArray curve:表示图像的轮廓

bool closed:表示轮廓是否封闭的

3、一般图像轮廓矩也可以算出图像的周长和面积

opencv代码:


    
    
  1. #include “opencv2/highgui/highgui.hpp”
  2. #include “opencv2/imgproc/imgproc.hpp”
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. using namespace cv;
  7. using namespace std;
  8. Mat src; Mat src_gray;
  9. int thresh = 100;
  10. int max_thresh = 255;
  11. RNG rng(12345);
  12. /// 函数声明
  13. void thresh_callback(int, void* );
  14. /** @主函数 */
  15. int main( int argc, char** argv )
  16. {
  17. /// 读入原图像, 返回3通道图像数据
  18. src = imread( argv[ 1], 1 );
  19. /// 把原图像转化成灰度图像并进行平滑
  20. cvtColor( src, src_gray, CV_BGR2GRAY );
  21. blur( src_gray, src_gray, Size( 3, 3) );
  22. /// 创建新窗口
  23. char* source_window = “Source”;
  24. namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  25. imshow( source_window, src );
  26. createTrackbar( ” Canny thresh:”, “Source”, &thresh, max_thresh, thresh_callback );
  27. thresh_callback( 0, 0 );
  28. waitKey( 0);
  29. return( 0);
  30. }
  31. /** @thresh_callback 函数 */
  32. void thresh_callback(int, void* )
  33. {
  34. Mat canny_output;
  35. vector< vector<Point> > contours;
  36. vector<Vec4i> hierarchy;
  37. /// 使用Canndy检测边缘
  38. Canny( src_gray, canny_output, thresh, thresh* 2, 3 );
  39. /// 找到轮廓
  40. findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point( 0, 0) );
  41. /// 计算矩
  42. vector<Moments> mu(contours.size() );
  43. for( int i = 0; i < contours.size(); i++ )
  44. { mu[i] = moments( contours[i], false ); }
  45. /// 计算中心矩:
  46. vector<Point2f> mc( contours.size() );
  47. for( int i = 0; i < contours.size(); i++ )
  48. { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
  49. /// 绘制轮廓
  50. Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  51. for( int i = 0; i< contours.size(); i++ )
  52. {
  53. Scalar color = Scalar( rng.uniform( 0, 255), rng.uniform( 0, 255), rng.uniform( 0, 255) );
  54. drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
  55. circle( drawing, mc[i], 4, color, -1, 8, 0 );
  56. }
  57. /// 显示到窗口中
  58. namedWindow( “Contours”, CV_WINDOW_AUTOSIZE );
  59. imshow( “Contours”, drawing );
  60. /// 通过m00计算轮廓面积并且和OpenCV函数比较
  61. printf( “\t Info: Area and Contour Length \n”);
  62. for( int i = 0; i< contours.size(); i++ )
  63. {
  64. printf( ” * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n”, i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
  65. Scalar color = Scalar( rng.uniform( 0, 255), rng.uniform( 0, 255), rng.uniform( 0, 255) );
  66. drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
  67. circle( drawing, mc[i], 4, color, -1, 8, 0 );
  68. }
  69. }


            </div>

1、arcLength函数

函数的作用

主要是计算图像轮廓的周长、

2、函数调用形式

C++: double arcLength(InputArray curve, bool closed)

参数详解:

InputArray curve:表示图像的轮廓

bool closed:表示轮廓是否封闭的

3、一般图像轮廓矩也可以算出图像的周长和面积

opencv代码:


  
  
  1. #include “opencv2/highgui/highgui.hpp”
  2. #include “opencv2/imgproc/imgproc.hpp”
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. using namespace cv;
  7. using namespace std;
  8. Mat src; Mat src_gray;
  9. int thresh = 100;
  10. int max_thresh = 255;
  11. RNG rng(12345);
  12. /// 函数声明
  13. void thresh_callback(int, void* );
  14. /** @主函数 */
  15. int main( int argc, char** argv )
  16. {
  17. /// 读入原图像, 返回3通道图像数据
  18. src = imread( argv[ 1], 1 );
  19. /// 把原图像转化成灰度图像并进行平滑
  20. cvtColor( src, src_gray, CV_BGR2GRAY );
  21. blur( src_gray, src_gray, Size( 3, 3) );
  22. /// 创建新窗口
  23. char* source_window = “Source”;
  24. namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  25. imshow( source_window, src );
  26. createTrackbar( ” Canny thresh:”, “Source”, &thresh, max_thresh, thresh_callback );
  27. thresh_callback( 0, 0 );
  28. waitKey( 0);
  29. return( 0);
  30. }
  31. /** @thresh_callback 函数 */
  32. void thresh_callback(int, void* )
  33. {
  34. Mat canny_output;
  35. vector< vector<Point> > contours;
  36. vector<Vec4i> hierarchy;
  37. /// 使用Canndy检测边缘
  38. Canny( src_gray, canny_output, thresh, thresh* 2, 3 );
  39. /// 找到轮廓
  40. findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point( 0, 0) );
  41. /// 计算矩
  42. vector<Moments> mu(contours.size() );
  43. for( int i = 0; i < contours.size(); i++ )
  44. { mu[i] = moments( contours[i], false ); }
  45. /// 计算中心矩:
  46. vector<Point2f> mc( contours.size() );
  47. for( int i = 0; i < contours.size(); i++ )
  48. { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
  49. /// 绘制轮廓
  50. Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  51. for( int i = 0; i< contours.size(); i++ )
  52. {
  53. Scalar color = Scalar( rng.uniform( 0, 255), rng.uniform( 0, 255), rng.uniform( 0, 255) );
  54. drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
  55. circle( drawing, mc[i], 4, color, -1, 8, 0 );
  56. }
  57. /// 显示到窗口中
  58. namedWindow( “Contours”, CV_WINDOW_AUTOSIZE );
  59. imshow( “Contours”, drawing );
  60. /// 通过m00计算轮廓面积并且和OpenCV函数比较
  61. printf( “\t Info: Area and Contour Length \n”);
  62. for( int i = 0; i< contours.size(); i++ )
  63. {
  64. printf( ” * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n”, i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
  65. Scalar color = Scalar( rng.uniform( 0, 255), rng.uniform( 0, 255), rng.uniform( 0, 255) );
  66. drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
  67. circle( drawing, mc[i], 4, color, -1, 8, 0 );
  68. }
  69. }


            </div>

猜你喜欢

转载自blog.csdn.net/monk1992/article/details/82620033