前提・実現したいこと
プログラミング超初心者です、、、。cv2.findContours()で"flower.jpg"の輪郭描出をしようとしたのですが、以下のようなエラーメッセージが出てしまいます。30分色々調べてみたのですが、よく分からず初めて質問させていただきました。レベルの低い質問をしてしまい申し訳ございませんが、ご教授お願いいたします、、、。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- error Traceback (most recent call last) <ipython-input-15-7a588cd4f199> in <module>() 18 19 for pt in cnts: ---> 20 x,y,w,h=cv2.boundingRect(pt) 21 if w<30 or w>200: continue 22 print(x,y,w,h) 'error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:743: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect''
該当のソースコード
Python
1import cv2 2import matplotlib.pyplot as plt 3 4img=cv2.imread("flower.jpg") 5img=cv2.resize(img, (300,169)) 6 7gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 8gray=cv2.GaussianBlur(gray,(7,7),0) 9im2=cv2.threshold(gray,140,240,cv2.THRESH_BINARY_INV)[1] 10 11 12plt.subplot(1,2,1) 13plt.imshow(im2,cmap="gray") 14 15cnts=cv2.findContours(im2, 16 cv2.RETR_LIST, 17 cv2.CHAIN_APPROX_SIMPLE)[1] 18 19for pt in cnts: 20 x,y,w,h=cv2.boundingRect(pt) 21 if w<30 or w>200: continue 22 print(x,y,w,h) 23 cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0),2) 24 25plt.subplot(1,2,2) 26plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2rGB)) 27plt.savefig("find_contours.png",dpi=200) 28plt.show()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/02 08:13