Python OpenCV TypeError:轮廓不是一个numpy数组,也不是一个标量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17628627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV TypeError: contour is not a numpy array, neither a scalar
提问by Omar Tariq
I'm trying to use OpenCV to extract tags from Nike images. This is a tutorial code taken from:
我正在尝试使用 OpenCV 从 Nike 图像中提取标签。这是一个教程代码,取自:
http://opencv-code.com/tutorials/ocr-ing-nikes-new-rsvp-program/
http://opencv-code.com/tutorials/ocr-ing-nikes-new-rsvp-program/
I've modified few lines of code though and there is no error in that part (not sure if it's working because I haven't been able to successfully completely run it.)
不过,我修改了几行代码,该部分没有错误(不确定它是否有效,因为我无法成功完全运行它。)
When I run command 'python a.py'. This error is displayed:-
当我运行命令“python a.py”时。显示此错误:-
Traceback (most recent call last):
File "a.py", line 42, in <module>
otcnt = [c for c in cnt if cv2.contourArea(c) < 100]
TypeError: contour is not a numpy array, neither a scalar
a.py:-
a.py:-
#!/usr/bin/env python
import numpy as np
import cv2
import cv2.cv as cv
def do_ocr(img0):
pass
if __name__ == "__main__":
img0 = cv2.imread('nike-1.jpg')
if img0 == None:
import sys
sys.exit()
do_ocr(img0)
img1 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
img2 = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 45, 0)
size = np.size(img2)
skel = np.zeros(img2.shape,np.uint8)
ret,img2 = cv2.threshold(img2,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
done = False
while( not done):
eroded = cv2.erode(img2,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img2,temp)
skel = cv2.bitwise_or(skel,temp)
img2 = eroded.copy()
zeros = size - cv2.countNonZero(img2)
if zeros==size:
done = True
img3 = img2
img4 = cv2.copyMakeBorder(img3, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
cv2.floodFill(img4, None, (0,0), 255)
img5 = cv2.erode(255-img4, np.ones((3,3), np.uint8), iterations=2)
cnt = cv2.findContours(img5, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
cnt = [c for c in cnt if cv2.contourArea(c) > 5000]
mask = np.zeros(img0.shape[:2], np.uint8)
cv2.drawContours(mask, cnt, -1, 255, -1)
dst = img2 & mask
cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
otcnt = [c for c in cnt if cv2.contourArea(c) < 100]
cv2.drawContours(dst, otcnt, -1, 0, -1)
api = tesseract.TessBaseAPI()
api.Init(".", "eng", tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "#ABCDEFGHIJKLMNOPQRSTUVWXYZ")
api.SetPageSegMode(tesseract.PSM_SINGLE_LINE)
image = cv.CreateImageHeader(dst.shape[:2], cv.IPL_DEPTH_8U, 1)
cv.SetData(image, dst.tostring(), dst.dtype.itemsize * dst.shape[1])
tesseract.SetCvImage(image, api)
print api.GetUTF8Text().string()
I'm very new to Python programming (and Python syntax) and I've to do this in Python anyway. I shall be very thankful to you if you can either post the complete correct version of it or finger point which line of code should be replaced with which one.
我对 Python 编程(和 Python 语法)非常陌生,无论如何我都必须在 Python 中执行此操作。如果您可以发布它的完整正确版本或指出哪一行代码应该替换为哪一行,我将非常感谢您。
Thanks
谢谢
p.s. My first question at stackoverflow so apologies if not following any convention.
ps 我在 stackoverflow 上的第一个问题很抱歉,如果没有遵循任何约定。
采纳答案by Sajjan Singh
This runtime error is caused by the fact that when you redefine cnt
on line 42 as
此运行时错误是由以下事实引起的:当您cnt
在第 42 行重新定义为
cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
you are setting it is the tuple of the two return values of cv2.findContours
. Look at your earlier call to the function on line 37 as a guide. All you need to do is change line 42 to
您正在设置它是 的两个返回值的元组cv2.findContours
。以您之前对第 37 行的函数调用为指导。您需要做的就是将第 42 行更改为
cnt = cv2.findContours(dst.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
Your next issue is that you haven't imported tesseract
.
您的下一个问题是您尚未导入tesseract
.