contourArea函数

double cvContourArea( const CvArr* contour, CvSlice slice=CV_WHOLE_SEQ );

contour:轮廓(顶点的序列或数组)。
slice:感兴趣区轮廓部分的起点和终点,默认计算整个轮廓的面积。

函数cvContourArea计算整个或部分轮廓的面积。在计算部分轮廓的情况时,由轮廓弧线和连接两端点的弦
围成的区域总面积被计算,如下图所示:

cvContourArea:计算整个或部分轮廓的面积

注意:轮廓的位置将影响区域面积的符号,因此函数范围的有可能是负值。可以使用C运行时函数fabs()来

得到面积的绝对值。


1、contourArea函数

函数作用

主要用于计算图像轮廓的面积

2、contourArea函数调用形式

C++: double contourArea(InputArray contour, bool oriented=false )

参数详解:

InputArray contour:输入的点,一般是图像的轮廓点

bool oriented=false:表示某一个方向上轮廓的的面积值,顺时针或者逆时针,一般选择默认false

3、OpenCV代码:

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


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值