您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了opencv提取輪廓大于某個(gè)閾值的圖像,供大家參考,具體內(nèi)容如下
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "stdio.h" #include"core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { const char* inputImage = "d:/3.jpg"; Mat img; int threshval =100; img = imread(inputImage,0); if (img.empty()) { cout << "Could not read input image file: " << inputImage << endl; return -1; } img = img >110; namedWindow("Img", 1); imshow("Img", img); vector<vector<Point> > contours; vector<Vec4i>hierarchy; vector<Point> contour; Mat dst = Mat::zeros(img.rows, img.cols, CV_8UC3); findContours(img, contours,hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); int m=contours.size();//得到輪廓的數(shù)量 int n=0; for (int i =0;i<m;++i) { n=contours[i].size(); for (int j =0;j<n;++j) { contour.push_back(contours[i][j]);//讀取每個(gè)輪廓的點(diǎn) } double area = contourArea(contour); //取得輪廓面積 if (area>10)//只畫出輪廓大于10的點(diǎn) { Scalar color( (rand()&255), (rand()&255), (rand()&255) ); drawContours( dst, contours, i, color, 1, 8, hierarchy ); } contour.clear(); } namedWindow("src", 1); imshow( "src", dst ); waitKey(); return 0; }
左邊為二值化的圖像
右邊為提取面積大于10的輪廓的圖像
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。