当前位置:网站首页>Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv
Opencv (13): brief introduction to cv2.findcontours, cv:: findcontours and description of cv2.findcontours function in various versions of opencv
2022-07-23 18:40:00 【biter0088】
Catalog
1.2 cv2.findContours、cv::findContours Function introduction
2. opencv Various versions cv2.findContours explain
3 cv2.findContours Examples of use :
1. findContours function
1.1. outline contours
A contour can be simply interpreted as connecting all continuous points ( Along the border ) The curve of , Having the same color or intensity . Contour is a useful tool for shape analysis and object detection and recognition .
· For better accuracy , Please use Binary image . So before looking for the outline , Apply threshold or precise edge detection .
·findContours Function to modify the source image . therefore , If you still want the source image after finding the contour , Please store it in some other variables .
· stay OpenCV in , Looking for outlines is like looking for white objects against a black background . So remember , The object to be found should be white , The background should be black . If there are other pretreatment methods , Just contrast the area where the contour is located with other areas as far as possible
1.2 cv2.findContours、cv::findContours Function introduction
function :
Looking for contours in binary images .
This function retrieves the contour from the binary image . Contour is a useful tool for shape analysis and object detection and recognition .
matters needing attention :
The source image is modified by this function .
Besides , This function does not consider the 1 Pixel boundary ( It USES 0 Fill in and use it for neighborhood analysis in the algorithm ), Therefore, the outline touching the image boundary will be clipped .
c++ Construction form 1:
void cv::findContours ( InputOutputArray image,
OutputArrayOfArrays contours,
OutputArray hierarchy,
int mode,
int method,
Point offset = Point()
) c++ Construction form 2:
void cv::findContours ( InputOutputArray image,
OutputArrayOfArrays contours,
int mode,
int method,
Point offset = Point()
) 1.3 Parameter description :
image:
The source image ,8 Bit single channel image . Non zero pixels are considered 1. Zero pixels remain 0, Therefore, the image is regarded as binary . You can use compare 、 inRange 、 threshold 、adaptiveThreshold 、 Canny etc. Create a binary image from a grayscale or color image . This function modifies the image while extracting the contour . If mode be equal to RETR_CCOMP or RETR_FLOODFILL, Then the input can also be a label 32 Bit integer image (CV_32SC1).
contours:
Detected contours . Each contour is stored as a point vector .
hierarchy:
Optional output vector , Contains information about the image topology . It has as many elements as the number of contours . For every second i A profile contours[i] , Elements hierarchy[i][0] 、 hiearchy[i][1] 、 hiearchy[i][2] and hiearchy[i][3] Set to 0- Index in the contour based on the next and previous contour of the same level , They are the first sub contour and the parent contour . If the contour i There's no next one 、 the previous 、 Parent or nested profiles , be hierarchy[i] The corresponding element of will be negative .
mode:
Contour retrieval mode , see cv::RetrievalModes
method:
Contour approximation method , see cv::ContourApproximationModes
offset:
Optional offset for each contour point movement . If from the image ROI Extract contour from , Then it should be analyzed in the whole image context , It will be useful .
2. opencv Various versions cv2.findContours explain
2.1 opencv3.x
image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])Three parameters , Note the order of the parameters
2.2 opencv2.x and 4.x
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])Two parameters , Note the order of the parameters
3 cv2.findContours Examples of use :
3.1 opencv3.x edition
The test image :test.png, Two pictures , The outline in the left figure will not be found, resulting in an error in the program , The right picture can be found


python Code :
# -*- coding:utf-8 -*-
import numpy as np
import cv2
im = cv2.imread('test2.png') ## Read in color picture , Used for the following display rectangle
cv2.imshow('im', im) ##hxz
cv2.waitKey(0)##hxz
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Find the largest outline
area = []
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
rect = cv2.minAreaRect(contours[max_idx])
points = cv2.boxPoints(rect) # Get the coordinates of the four points of the smallest circumscribed rectangle
points = np.int0(points) # Coordinate values are rounded
image_show = cv2.drawContours(im, [points], 0, (0 ,0 , 255), 4) ##hxz Red is more obvious
cv2.imshow('image_show', image_show) ##hxz
cv2.waitKey(0)##hxzProgram execution effect

4. Reference link
opencv3.2 Contours Official documents : OpenCV: Contours : Getting Started
Change the arrow in the following figure to view other opencv The content of the version

staclflow About cv2.findContours Return the answer with an error :
python - compatibility issue with contourArea in openCV 3 - Stack Overflow
cv2.findContours Function description OpenCV: Structural Analysis and Shape Descriptors
边栏推荐
- [sharing game modeling model making skills] how ZBrush adjusts the brush size
- Tampermonkey究竟有什么用?
- JS 将伪数组转换成数组
- Deep learning learning record - update of learning rate of optimizer
- Flutter operation mode
- JS convert pseudo array to array
- Spark installation and startup
- [jzof] 13 motion range of robot
- Gradle【图文安装及使用演示 精讲】
- 类的基础
猜你喜欢

Rhcsa Notes 6

Flame Graphs 火焰图安装与使用
![[sharing game modeling model making skills] how ZBrush adjusts the brush size](/img/12/4c9be15266bd01c17d6aa761e6115f.png)
[sharing game modeling model making skills] how ZBrush adjusts the brush size

一文了解 NebulaGraph 上的 Spark 项目

【2018】【论文笔记】石墨烯场效应管及【2】——石墨烯的制备、转移

Foundation of class

学次世代建模是场景好还是角色好?选对职业薪资多一半

ResponseBodyAdvice接口使用导致的报错及解决
![[attack and defense world web] difficulty Samsung 9-point introductory question (end): Fakebook, favorite_ number](/img/f7/e7848a8aa70ed34b166716815617e0.png)
[attack and defense world web] difficulty Samsung 9-point introductory question (end): Fakebook, favorite_ number

【游戏建模模型制作技巧分享】ZBrush如何调整笔刷大小
随机推荐
一文了解 NebulaGraph 上的 Spark 项目
【ONNX】动态输入尺寸的问题(多输出/多输入)
作为3D资深建模师给刚入门学习伙伴的一些建议,用起来
SQLZOO——SELECT from Nobel Tutorial
如何评估券商分析师预测股票涨跌的准确性?
Database modeling
Jetpack Compose之Navigation组件使用
Time frequency domain analysis of 20220721 integral link
Interface test overview
[jzof] 13 motion range of robot
Rhcsa Notes 6
How to become a modeler? Which is more popular, industrial modeling or game modeling?
Jetty 服务器的 NIO 机制是如何导致堆外内存溢出的
银行业如何实现数字化转型风口全速启航
JUC并发编程【详解及演示】
rhcsa笔记三
Problems and methods of creating multiple projects under one solution in VS2010
MySQL performance tuning
【Coggle 30 Days of ML】糖尿病遗传风险检测挑战赛(2)
大神“魔改”AirPods,配备USB-C接口,3D打印外壳让维修更容易