OpenCV如何在图像中寻找轮廓(60)

 返回:OpenCV系列文章目录(持续更新中......)

上一篇:OpenCV如何模板匹配(59)
下一篇 :OpenCV检测凸包(61)

目标

在本教程中,您将学习如何:

cv::findContours 和 cv::drawContours 都是 OpenCV 库中常用的图像处理函数,主要用于图像分割和轮廓绘制等操作。

cv::findContours 是一个用于在二值图像中查找轮廓的函数。它可以根据二值图像中的像素灰度值(0 或非零)来确定对象的轮廓,并返回一个由所有轮廓点构成的向量。此外,findContours 还可以实现轮廓间的层次结构分析,进一步提高轮廓分析的精度。

cv::drawContours 则可以根据给定的轮廓向量,对指定的图像进行轮廓绘制操作。它可以绘制轮廓内部、轮廓外边界、轮廓及其外边界,也可以指定边界的颜色和宽度。通过 drawContours 函数,我们可以将轮廓绘制在原图像中,以便后续的图像分析和处理。

因此,cv::findContours 和 cv::drawContours 通常会一起使用。通过 findContours 函数查找轮廓,然后利用 drawContours 函数在原图像中绘制轮廓,可以更好地实现图像分割和轮廓分析等操作。

C++代码

本教程代码如下所示。您也可以从这里下载

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
 
using namespace cv;
using namespace std;
 
Mat src_gray;
int thresh = 100;
RNG rng(12345);
 
void thresh_callback(int, void* );
 
int main( int argc, char** argv )
{
 CommandLineParser parser( argc, argv, "{@input | HappyFish.jpg | input image}" );
 Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
 if( src.empty() )
 {
 cout << "Could not open or find the image!\n" << endl;
 cout << "Usage: " << argv[0] << " <Input image>" << endl;
 return -1;
 }
 
 cvtColor( src, src_gray, COLOR_BGR2GRAY );
 blur( src_gray, src_gray, Size(3,3) );
 
 const char* source_window = "Source";
 namedWindow( source_window );
 imshow( source_window, src );
 
 const int max_thresh = 255;
 createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
 thresh_callback( 0, 0 );
 
 waitKey();
 return 0;
}
 
void thresh_callback(int, void* )
{
 Mat canny_output;
 Canny( src_gray, canny_output, thresh, thresh*2 );
 
 vector<vector<Point> > contours;
 vector<Vec4i> hierarchy;
 findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );
 
 Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
 for( size_t i = 0; i< contours.size(); i++ )
 {
 Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
 drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 );
 }
 
 imshow( "Contours", drawing );
}

结果

在这里:


参考文献:

1、《Finding contours in your image》-----Ana Huamán

相关推荐

  1. opencv如何寻找图片轮廓

    2024-05-03 14:22:07       31 阅读
  2. C#使用OpenCV获取图像轮廓

    2024-05-03 14:22:07       63 阅读
  3. 如何生活寻找心流?

    2024-05-03 14:22:07       45 阅读
  4. opencv通过轮廓点生成闭合图像

    2024-05-03 14:22:07       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-03 14:22:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 14:22:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 14:22:07       87 阅读
  4. Python语言-面向对象

    2024-05-03 14:22:07       96 阅读

热门阅读

  1. MATLAB向量的模||MATLAB向量点积

    2024-05-03 14:22:07       31 阅读
  2. Python 正则表达式1 函数基础

    2024-05-03 14:22:07       32 阅读
  3. Jina,一个神经搜索超神奇Python库

    2024-05-03 14:22:07       29 阅读
  4. RESTful API 构建 Web 应用程序

    2024-05-03 14:22:07       30 阅读
  5. 面试经典150题——文本左右对齐

    2024-05-03 14:22:07       31 阅读
  6. php 追加 内容

    2024-05-03 14:22:07       29 阅读
  7. PostgreSQL自带的工具介绍

    2024-05-03 14:22:07       30 阅读
  8. 单例模式的几种实现方式

    2024-05-03 14:22:07       36 阅读