良い日、私はいままで数日前にAndroidで試してみました。今、私はPython 2でOpenCV 2を試しています。これまではライブカメラのフィードを取得するためにこれを使用できました。別のプロジェクトでは、テンプレートマッチングを実装することができました前記親画像内の前記サブ画像とマッチングさせ、前記画像マッチングに赤色の矩形を描画する他の画像を出力することを特徴とする請求項1に記載の画像処理装置。Python OpenCV - 入力としてライブカメラのフィードフレームを使用したテンプレートマッチング
テンプレートマッチングのコードは次のとおりです。それはOpenCVのサイトから同じものですが、特別何もない:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
そしてこれまでのところ、これら両方のコードの作業:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv2.imread('mario.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('mario_coin.png',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.imwrite('res.png',img_rgb)
が続いてライブカメラフィードのための私のコードのように、私はこれを持っていますうまく、互いに独立しています。私が試みたのは、カメラストリームコードが何かを表示する前に、パートにテンプレートマッチングコードを挿入しようとしたことでした。ここで
は私が思い付いた何だった:私は何をしようとしている
from picamera.array import PiRGBArray
from picamera import PiCamera
from matplotlib import pyplot as plt
import time
import cv2
import numpy as np
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
template = cv2.imread('mario_coin.png', 0)
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr",
use_video_port=True):
# grab the raw NumPy array representing the image,
# then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# we do something here
# we get the image or something then run some matching
# if we get a match, we draw a square on it or something
## img_rbg = cv2.imread('mario.jpg')
img_rbg = image
## img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
## cv2.rectangle(img_rbg, pt, (pt[0] + w, pt[1] + h),
## (0,0,255), 2)
cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h),
(0,0,255), 2)
## image = img_rgb
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
代わりcv2.imread(sample.png)
の、私はカメラからの映像入力を使用してのそれを使用しようとしているということです私が以前持っていたテンプレートマッチングのアルゴ。
しかし、何が起こるかは、カメラが1秒間(光で示される)開いた後にシャットダウンし、プログラムが停止することです。
私は本当に何が起こっているのか分かりません。私はこれが私に疲れていると思う。なぜなら、私は、むしろ単純なものを見過ごしているという、こだわりの気持ちを持っているからです。
ライブカメラのフィードをテンプレートマッチングの入力として使用する方法について誰かが鉛筆を持っていたら、感謝しています。
私が役立つ場合は、v1.3カメラでRaspberry Pi 2を使用しています。