当前位置:网站首页>47_ Contour lookup in opencv cv:: findcontours()
47_ Contour lookup in opencv cv:: findcontours()
2022-07-07 16:15:00 【sinat_ forty-one million seven hundred and fifty-two thousand t】
A contour corresponds to a series of points , These points represent a curve in the image in some way .OpenCV in , Contour with standard template library vector vector<> Express , The most common is to use a series of two-dimensional vertices (vector<cv::Point> or vector<cv::Point2f> Express .
function cv::findContours() Calculate the contour from the two-dimensional image , It can process images from cv::Canny() Function to get the image with edge pixels , Or from cv::threshold() And cv::adaptiveThreshold() Image obtained by function .
1. Find the outline cv::findContours
cv::findContours() The function prototype :
void cv::findContours(
cv::InputOutputArray image, // input binary 8-bit single channel
cv::InputOutputArrayOfArrays contours, // vector of vectors or points
cv::OutputArray hierarchy, // (optional) topology information
int mode, // contour retrieval mode
int method, // approximation method
cv::Point offset = cv::Point() // (optional) offset every point
);
void cv::findContours(
cv::InputOutputArray image, // input binary 8-bit single channel
cv::OutputArrayOfArrays contours, // vector of vectors or pointd
int mode, // contour retrieval mode
int method, // approximation method
cv::Point offset = cv::Point() // (optional) offset every point;
);Parameters image It's the input image , Must be 8 Bit single channel image , Should be transformed into binary Of .cv::findContours() Function will change the parameter , So if the image will be useful in the future , It should be copied and then transmitted to cv::findContours().
Parameters contours It's a set of arrays , In most cases, it is one or more standard template libraries vector. This parameter is the contour found . For example, in an outline vector in ,contours[i] It's an outline , and contours[i][j] It's the outline contours[i] A point on .
Parameters hierarchy Optional. , If this parameter is given ,hierarchy The tree structure of all contours will be output . This parameter is an array , Each contour corresponds to a value in the array . Each value in the array is a quaternion array , Each element represents a node with a specific link to the current node , The meaning of each element is as follows :
| Indexes | meaning |
| 0 | The next contour of the same level |
| 1 | The previous contour of the same level |
| 2 | The first child node of the lower level |
| 3 | Parent node of the parent |
Parameters mode Represents the desired contour extraction method , Yes 4 Kind of :
- cv::RETR_EXTERNAL: Retrieve only the outermost contour , And the contour is not connected with other contours .
- cv::RETR_LIST: Retrieve all contours and save them to the table .
- cv::RETR_CCOMP: Retrieve all the contours , And organize them into a double-layer structure .
- cv::RETR_TREE: Retrieve all contours and rebuild the mesh contour structure .
Parameters method Indicates how the outline is expressed , The options are :
- cv::CHAIN_APPROX_NONE: Convert all points in contour coding into points , This operation will produce a large number of points , Each point will become the 8 One of the adjacent points , Will not reduce the number of points returned .
- cv::CHAIN_APPROX_SIMPLE: Compression level 、 vertical 、 The oblique part , Keep only the last point , In many special cases , This operation will greatly reduce the number of returned points . An extreme example is , For one along x-y Rectangle in direction , Only return 4 A little bit .
- cv::CHAIN_SPPROX_TC89_L1 or cv::CHAIN_APPROX_TC89_KCOS: Use Teh-chin One of the chain approximation algorithms .Teh-Chin Algorithm is a more complex and computationally intensive Algorithm , Used to reduce the number of points returned . function T-C No additional parameters are required .
Parameters offset Optional. , If this parameter is given , All points in the returned contour will be offset according to the parameter value . Usually used in two cases : When the contour extracted from the region of interest is expressed in the original coordinate system , Second, when the contour extracted from the original image is expressed in the image sub region coordinate system .
2. Draw the outline cv::drawContours
After finding the contour, the most common function is to draw the detected contour on the screen , You can use functions cv::drawContours() Function completion . The function prototype :
void cv::drawContours(
cv::InputOutputArray image, // will draw on input image
cv::InputArrayOfArrays contours, // vector of vectors or pointd
int contourIdx, // contour to draw (-1 is all)
const cv::Scalar &color, // color for contours
int thickness = 1, // thickness for contour lines
int lineType = 8, // connectedness ('4' or '8')
cv::InputArray hierarchy = cv::noArray(), // optional from find contours
int maxLevel = INT_MAX, // max descent in hierarchy
cv::Point offset = cv::Point() // (optional) offset all points
);Parameters image, The image to be outlined .
Parameters contour Is the outline to be drawn , The type of this parameter is the same as cv::findContours() Output contour identical , Is the point stored in the list .
Parameters contourIdx Used to tell cv::drawContours() What needs to be drawn is contours Whether a certain contour or all contours in the parameter , If it's a positive number , Then the corresponding contour will be drawn , If it's negative , All outlines will be drawn .
Parameters color、thickness、lineType The function of is the same as that of the corresponding parameters in other functions used for drawing , Respectively represent the color of painting , The thickness of the drawn line , Type of sketch line ( Four Unicom / Eight connections /AA Line ).
Parameters hierarchy Corresponding cv::findContours() Level of function output . Parameters hierarchy And parameters maxLevel Work together .maxLevel Limit the depth of the contour hierarchy that will be drawn on the graph ,maxLevel=0 It means to draw only the second 0 The outline of the layer , Set to other non 0 Positive numbers , It means drawing the contour of the same number of levels below the highest level . If you want to display only the outermost outline when connecting components , Very helpful .
Parameters offset Optional , When the contour coordinate system is converted into the execution coordinate system or other local coordinate system , This feature is very useful .
Use VS2010+opencv2.4.9 when ,32 Bit tests always crash , Prompt that the heap is broken , And there is also a problem with the contour obtained by the function , use 64 Bit won't crash , The contour obtained is also correct .
3. Using examples : Find the outline and draw it one by one
#include <opencv.hpp>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
struct AreaCmp{
AreaCmp(const vector<float>& _areas):areas(&_areas){}
bool operator()(int a,int b) const {return (*areas)[a] > (*areas)[b];}
const vector<float>* areas;
};
int main(int argc, char *argv[])
{
Mat img,img_edge,img_color;
img = cv::imread(argv[1],cv::IMREAD_GRAYSCALE);
if(img.empty())
{
std::cout << "Load image fail," << argv[1] << std::endl;
getc(stdin);
return -1;
}
cv::threshold(img,img_edge,128,255,cv::THRESH_BINARY);
cv::namedWindow("Image after threshold",cv::WINDOW_NORMAL);
cv::imshow("Image after threshold",img_edge);
cv::vector< cv::vector<cv::Point> > contours;
vector<cv::Vec4i> hierarchy;
// Find the outline
cv::findContours(img_edge,contours,hierarchy,cv::RETR_LIST,cv::CHAIN_APPROX_SIMPLE);
cout << "\nTotal contours detected:" << contours.size() << endl;
vector<int> sortIdx(contours.size());
vector<float> areas(contours.size());
for(int n=0;n<(int)contours.size();n++)
{
sortIdx[n] = n;
areas[n] = contourArea(contours[n],false); // Calculate the contour area
}
// sort contours so that largest contours go first
std::sort(sortIdx.begin(),sortIdx.end(),AreaCmp(areas));
for(int n=0;n<(int)sortIdx.size();n++)
{
int idx = sortIdx[n];
cv::cvtColor(img,img_color,cv::COLOR_GRAY2BGR);
// Draw the outline
cv::drawContours(img_color,contours,idx,cv::Scalar(0,0,255),2,8,hierarchy,0); // Draw the outline one by one
cout << "Contour #" << idx << ": area=" << areas[idx] << ", nvertices=" << contours[idx].size() << endl;
cv::namedWindow(argv[0],cv::WINDOW_NORMAL);
cv::imshow(argv[0],img_color);
int k;
if((k = cv::waitKey()&255) == 27)
break;
}
std::cout << "Finished all contours\n" ;
getc(stdin);
return 0;
}边栏推荐
- 应用程序和matlab的通信方式
- Logback logging framework third-party jar package is available for free
- Three. JS introductory learning notes 15: threejs frame animation module
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- Leetcode-231-2的幂
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
- AE learning 01: AE complete project summary
- Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
- Laravel5.1 路由 -路由分组
- Numpy -- epidemic data analysis case
猜你喜欢

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

numpy---基础学习笔记

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation

Continuous creation depends on it!

Vite path alias @ configuration

模仿企业微信会议室选择

保证接口数据安全的10种方案

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

Talk about the cloud deployment of local projects created by SAP IRPA studio

Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
随机推荐
The unity vector rotates at a point
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
You Yuxi, coming!
Mysql database backup script
numpy---基础学习笔记
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
无线传感器网络--ZigBee和6LoWPAN
Unity3d click events added to 3D objects in the scene
Step by step monitoring platform ZABBIX
Performance measure of classification model
How to implement backspace in shell
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
How does geojson data merge the boundaries of regions?
Odoo integrated plausible embedded code monitoring platform
强化实时数据管理,英方软件助力医保平台安全建设
模仿企业微信会议室选择
JS array foreach source code parsing
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Migration and reprint
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des