溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

opencv提取輪廓大于某個(gè)閾值的圖像

發(fā)布時(shí)間:2020-09-22 10:09:05 來源:腳本之家 閱讀:196 作者:既然如此 欄目:編程語言

本文實(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的輪廓的圖像

opencv提取輪廓大于某個(gè)閾值的圖像

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(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)容。

AI