実現したいこと
画像の分割処理を行い、分割したそれぞれの画像に対して、輪郭抽出、外接矩形の描画を行うプログラムを考えていました。
発生しているエラーの意味がよくわからないです、、
どなたか教えていただけますと助かります。
発生している問題・エラーメッセージ
error Traceback (most recent call last) <ipython-input-2-2c255fdea394> in <module>() 28 #cv2.imwrite('C:/Users/ito/Anaconda3/envs/Sample/pic/split/split' + str(i) +'.png', out_img[i]) 29 ---> 30 DrawCont(out_img[i]) <ipython-input-1-23054e91f53b> in DrawCont(img) 11 12 # グレースケールに変換 ---> 13 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 14 15 # 二値化 error: C:\ci\opencv_1512684736357\work\modules\imgproc\src\color.cpp:11048: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
輪郭抽出→外接矩形描画を行うコード↓
python
1import cv2 2import numpy as np 3 4def DrawCont(img): 5 6 # 画像の読み込み 7 img = cv2.imread('test.jpg') 8 9 # リサイズ 10 #img = cv2.resize(img, dsize=None, fx=0.2, fy=0.2) 11 12 # グレースケールに変換 13 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 14 15 # 二値化 16 ret, binary = cv2.threshold(gray, 166, 255, cv2.THRESH_BINARY_INV) 17 18 #輪郭抽出 19 _, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 20 21 # 面積が小さい輪郭削除 22 area = img.shape[0] * img.shape[1] 23 contours = list(filter(lambda cnt: 1000 < cv2.contourArea(cnt), contours)) 24 25 # 外接矩形に変換 26 rects = [cv2.boxPoints(cv2.minAreaRect(cnt)).astype(int) for cnt in contours] 27 28 # 外接矩形を描画 29 cv2.drawContours(img, rects, -1, (0, 255, 0), 2) 30 31 # 画像を保存 32 cv2.imwrite('C:/Users/ito/Anaconda3/envs/pic/split_result/result' + str(i) + '.jpg', img)
分割処理を行うコード↓
python
1#参考URL https://qiita.com/ka10ryu1/items/015c6a6a5fa287a47828 2 3import cv2 4import numpy as np 5 6 7img = cv2.imread('C:/Users/ito/Anaconda3/envs/Sample/pic/testimg/IMG_0070.jpg') 8#img = cv2.resize(img, (1275, 997)) 9 10size = 252 11v_size = img.shape[0] // size * size 12h_size = img.shape[1] // size * size 13img = img[:v_size, :h_size] 14 15v_split = img.shape[0] // size 16h_split = img.shape[1] // size 17out_img = [] 18 19[out_img.extend(np.hsplit(h_img, h_split)) for h_img in np.vsplit(img, v_split)] 20 21out_img = np.array(out_img) 22 23i=0 24for i, lst in enumerate(out_img): 25 #print(type(lst), lst) 26 27 DrawCont(out_img[i]) 28 29
開発環境
windows7 professional
python3.6.5
opencv3.3.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/28 06:46
2018/10/28 06:47
2018/10/28 06:49
2018/10/28 06:50 編集
2018/10/28 06:57 編集
2018/10/28 07:21