国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

Opencv提取連通區(qū)域輪廓的方法

2020-01-26 13:34:38
字體:
供稿:網(wǎng)友

本文實例為大家分享了Opencv提取連通區(qū)域輪廓的具體代碼,供大家參考,具體內(nèi)容如下

在進(jìn)行圖像分割后,可能需要對感興趣的目標(biāo)區(qū)域進(jìn)行提取,比較常用的方法是計算輪廓。

通過輪廓可以獲得目標(biāo)的一些信息:

(1)目標(biāo)位置

(2)目標(biāo)大小(即面積)

(3)目標(biāo)形狀(輪廓矩)

當(dāng)然,輪廓不一定代表希望目標(biāo)區(qū)域,閾值分割時可能造成一部分信息丟失,因此可以計算輪廓的質(zhì)心坐標(biāo),再進(jìn)行漫水填充。

程序中有尋找質(zhì)心+填充,但效果不好,因此就不放填充后的圖了。

實驗結(jié)果:

#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdio.h> using namespace cv;using namespace std; vector<vector<Point> > contours; //輪廓數(shù)組vector<Point2d> centers; //輪廓質(zhì)心坐標(biāo) vector<vector<Point> >::iterator itr; //輪廓迭代器vector<Point2d>::iterator itrc; //質(zhì)心坐標(biāo)迭代器vector<vector<Point> > con; //當(dāng)前輪廓  int main(){ double area; double minarea = 100; double maxarea = 0; Moments mom; // 輪廓矩 Mat image,gray,edge,dst; namedWindow("origin"); namedWindow("connected_region");  image = imread("view.jpg"); cvtColor(image, gray, COLOR_BGR2GRAY); blur(gray, edge, Size(3,3)); //模糊去噪 threshold(edge,edge,200,255,THRESH_BINARY); //二值化處理  /*尋找輪廓*/ findContours( edge, contours,  CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); itr = contours.begin();  //使用迭代器去除噪聲輪廓 while(itr!=contours.end()) { area = contourArea(*itr); if(area<minarea) { itr = contours.erase(itr); //itr一旦erase,需要重新賦值 } else {  itr++; } if (area>maxarea) { maxarea = area; } } dst = Mat::zeros(image.rows,image.cols,CV_8UC3);  /*繪制連通區(qū)域輪廓,計算質(zhì)心坐標(biāo)*/ Point2d center; itr = contours.begin(); while(itr!=contours.end()) { area = contourArea(*itr); con.push_back(*itr); if(area==maxarea) drawContours(dst,con,-1,Scalar(0,0,255),2); //最大面積紅色繪制 else drawContours(dst,con,-1,Scalar(255,0,0),2); //其它面積藍(lán)色繪制 con.pop_back();  //計算質(zhì)心 mom = moments(*itr); center.x = (int)(mom.m10/mom.m00); center.y = (int)(mom.m01/mom.m00); centers.push_back(center);  itr++; } imshow("origin",image); imshow("connected_region",dst); waitKey(0);  /*漫水填充連通區(qū)域*/ Point2d seed; int new_scalar = 0; int loDiff = 8, upDiff = 8; int connectivity = 4;   itrc = centers.begin(); while(itrc!=centers.end()) {  seed = *itrc; floodFill(image,seed,Scalar::all(new_scalar),NULL, Scalar::all(loDiff),Scalar::all(upDiff),connectivity); itrc++; }  waitKey(0); return 0 ;}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 张家界市| 革吉县| 神农架林区| 安丘市| 西乌珠穆沁旗| 台东市| 五华县| 蕲春县| 岱山县| 洛宁县| 永年县| 巴林右旗| 紫金县| 宜阳县| 玉田县| 叶城县| 大邑县| 罗田县| 徐汇区| 碌曲县| 郓城县| 海兴县| 年辖:市辖区| 郁南县| 长顺县| 平谷区| 美姑县| 图木舒克市| 冕宁县| 开阳县| 噶尔县| 花垣县| 杨浦区| 堆龙德庆县| 灯塔市| 志丹县| 大宁县| 绥棱县| 福鼎市| 雷山县| 读书|