任意の画像を二値化して四角で囲みたいのですが以下のようなエラーが出てしまい困っています.
https://ymgsapo.com/2018/12/12/find-contours/
これを参考にしました.
python
1import cv2 2import matplotlib.pyplot as plt 3 4#画像を読み込みリサイズ 5#img_path='C:/Users/mkou0/Desktop/interest' 6img = cv2.imread("./spidy01.jpg") 7#img = cv2.resize(img,(300,300)) 8 9#グレースケールにする 10gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 11 12#ブラーと二値化 13gray = cv2.GaussianBlur(gray,(7,7),0) 14im2 = cv2.threshold(gray,140,240,cv2.THRESH_BINARY_INV)[1] 15 16#二値化した画像を左にプロット 17plt.subplot(1,2,1) 18plt.imshow(img,cmap='gray') 19 20#輪郭を検出 21cnts = cv2.findContours(im2,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1] 22 23for pt in cnts: 24 x,y,w,h = cv2.boundingRect(pt) 25 #幅が40以下のものをスキップ 26 if w%40 <= 40: 27 continue 28 print(x,y,w,h) 29 #四角形を赤色で描く 30 cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2) 31#輪郭抽出の結果を右にプロット 32plt.subplot(1,2,2) 33plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) 34plt.show 35
error
1cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:743: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::pointSetBoundingRect'
回答1件
あなたの回答
tips
プレビュー