python - opencv+python : Assertion failure when findcontours

标签 python opencv

我正在从事一个图像处理项目。

我使用的是opencv 2.4.11 + python 2.7

代码很简单

def preprocessing(src):
    src = cv2.resize(src, (0,0), fx=6, fy=6)
    kernel = np.ones((5,5), np.uint8)

    dilate = cv2.dilate(src, kernel, iterations = 1)
    erode = cv2.erode(dilate, kernel, iterations = 1)

    cv2.imshow("dilate", dilate)
    cv2.imshow("erode", erode)

    th, thresh = cv2.threshold(erode, 200, 255, cv2.THRESH_BINARY)
    cv2.imshow("thresh", thresh)

    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(src,contours,-1,(0,255,0),3)

    cv2.imshow("src", src)
    cv2.waitKey()


print "==== start image processing ====="
im2 = cv2.imread("training1.png",cv2.CV_LOAD_IMAGE_GRAYSCALE)
preprocessing(im2)

但是 findcontours 返回断言失败

输出为

OpenCV Error: Assertion failed (step[dims-1] == (size_t)CV_ELEM_SIZE(flags)) in create, file /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/core/src/matrix.cpp, line 236
Traceback (most recent call last):
  File "process.py", line 55, in <module>
preprocessing(im2)
  File "process.py", line 44, in preprocessing
contours, hierarchy =  cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.error: /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/core/src/matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function create

有人可以帮忙吗?非常感谢

最佳答案

代码运行良好。原因是图片文件丢失。

关于python - opencv+python : Assertion failure when findcontours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084893/

相关文章:

Python:无法使用请求模块进行身份验证

Python。对象集中的标识。和散列

php - 运行PHP代码而无需等待Python返回值

openCV RunningAvg 实现

opencv - "detecting object"opencv 的 HOG

python - 使用 csv.reader 和 csv.writer 写入同一文件而不是不同的文件?

Python - 如何通知一个线程已经完成另一个类?

python - 我正在尝试使用 cv2.solvePnP() 但出现错误

OpenCV 填充缺失像素

algorithm - tv logo自动查找/定位/检测的建议或方法