opencv提取輪廓大于某個(gè)閾值的圖像
本文實(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)//只畫(huà)出輪廓大于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的輪廓的圖像
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++一個(gè)函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)
這篇文章主要介紹了C++一個(gè)函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02C++異常處理 try,catch,throw,finally的用法
這篇文章主要介紹了C++異常處理 try,catch,throw,finally的用法,需要的朋友可以參考下2018-01-01C語(yǔ)言簡(jiǎn)明講解歸并排序的應(yīng)用
這篇文章主要介紹了 c語(yǔ)言排序之歸并排序,歸并就是把兩個(gè)或多個(gè)序列合并,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C語(yǔ)言GetStdHandle函數(shù)使用方法
這篇文章介紹了C語(yǔ)言GetStdHandle函數(shù)的使用方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12C++實(shí)現(xiàn)LeetCode(87.攪亂字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(87.攪亂字符串),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07OpenCV基于距離變換和分水嶺實(shí)現(xiàn)圖像分割
圖像分割是根據(jù)灰度、顏色、紋理和形狀等特征,把圖像分成若干個(gè)特定的、具有獨(dú)特性質(zhì)的區(qū)域。本文將基于距離變換和分水嶺實(shí)現(xiàn)圖像分割,需要的可以了解一下2022-09-09visual studio 2013中配置opencv圖文教程 Opencv2.4.9安裝配置教程
這篇文章主要為大家詳細(xì)介紹了Opencv2.4.9安裝教程,以及在visualstudio 2013中opencv的配置步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04