opencv 求连通区域的重心
主要步骤 用findContours获取连通域轮廓通过Moments对象获取轮廓所在重心findContours函数的讲解实现代码 #include#in
主要步骤
- 用
findContours获取连通域轮廓 - 通过
Moments对象获取轮廓所在重心 findContours函数的讲解
实现代码
#include
#include
using namespace std;
using namespace cv;int main()
{Mat src = imread("F:\\testdata\\test.jpg", 0);threshold(src, src, 0, 255, THRESH_OTSU);//获取图像轮廓vector>contours; //每个轮廓中的点vectorhierarchy; //轮廓的索引??? findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());for (int i = 0; i < contours.size(); ++i) {Mat tmp(contours.at(i));Moments moment=moments(tmp, false);if (moment.m00 != 0)//除数不能为0{int x = cvRound(moment.m10 / moment.m00);//计算重心横坐标int y = cvRound(moment.m01 /
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!