当前位置:网站首页>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)
边栏推荐
- VBA reads the create document of SQL in batches to create tables
- 贪心——55. 跳跃游戏
- C语言:不创建临时变量实现两数交换
- LightPicture – 精致图床系统
- [openvx] VX for basic use of objects_ convolution
- ES6 from getting started to mastering 08: extended object functions
- Summary and interpretation of CONDA virtual environment
- Crowdfunding platform system based on JSP & Servlet
- Vertical align align the elements in the row are vertically centered
- Leetcode58. Length of the last word
猜你喜欢

LightPicture – 精致图床系统

Weekly recommended short video: how to correctly understand the word "lean"?

Capacity expansion and reduction of RBD block storage device (VI)

常用的弱网测试工具
![[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix](/img/6c/b5fd3350886fd74557439f5361e7f8.png)
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix

LabVIEW loads and uses custom symbols in tree control projects

基于SSM实现在线租房系统

Crowdfunding platform system based on JSP & Servlet

数据丰富的计算:M.2在边缘遇到AI

【原型与原型链】初识原型与原型链~
随机推荐
Jumping game II in question 45 of C language power deduction. Ergodic jump
贪心——122. 买卖股票的最佳时机 II
Read Plato farm's eplato and the reason for its high premium
Leetcode 0141. circular linked list - three solutions
Crowdfunding platform system based on JSP & Servlet
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
Common interface testing tools
CH340 RTS DTR引脚编程驱动OLED
[openvx] VX for basic use of objects_ distribution
deepstream 检测结果截图
BRD,MRD,PRD的区别
Dynamic programming - 416. Segmentation and subsets
Ch340 RTS DTR pin programming drives OLED
[错题]Mocha and Railgun
95后阿里P7晒出工资单:真的是狠狠扎心了...
Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
After 95, Alibaba P7 published the payroll: it's really heartbreaking
贪心——53. 最大子数组和
Xctf attack and defense world web master advanced area php2