当前位置:网站首页>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)
边栏推荐
- Selenium -- Web automated testing tool
- 贪心——55. 跳跃游戏
- Qt:qmessagebox message box, custom signal and slot
- [image classification] 2021 MLP mixer nips
- Common weak network testing tools
- Qt:QMessageBox消息框、自定义信号和槽
- 一文读懂Plato Farm的ePLATO,以及其高溢价缘由
- Leetcode 0141. circular linked list - three solutions
- [openvx] VX for basic use of objects_ image
- Read Plato farm's eplato and the reason for its high premium
猜你喜欢

超好用的 PC 端长截图工具

Dynamic planning - 62. Different paths

Implementation of online rental system based on SSM

Leetcode brush question: dynamic planning 09 (weight of the last stone II)

Protocols in swift

After 95, Alibaba P7 published the payroll: it's really heartbreaking

动态规划——63. 不同路径 II

常用的接口测试工具

Prefix-Tuning: Optimizing Continuous Prompts for Generation

ES6 from getting started to mastering 09: symbol type
随机推荐
Screenshot of deepstream detection results
Leetcode 0141. circular linked list - three solutions
贪心——122. 买卖股票的最佳时机 II
【力扣】1337.矩阵中战斗力最弱的k行
Fourier series
Summary and interpretation of CONDA virtual environment
Dynamic programming - 474. One and zero
Differences among BRD, MRD and PRD
Prefix-Tuning: Optimizing Continuous Prompts for Generation
数据挖掘-01
What is tor? What is the use of tor browser update?
Greedy - 53. Maximum subarray sum
leetcode刷题:动态规划08(分割等和子集)
Crowdfunding platform system based on JSP & Servlet
BRD,MRD,PRD的区别
LeetCode_409_最长回文串
An article grasps the calculation and processing of date data in PostgreSQL
The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
Push chart written by helm to harbor warehouse
巧用栈回溯,帮你快速定位问题