OpenCV2 与opencv3 问题解决

本文链接: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  

posted @ 2019-11-29 12:17  静悟生慧  阅读(1226)  评论(0编辑  收藏  举报