本文链接:https://blog.csdn.net/ling_xiobai/article/details/79691785
今天使用opencv运行一个脚本,但是出现cv2.cv.BoxPoints()函数有错,提示’module’ object has no attribute ‘cv’ 的错误,找了一些帖,发现最多的解决方法就是说“这是因为opencv3已经没有了cv,只能重新下载opencv2 ; ”,我就纳闷了,有新的版本不用,为什么还要下载旧的版本。有的更是建议“from cv2 import cv as cv”这是开玩笑吧,根本不起作用,根本原因就是版本不支持。但是我只是想用这个函数来找出坐标而已,重新下载一个opencv,感觉好麻烦,最后去stackflow硬是锻炼了一下英文参考链接,偶然发现原来opencv3的版本中只是改了一点,所以:

如果你只是想用这个函数的功能,又不想装opencv2,那么只需把cv2.cv.BoxPoints()函数改为cv2.boxPoints()。
#顺便提一下,版本语法不兼容的错误经常会出现,比如python2 vs python3 , opencv2 vs opencv3 ,

也不建议大家要一根筋去把他们的差异背下来,只需要出现问题的时候去网上找就行。

附加:
有时候写findContours函数时会遇上too many values to unpack (expected 2),也是版本的原因:

cv2.findContours()函数

函数的原型为

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset ]]])  

opencv2返回两个值:contours,hierarchy。注:opencv3会返回三个值,分别是img, countours, hierarchy

参考文章:https://blog.csdn.net/hjxu2016/article/details/77833336

解析差异:

    OpenCV2和OpenCV4中:

      findContours这个轮廓提取函数会返回两个值:

      ①轮廓的点集(contours)

      ②各层轮廓的索引(hierarchy)

    OpenCV3中:

      则会返回三个值:

      ①处理的图像(image)

      ②轮廓的点集(contours)

      ③各层轮廓的索引(hierarchy)

三、cv2.findContours()相关:

1、参数:

①第一个参数:

寻找轮廓的图像
 

②第二个参数表示轮廓的检索模式,有四种:

cv2.RETR_EXTERNAL 只检测外轮廓
cv2.RETR_LIST 检测的轮廓不建立等级关系
cv2.RETR_CCOMP 建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息
cv2.RETR_TREE 建立一个等级树结构的轮廓
 

③第三个参数method为轮廓的近似办法:

cv2.CHAIN_APPROX_SIMPLE 压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标(矩形只需四顶点)
cv2.CHAIN_APPROX_TC89_L1 使用teh-Chinl chain 近似算法
CV_CHAIN_APPROX_TC89_KCOS
cv2.CHAIN_APPROX_NONE 存储所有的轮廓点,相邻的两个点的像素位置差不超过1
# USAGE
# python detect_barcode.py --image images/barcode_01.jpg # import the necessary packages
import numpy as np
import argparse
import cv2 # construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "path to the image file")
args = vars(ap.parse_args()) # load the image and convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # compute the Scharr gradient magnitude representation of the images
# in both the x and y direction
gradX = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 1, dy = 0, ksize = -1)
gradY = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 0, dy = 1, ksize = -1) # subtract the y-gradient from the x-gradient
gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient) # blur and threshold the image
blurred = cv2.blur(gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY) # construct a closing kernel and apply it to the thresholded image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) # perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations = 4)
closed = cv2.dilate(closed, None, iterations = 4) # find the contours in the thresholded image, then sort the contours
# by their area, keeping only the largest one
(kkk, cnts, lll) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key = cv2.contourArea, reverse = True)[0] # compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect)) # draw a bounding box arounded the detected barcode and display the
# image
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)

  

 https://www.cnblogs.com/guobin-/p/10842486.html  

最新文章

  1. HTML5本地存储——IndexedDB(一:基本使用)
  2. sharepoint 2013 开发环境安装
  3. [转]C服务端与java客户端的socket通信注意事项
  4. tableView的总结
  5. Javascript中关于作用域和闭包和域解释的面试题
  6. win7系统下dos界面无法自由调整大小
  7. Java NIO 系列学习(一)Java NIO概述
  8. tableview分割线
  9. windows环境下安装Python的Rtree包
  10. zmq setsockopt()
  11. java用星星符号打印出一个直角三角形
  12. Android性能优化-App启动优化
  13. AudioUnit录音和播放同时进行的一些注意点
  14. mysql实现分页的几种方式
  15. java自学-编程入门
  16. JavaIO流(输入输出操作)
  17. 如何快速解决MySQL 1032 主从错误
  18. 斯坦福大学卷积神经网络教程UFLDL Tutorial - Convolutional Neural Network
  19. python装饰器中functools.wraps的作用详解
  20. ie8下使用bootstrap不能显示icon的图标

热门文章

  1. MES应用案例 | 天博集团成功完成数字化转型
  2. SAP Cloud Platform integration上创建一个最简单的iFlow
  3. TP5日志打印方法封装
  4. CentOS7安装rpm包时依赖检测失败,加上--nodeps --force
  5. 计算python脚本的运行时间
  6. 实例讲解ip地址、子网掩码、可用地址范围的计算
  7. java 的任意进制间转换(很方便)
  8. Mybatis框架进行批量导入和删除有三种方式
  9. 项目Beta冲刺总结随笔
  10. 22、pandas表格、文件和数据库模块