opencv提取輪廓大于某個(gè)閾值的圖像
更新時(shí)間:2020年03月21日 11:46:58 作者:既然如此
這篇文章主要為大家詳細(xì)介紹了opencv提取輪廓大于某個(gè)閾值的圖像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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++代碼實(shí)現(xiàn)網(wǎng)絡(luò)Ping功能
這篇文章主要介紹了C++代碼實(shí)現(xiàn)網(wǎng)絡(luò)Ping功能,Ping命令被送到本地計(jì)算機(jī)的IP軟件,該命令永不退出該計(jì)算機(jī),本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
C語(yǔ)言基于EasyX實(shí)現(xiàn)貪吃蛇
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言基于EasyX實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C語(yǔ)言實(shí)現(xiàn)消消樂(lè)游戲的代碼分享
本章我們將編寫(xiě)十字消除游戲,用戶點(diǎn)擊空白方塊,沿其上下左右方向?qū)ふ业谝粋€(gè)彩色方塊,如果有兩個(gè)或兩個(gè)以上顏色一致,就將其消除,感興趣的可以了解一下2023-02-02
深入理解C++中的new和delete并實(shí)現(xiàn)對(duì)象池
這篇文章主要介紹了C++中的new和delete并實(shí)現(xiàn)對(duì)象池,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

