[英]opencv+python: Assertion failure when findcontours
我正在做一個圖像處理項目。
我正在使用 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
任何人都可以幫忙嗎? 非常感謝
該代碼工作正常。 原因是圖像文件丟失。
解決方案
從 brew 取消鏈接 opencv。
下載 Anaconda 包: http : //continuum.io/downloads#all並安裝。
安裝 openCV:“conda install opencv”,版本 2.4.8。
如果anaconda和bottleneck有沖突,把引用去掉,用conda install命令重新安裝。
我仍然不知道為什么opencv 2.4.12會拋出這樣的異常,我猜是因為numpy與opencv有沖突。
您需要將圖像轉換為灰度,然后嘗試輪廓。它對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.