7

OpenCV 3.0.0 では、templateMatch の実行中にマスクを指定する機能が追加されました。マスクを指定すると、次のエラーが発生します。error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function matchTemplateMask

テンプレート画像 (透明度のある PNG):

ここに画像の説明を入力

ソース画像:

ここに画像の説明を入力

コード

# read the template emoji with the alpha channel
template = cv2.imread(imagePath, cv2.IMREAD_UNCHANGED)
channels = cv2.split(template)
zero_channel = np.zeros_like(channels[0])
mask = np.array(channels[3])

# all elements in alpha_channel that have value 0 are set to 1 in the mask matrix
mask[channels[3] == 0] = 1

# all elements in alpha_channel that have value 100 are set to 0 in the mask matrix
mask[channels[3] == 100] = 0

transparent_mask = cv2.merge([zero_channel, zero_channel, zero_channel, mask])

print image.shape, image.dtype  # (72, 232, 3) uint8
print template.shape, template.dtype  # (40, 40, 4) uint8
print transparent_mask.shape, transparent_mask.dtype    # (40, 40, 4) uint8

# find the matches
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED, mask=transparent_mask)

画像の種類に問題はありませんか?matchTemplate メソッドの新しい mask パラメーターを使用した例 (Python で) を見つけることができません。マスクの作り方わかる人いますか?

4

4 に答える 4