首页 > 编程知识 正文

cv2.cvtColor()的功能与参数,opencv轮廓抖动

时间:2023-05-03 12:21:15 阅读:163596 作者:3560

1cv2.findContours ()在OpenCV-Python界面中,cv2.findContours ) )函数查找检测对象的轮廓。

cv2.findcontours(image,mode,method,contours=None,hierarchy=None, offset=none (3358 www.Sina.com/http://www.Sina.com/image查找轮廓的图像时,http://www.Sina.com

opencv3返回img、countours和hierarchy三个值

mode轮廓的检索模式有4种method轮廓逼近方法,4种contours利用findContours检测到的轮廓数据,每个轮廓都以点矢量的形式保存, point类型的vectorhierarchy选项层次结构信息offset选项轮廓偏移参数是用于绘制偏移量offset(dy )轮廓的偏移量http://www.Sina.com/http://www.Sina cv2.retr_external仅检测外轮廓cv2.RETR_LIST内孔内存在连通物体时,该物体的边界也位于最上层。 cv2.RETR_TREE检测所有轮廓,建立完整的分层结构,网状轮廓结构http://www.Sina.com/method 3358 www.Sina.com/cv2.chain_approx_none建立以获取每个轮廓的像素的相邻两个点的像素差是1cv2.CHAIN_APPROX_SIMPLE压缩的水平方向、垂直方向和对角线方向上的元素,其中值保持该方向上的重点坐标。 矩形轮廓仅4点即可保存轮廓信息时cv2.chain _ approx _ tc89 _ l1teh-chini chain近似算法cv2.chain _ approx _ tc89 _ kco steh-chini chain近似算法函数首先返回list。 list中的每个元素都是图像中的轮廓信息,而list中的每个元素(轮廓信息)类型为ndarray。 len(contours[1] )表示保存在第一个轮廓中的元素的数量,即保存在该轮廓中的点的数量。

1.2hierarchy返回值此函数还返回可选的hiararchy结果。 这是ndarray,元素的数量和轮廓的数量相同,每个轮廓contours[i]对应于四个hierarchy元素hierarchy[I][0]~hierarchy[1]

第一个数:表示同一级别轮廓的下一个轮廓的编号。 如果该级别的轮廓没有下一个轮廓,则在该级别轮廓的最后一个时刻,通常为-1。 第二数:表示同一级别轮廓之前轮廓的编号。 如果此级别的轮廓没有上一个轮廓,则在此级别轮廓的第一个轮廓时,通常为-1。 第三个数:表示该轮廓中包含的下一级轮廓的开头的编号。 如果没有-1。 第四个数:表示其轮廓上一个轮廓的编号。 如果没有一个以上的轮廓的话-1。 1.3板栗参数

3358 www.Sina.com/import cv 2img=cv2.im read (' ./Davis-2016/annotations/1080 p/flamingo/0007.png ' 16 ) cv2.CHAIN_APPROX_SIMPLE ) print ) len ) returns ) ) print ) cv2.chain_appprox_s returns的长度为三个,returns将三个值确定为这是因为opencv3版本的cv2.findContours )函数的返回值为三个,并且都是元组类型,因此在按一个参数接收时会接收三个值。 (opencv2版本只返回两个值。)。

描述

import cv2img

= cv2.imread('../../DAVIS-2016/Annotations/1080p/flamingo/00007.png', 0)img = cv2.resize(img, (16, 16)) # 为了方便显示,调整下尺寸# print(img.shape)return1, return2 = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# print(returns)# print(len(returns))# print(type(returns))# resultsValueError: too many values to unpack (expected 2)

可以看出,代码执行错误。ValueError: too many values to unpack (expected 2)

以三个参数去接收返回值,每个参数各接收一个返回值import cv2img = cv2.imread('../../DAVIS-2016/Annotations/1080p/flamingo/00007.png', 0)img = cv2.resize(img, (16, 16))# print(img.shape)image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)print(image)print(contours)print(hierarchy)

输出结果:

[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 255 255 0 255 255 0 0 0] [ 0 0 0 0 0 0 0 255 255 255 0 0 0 0 0 0] [ 0 0 0 0 0 0 255 255 255 0 255 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]][array([[[ 8, 10]]], dtype=int32), array([[[11, 5]], [[12, 5]]], dtype=int32), array([[[ 9, 4]], [[ 6, 7]], [[ 8, 7]], [[ 9, 6]], [[10, 7]], [[ 9, 6]]], dtype=int32)][[[ 1 -1 -1 -1] [ 2 0 -1 -1] [-1 1 -1 -1]]] 第一个返回值,返回了所处理的图像第二个返回值就是我们要找的轮廓的点集print(type(contours)) # 输出为:<class 'list'>print(type(contours[0])) # 输出为:<class 'numpy.ndarray'>print(len(contours)) # 图像中轮廓的数量,输出为:3print(len(contours[0])) # 轮廓0中的元素(点)的数量,输出为:1# results<class 'list'><class 'numpy.ndarray'>31   第三个返回值为各层轮廓的索引print(hierarchy)# results[[[ 1 -1 -1 -1] [ 2 0 -1 -1] [-1 1 -1 -1]]]

具体意义不做解释,可查看1.2,自行理解。

2 cv2.drawContours()

OpenCV中通过cv2.drawContours在图像上绘制轮廓。 

cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None) 参数描述返回值image需要绘制轮廓的目标图像,注意会改变原图绘制的图像arraycontours轮廓点,上述函数cv2.findContours()的第一个返回值contourIdx轮廓的索引,表示绘制第几个轮廓,-1表示绘制所有的轮廓color绘制轮廓的颜色thickness(可选参数)轮廓线的宽度,-1表示填充lineType可选参数)轮廓线型,包括cv2.LINE_4,cv2.LINE_8(默认),cv2.LINE_AA,分别表示4邻域线,8领域线,抗锯齿线(可以更好地显示曲线)hierarchy(可选参数)层级结构,上述函数cv2.findContours()的第二个返回值,配合maxLevel参数使用maxLevel(可选参数)等于0表示只绘制指定的轮廓,等于1表示绘制指定轮廓及其下一级子轮廓,等于2表示绘制指定轮廓及其所有子轮廓offset(可选参数)轮廓点的偏移量3 实例 读入的为二值图import cv2import numpy as np# 读入的为二值图img = cv2.imread('../../DAVIS-2016/Annotations/1080p/flamingo/00007.png')img = cv2.resize(img, (224, 224))# 灰度图img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 获取轮廓image, contours, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 创建白色幕布,画图temp = np.ones(img_gray.shape, np.uint8) * 255# 画轮廓:temp为白色幕布,contours为轮廓, -1表示画所有轮廓,颜色:绿色,厚度cv2.drawContours(temp, contours, -1, (0, 255, 0), 3)cv2.imshow('pic', img)cv2.waitKey()cv2.imshow('contour', temp)cv2.waitKey()

结果:

读入RGB图像:灰度化、二值化

原图:

import cv2import numpy as np# 读入RGB图像图像img = cv2.imread('../../DAVIS-2016/JPEGImages/1080p/flamingo/00007.jpg')img = cv2.resize(img, (224, 224))# 灰度化img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 二值化ret, binary = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)# 获取轮廓image, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 白色幕布temp = np.ones(img_gray.shape, np.uint8) * 255# 绘制轮廓,在白色幕布上绘制,也可以在原图上绘制cv2.drawContours(temp, contours, -1, (0, 0, 255), 3)cv2.imshow('img', img) # 原图cv2.waitKey()cv2.imshow('gray', img_gray) # 灰度图cv2.waitKey()cv2.imshow('binary', binary) # 二值图cv2.waitKey()cv2.imshow('result', temp)cv2.waitKey()

结果:

在白色幕布上绘制

在原图上绘制

参考:

https://blog.csdn.net/Easen_Yu/article/details/89365497https://blog.csdn.net/keith_bb/article/details/70185209https://blog.csdn.net/hjxu2016/article/details/77833336

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。