如何使用OpenCV筛选出轮廓面积超过特定阈值的图像?

2026-04-29 07:492阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计370个文字,预计阅读时间需要2分钟。

如何使用OpenCV筛选出轮廓面积超过特定阈值的图像?

原文示例:本文字例为大师分享了OpenCV提取轮廊大于某个阈值的图像,提供大师参考,具体内容如下:

修改后:大师分享了OpenCV提取大于阈值轮廊的图像方法,供大家参考。

本文实例为大家分享了opencv提取轮廓大于某个阈值的图像,供大家参考,具体内容如下

#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();//得到轮廓的数量 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]);//读取每个轮廓的点 } double area = contourArea(contour); //取得轮廓面积 if (area>10)//只画出轮廓大于10的点 { 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筛选出轮廓面积超过特定阈值的图像?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签:图像本文

本文共计370个文字,预计阅读时间需要2分钟。

如何使用OpenCV筛选出轮廓面积超过特定阈值的图像?

原文示例:本文字例为大师分享了OpenCV提取轮廊大于某个阈值的图像,提供大师参考,具体内容如下:

修改后:大师分享了OpenCV提取大于阈值轮廊的图像方法,供大家参考。

本文实例为大家分享了opencv提取轮廓大于某个阈值的图像,供大家参考,具体内容如下

#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();//得到轮廓的数量 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]);//读取每个轮廓的点 } double area = contourArea(contour); //取得轮廓面积 if (area>10)//只画出轮廓大于10的点 { 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筛选出轮廓面积超过特定阈值的图像?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签:图像本文