前に書いた言葉
私はlabelme ラベリング ソフトウェアを使用して、図のsow のアウトラインにラベルを付けました. 生成されたjson ファイルには、sow のアウトライン ポイントの66 座標が含まれており、これをマスク形式に変換し、 opencv-python ライブラリのcv2で使用する必要があります.fillPoly 関数
オリジナルコード
seg_img = np.zeros_like(image), dtype=np.int8)#黑底
cv2.fillPoly(seg_img, np.array(json_data['shapes'][17]['points'], dtype=np.int32), (255,255,255))#白色mask
実行エラー
cv2.fillPoly(seg_img, np.array(json_data['shapes'][17]['points'], dtype=np.int32), (255,255,255))
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-ms668fyv/opencv/modules/imgproc/src/drawing.cpp:2395: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly'
変更後のコード
注:json_data['shapes'][17]['points']
これは 66 個の輪郭点の xy 座標であり、リストであり、次元は (66,2) です。(1,66,2) をポリゴンに変換するには、次元を追加する必要があります。ここでは、リストを行列に変換します。
seg_img = np.zeros_like(image), dtype=np.int8)#黑底
cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.int32), (255,255,255))
正常に実行
単一チャネルの実行結果
seg_img = np.zeros((image.shape[0], image.shape[1]), dtype=np.int8)
cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.int32), [255])
補充する
dtype=np.uint32
次のミスになると
cv2.fillPoly(seg_img, np.array([json_data['shapes'][17]['points']], dtype=np.uint32), (255,255,255))
エラーを報告する
TypeError: Expected Ptr<cv::UMat> for argument 'pts'