当前位置:网站首页>CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing
CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing
2022-07-28 03:52:00 【MAR-Sky】
Parameters and meaning cv2.threshold
threshold(src, thresh, maxval, type[, dst]) -> retval, dst, The first return value is ** threshold ** Of float type , The second return value is the result of image pixel processing .
cv2.threshold(img,threshold,maxval,type)
img: It has to be grayscale , yes 8-bit or 32float Type ,
threshold: Judgment threshold of pixels
dst: The resulting image is the same size and channel as the original image ,
**maxval:** Maximum threshold , stay THERSH_BINARY and THERSH_BINARY_INV In the mode of 
Here are the changes of pixel values in different modes .

cv2.findContours(), Find the edge outline of the binary image
cv2.findContours(image, mode, method[, contours[, hierarchy[, offset ]]]) → image, contours, hierarchy
img: The original image is a 8-bit single-channel image
mode : Represents different algorithms for obtaining contours
contours: It represents the vector of multiple contours in the image , Each contour is composed of a vector pointer
Return value ,
image - Processed image
contours - The point set of the detected contour
hierarchy - Index of each layer outline
void cv::findContours ( InputOutputArray image,
OutputArrayOfArrays contours,
OutputArray hierarchy,
int mode,
int method,
Point offset = Point()
)
The retrieval methods of contour include the following :
Reference resources :https://www.csdn.net/tags/NtzakgxsODI4NDQtYmxvZwO0O0OO0O0O.html
Contour retrieval method
mode meaning
cv2.RETR_EXTERNAL Only detect Outline Information
cv2.RETR_LIST Between the output contours No hierarchical relationship , With list Form output outline Information
cv2.RETR_CCOMP Output Two layers of contour information , That is, the inner and outer boundaries . The upper layer is the outer boundary , The inner layer is the boundary information of the inner hole
cv2.RETR_TREE Output contour information in tree structure
Contour approximation
method meaning
cv2.CHAIN_APPROX_NONE Store all boundary points
cv2.CHAIN_APPROX_SIMPLE Compress vertical 、 level 、 Diagonally , Keep only endpoints
cv2.CHAIN_APPROX_TX89_L1 Use teh-Chini The approximate algorithm
cv2.CHAIN_APPROX_TC89_KCOS Use teh-Chini The approximate algorithm
cv2.findContours
Reference resources :https://blog.csdn.net/Easen_Yu/article/details/89380578
cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None)
**image:** Is to indicate on which image the outline is drawn ;image Three channels to show the outline
**contours:** It's the outline itself , stay Python There is a list;
contourIdx: Specifies the sketch profile list Which outline in the , If it is -1, Then draw all the contours in it . The following parameters are simple .
thickness Indicates the width of the contour line , If it is -1(cv2.FILLED), Fill mode
Comprehensive use of examples
import cv2
img = cv2.imread('test4.jpg')
image_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## t Is the set threshold ,binary Is the image obtained by threshold comparison
t, binary = cv2.threshold(image_gray, 127, 255, cv2.THRESH_BINARY)
# There are three return values , Be sure to define three return values , Otherwise, it will be wrong ,ValueError: too many values to unpack
_,contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Draw outline
r = cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('img1',r)
# cv2.imshow('res', img)
k = cv2.waitKey(0)
if k == ord('e'):
exit(0)
边栏推荐
- Input upload file and echo FileReader and restrict the type of file selection
- Developing rc522 module based on c8t6 chip to realize breathing lamp
- C language: find the number of 1 in binary stored in memory as an integer
- Fourier series
- Common weak network testing tools
- 高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
- [force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
- 面试必备杀技:SQL查询专项训练!
- [openvx] VX for basic use of objects_ convolution
- SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
猜你喜欢

Notes on writing questions in sword finger offer

C language: realize the exchange of two numbers without creating temporary variables

Fourier series

8000 word explanation of OBSA principle and application practice

Common interface testing tools

Data rich Computing: m.2 meets AI at the edge

leetcode刷题:动态规划08(分割等和子集)

Swift中的协议

一个仿win10蓝屏的404页面源码

基于SSM实现在线租房系统
随机推荐
Selenium--WEB自动化测试工具
Simple and easy-to-use performance testing tools recommended
8000 word explanation of OBSA principle and application practice
我的创作纪念日
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
MySQL Basics (create, manage, add, delete, and modify tables)
数据挖掘-01
Is there a bonus period for robot engineering
贪心——53. 最大子数组和
Protocols in swift
数据挖掘-02
Common interface testing tools
C语言力扣第45题之跳跃游戏 II。遍历跳跃
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (the last 8 questions)
Mysql基础篇(创建、管理、增删改表)
贪心——122. 买卖股票的最佳时机 II
What is tor? What is the use of tor browser update?
Dynamic programming - 416. Segmentation and subsets
LeetCode_ 409_ Longest palindrome string
常用的弱网测试工具