寻找轮廓的中点

主要是回答网友提问,同时回顾主要知识。
 
#include "stdafx.h"
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "GOCVHelper.h"
 
using namespace cv;
using namespace std;
using namespace GO;
 
RNG rng12345(12345);
int main()
{
    Mat matSrc = imread("e:/sandbox/shapes_and_colors.jpg",0);
    GaussianBlur(matSrc,matSrc,Size(5,5),0);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    //阈值
    threshold(matSrc,matSrc,60,255,THRESH_BINARY);
    //寻找轮廓
    findContours(matSrc.clone(),contours,hierarchy, RETR_EXTERNAL,CHAIN_APPROX_SIMPLE,Point(0, 0) );
    /// 计算矩
    vector<Moments> mu(contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { mu[i] = moments( contours[i], false ); }
    ///  计算中心矩:
    vector<Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
    /// 绘制轮廓
    Mat drawing = Mat::zeros( matSrc.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng12345.uniform(0, 255), rng12345.uniform(0,255), rng12345.uniform(0,255) );
        drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
        circle( drawing, mc[i], 4, color, -1, 8, 0 );
    }
    waitKey();
    return 0;
 
};
 

 

posted on   jsxyhelu  阅读(36)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示