前提・実現したいこと
こちらのリンク先で使用しているプログラムを使用し、
葉領域のみの画像(葉領域以外を黒塗りしたRGB画像)を作りたいです。
しかし、エラーが出てうまくいきません。
どうすればうまくいくでしょうか。
発生している問題・エラーメッセージ
error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\core\src\arithm.cpp:225: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function 'cv::binary_op'
該当のソースコード
参考にしたサイトでは、マスク画像作成時に、葉領域が黒色だったのですが、逆にするため、そこだけ変更しています。
dst = cv2.bitwise_and(img, img_greenmask)
この行がうまくいっていないみたいです。
python
import cv2 import numpy as np img_in = 'C:\Users\potapotapotato\Desktop\leaf.png' # load image and change the color spaces img = cv2.imread(img_in) # change BGR to HSV # H: 0-179, S: 0-255, V: 0-255 img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # split photo into 3 channels (H, S, V) img_H, img_S, img_V = cv2.split(img_HSV) # binary transformation (only use H channnel) th, img_H1 = cv2.threshold(img_H, 50 / 360 * 179, 255, cv2.THRESH_BINARY_INV) th, img_H2 = cv2.threshold(img_H, 150 / 360 * 179, 255, cv2.THRESH_BINARY_INV) img_greenmask = img_H2 - img_H1 #マスク箇所だけ色付け dst = cv2.bitwise_and(img, img_greenmask) # save #cv2.imwrite('C:\Users\potapotapotato\Desktop\output.png', img_greenmask) cv2.imwrite('C:\Users\potapotapotato\Desktop\output2.png', dst)
試したこと
dst = cv2.bitwise_and(img, img_greenmask)
この行がうまくいっていないみたいでしたので、
img.shapeで形状を調べてみたところ、
imgが(1000, 750, 3)
img_greenmaskが(1000, 750)
だったので、imgをGrayScaleに変換したところ、グレースケールでの合成はできました。
逆にgreenmaskを3バンドにすればいいんだ!とおもい、
img_greenmask = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
にしたところ、それはおかしいやろがい!
とエラー表記を返されました
error: OpenCV(3.4.2) c:\miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'
まだ回答がついていません
会員登録して回答してみよう