轮廓矩-- moments()、contourArea()和 arcLength()

moments()

作用:计算一个多边形或栅格化形状的多达3阶的所有矩。

形式:Moments moments(InputArray array, bool binaryImage=false );

参数:

array:栅格图像或者二维点列;

binaryImage:如果是true:所有非零值图像像素被置1,该参数仅用于图像;

moments:输出的矩;


contourArea()

作用:计算轮廓矩面积。

形式:double contourArea(InputArray contour, bool oriented=false );

参数:

contour:输入二维点集,并用std::vector or Mat存储;

oriented:面向区域标志,如果是true:返回一个有向面积值,默认该值为false,即返回的是绝对值;


arcLength()

作用:计算轮廓或曲线的长度。

形式:double arcLength(InputArray curve, bool closed);

参数:

curve:输入二维点集,并用std::vector or Mat存储;

closed:该标志指明曲线是否封闭;



    
    
  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>

moments()

作用:计算一个多边形或栅格化形状的多达3阶的所有矩。

形式:Moments moments(InputArray array, bool binaryImage=false );

参数:

array:栅格图像或者二维点列;

binaryImage:如果是true:所有非零值图像像素被置1,该参数仅用于图像;

moments:输出的矩;


contourArea()

作用:计算轮廓矩面积。

形式:double contourArea(InputArray contour, bool oriented=false );

参数:

contour:输入二维点集,并用std::vector or Mat存储;

oriented:面向区域标志,如果是true:返回一个有向面积值,默认该值为false,即返回的是绝对值;


arcLength()

作用:计算轮廓或曲线的长度。

形式:double arcLength(InputArray curve, bool closed);

参数:

curve:输入二维点集,并用std::vector or Mat存储;

closed:该标志指明曲线是否封闭;



  
  
  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/82592486
今日推荐