質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

5641閲覧

Pythonでクロマキーがしたい

rararand

総合スコア4

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/07/11 13:41

編集2022/01/12 10:55

前提・実現したいこと

pythonでクロマキーがしたいです。
サンプルコードそのままのはずなのですがエラーが出てしまい、調べてもよくわかりません。

macでnumpyやopencvはちゃんと入っています。

発生している問題・エラーメッセージ

OpenCV(3.4.1) Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/arithm.cpp, line 659
Traceback (most recent call last):
File "runrun.py", line 51, in <module>
main()
File "runrun.py", line 44, in main
keyed = cv2.add(bg_key, fg_key)
cv2.error: OpenCV(3.4.1) /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/arithm.cpp:659: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op

エラーメッセージ

該当のソースコード

使っているコードは

import numpy as np
import cv2

def main():
"""
aaaa
"""
# default cam matrix rows and columns
cap = cv2.VideoCapture(0) # use external cam

# get the background and resize it. img_back = cv2.imread('/Users/reraito/desktop/background.jpg') background_color = np.uint8([[[0, 255, 0]]]) hls_background_color = cv2.cvtColor(background_color, cv2.COLOR_BGR2HLS) hls_background_color = hls_background_color[0][0] while True: _, frame = cap.read() cols, rows = frame.shape[:2] background = img_back[0:cols, 0:rows] frame = cv2.flip(frame, 1, frame) hls_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS) hue = hls_image[:, :, 0] binary_hue = cv2.inRange(hue, 100, 120) mask = np.zeros(hls_image.shape, dtype=np.uint8) mask[:, :, 0] = binary_hue mask[:, :, 1] = binary_hue mask[:, :, 2] = binary_hue blured = cv2.GaussianBlur(mask, (11, 11), 0) blured_inverted = cv2.bitwise_not(blured) bg_key = cv2.bitwise_and(background,background, blured) fg_key = cv2.bitwise_and(frame,frame, blured_inverted) cv2.imwrite('bg.jpg', bg_key) cv2.imwrite('fg.jpg', fg_key) keyed = cv2.add(bg_key, fg_key) cv2.imshow('frame', keyed) k = cv2.waitKey(33) if k == 27: # ESC break

if name == "main":
main()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op

ですので、

cv2.imwrite('bg.jpg', bg_key)
cv2.imwrite('fg.jpg', fg_key)

が正しく実行できたのか確認しましたか?

投稿2018/07/11 13:53

mkgrei

総合スコア8560

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rararand

2018/07/11 14:34 編集

すみません。初心者なのもあって正しく実行できたかを確認できるようにするにはどうしたらいいのでしょうか 一応ウェブカメラが一瞬だけ光ります。関係あるかはわかりませんが、、 追記失礼します。fg.jpgが保存され自分の画像になっていたので、実行は出来ています。
mkgrei

2018/07/11 14:33

cv2.imwriteで書き出ししているので、bg.jpgとfg.jpgが正しく書き出されているのかを確認してください。
rararand

2018/07/11 14:55

真っ黒な画像(fg.jpg)とそのままの画像(bg.jpg)で書き出されています。
rararand

2018/07/11 15:36

追記です。何度もすみません! 少し状況が改善されました。 bg_key = cv2.bitwise_and(blured,blured) fg_key = cv2.bitwise_and(blured_inverted,blured_inverted) cv2.imwrite('bg.jpg', bg_key) cv2.imwrite('fg.jpg', fg_key) にしたところ、白黒の画像が2枚書き出されるようになり、またframeも表示されるようになりました。(しかし依然映像はつかず白い画面の状態)
mkgrei

2018/07/11 16:04

cv2.imwrite('fg.jpg', fg_key) の直後に print(fg_key.shape) print(bg_key.shape) すると何が出力されますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問