opencv4轮廓的提取与筛选(VS2019 & C++)

findContours发现轮廓

findContours(
InputOutputArray  binImg, //输入8bit图像,0值像素值不变,非0的像素看成1;(变为二值图像)
 OutputArrayOfArrays  contours,//输出找到的轮廓对象
OutputArray,  hierachy// 图像的拓扑结构
int mode, //轮廓返回的模式(RETR_TREE等)
int method,//发现方法(CHAIN_APPROX_SIMPLE等)
Point offset=Point()//轮廓像素的位移(默认没有位移(0, 0))
)

drawContours绘制轮廓

drawContours(
InputOutputArray  binImg, // 输出图像
 OutputArrayOfArrays  contours,//找到的全部轮廓对象
Int contourIdx//轮廓索引号
const Scalar & color,//绘制颜色
int  thickness,//绘制线宽
int  lineType ,//线的类型(默认8)
InputArray hierarchy,//拓扑结构图
int maxlevel,//最大层数(0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓)
Point offset=Point()//轮廓位移
)
#include<opencv2/opencv.hpp>
#include<iostream>
#include<opencv2/highgui/highgui_c.h>
#include<vector>

using namespace cv;
using namespace std;


int main()
{
    Mat src, dst;
    src = imread("D://5.jpg");
    if (src.empty())
    {
        printf("can not load image \n");
        return -1;
    }
    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);
    dst = Mat::zeros(src.size(), CV_8UC3); 

    blur(src, src, Size(3, 3));      //对输入的图像进行均值滤波
    cvtColor(src, src, COLOR_BGR2GRAY);
    Canny(src, src, 20, 80, 3, false);    //对输入图像进行边缘检测
    std::vector<std::vector<Point>> contours;
    std::vector<Vec4i> hierarchy;
    findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

    //RNG类是opencv里C++的随机数产生器。它可产生一个64位的int随机数。
    //目前可按均匀分布和高斯分布产生随机数
    //RNG::uniform(a, b )为返回一个[a,b)范围的均匀分布的随机数
    RNG rng(0);  
    for (int i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //绘图线条颜色随机,并绘制在新建的画布上
        drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point(0, 0));
    }
    namedWindow("output", WINDOW_AUTOSIZE);
    imshow("output", dst);
    waitKey();
    return 0;
}

结果如下:

 

 

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个流行的计算机视觉库,提供了许多图像处理和计算机视觉算法。轮廓提取OpenCV中的一个重要功能,它可以用于检测图像中的形状和物体。 在Java中使用OpenCV进行轮廓提取,可以使用OpenCV的Java接口。以下是基本的轮廓提取代码示例: ``` Mat src = Imgcodecs.imread(&quot;input.jpg&quot;); Mat gray = new Mat(); Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(gray, gray, 100, 200); List&lt;MatOfPoint&gt; contours = new ArrayList&lt;&gt;(); Mat hierarchy = new Mat(); Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); ``` 这段代码读取输入图像,将其转换为灰度图像,使用Canny算子进行边缘检测,然后使用findContours函数提取轮廓。这个函数返回一个包含所有轮廓的MatOfPoint列表,以及一个包含层次结构信息的Mat。 在C++中,轮廓提取与Java非常相似。以下是一个简单的例子: ``` cv::Mat src = cv::imread(&quot;input.jpg&quot;); cv::Mat gray; cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY); cv::Canny(gray, gray, 100, 200); std::vector&lt;std::vector&lt;cv::Point&gt;&gt; contours; cv::findContours(gray, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); ``` 这段代码也读取输入图像,将其转换为灰度图像,使用Canny算子进行边缘检测,然后使用findContours函数提取轮廓。这个函数返回一个包含所有轮廓的vector&lt;vector&lt;Point&gt;&gt;列表。 在Java或C++中,提取轮廓后可能需要对它们进行筛选,以便只保留感兴趣的轮廓。例如,可以使用轮廓的面积、周长或形状等特征来筛选轮廓。以下是一个简单的示例,仅保留面积大于100的轮廓: Java: ``` List&lt;MatOfPoint&gt; filteredContours = new ArrayList&lt;&gt;(); for (MatOfPoint contour : contours) { double area = Imgproc.contourArea(contour); if (area &gt; 100) { filteredContours.add(contour); } } ``` C++: ``` std::vector&lt;std::vector&lt;cv::Point&gt;&gt; filteredContours; for (const auto&amp; contour : contours) { double area = cv::contourArea(contour); if (area &gt; 100) { filteredContours.push_back(contour); } } ``` 这个例子计算每个轮廓的面积,然后仅保留面积大于100的轮廓。可以根据需求使用不同的特征和阈值来筛选轮廓

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值