ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

轮廓发现

2021-05-25 22:57:52  阅读:162  来源: 互联网

标签:发现 Point int value threshold output 轮廓


轮廓发现是基于图像边缘提取寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终的轮廓发现结果

cv::findContours()

InputOutputArray binImg//输入图像,非0像素被看成1,0的像素值保持不变8-bit

OutputArrayOfArrays contours//全部发现的非轮廓对象

OutputArray hierachy//图的拓卜结构

int mode//轮廓的返回模式

int method//发现方法

Point offset=Point()//轮廓位移,默认是0

 

cv::drawContours()

InputOutputArray image表示目标图像,

InputArrayOfArrays contour表示输入的轮廓组,每一组轮廓由点vector构成,

int contourIdx指明画第几个轮廓,如果该参数为负值,则画全部轮廓,

const Scalar& color为轮廓的颜色,

int thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部,

int lineType为线型,

InputArray hierarchy为轮廓结构信息,

int maxLevel为maxLevel

Point offset=Point()//轮廓位移,默认是0

 

流程:

彩图转灰度图

灰度图转二值图像

发现轮廓

绘制轮廓

#include"pch.h"
#include<iostream>
#include<math.h>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

Mat gray_pic;
int threshold_value = 100;
int threshold_max = 255;
void DemoContours(int, void*);
int main(int argc, char**argv)
{
    Mat src, dst;
    src = imread("2.jpg");
    namedWindow("output window", CV_WINDOW_AUTOSIZE);
    imshow("output window", src);
    cvtColor(src, gray_pic, COLOR_BGR2GRAY);
    createTrackbar("Threshold value", "output window", &threshold_value, threshold_max, DemoContours);
    DemoContours(0, 0);
    waitKey(0);
    return 0;
}

void DemoContours(int, void*)
{
    Mat canny_output;
    vector<vector<Point>> points;
    vector<Vec4i>hierachy;
    Canny(gray_pic, canny_output, threshold_value, threshold_value * 2, 3, false);
    findContours(canny_output, points, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
    Mat dst = Mat::zeros(gray_pic.size(), CV_8UC3);
    RNG rng(12345);
    for (size_t i = 0; i < points.size(); ++i)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(dst, points, i, color, 2, 8, hierachy, 0, Point(0, 0));
    }
    imshow("rslt", dst);
}

 

标签:发现,Point,int,value,threshold,output,轮廓
来源: https://blog.51cto.com/u_12870633/2812915

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有