2017-03-02 20 views
1

良い日、私はいままで数日前に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を使用しています。

答えて

0

は、私はすでに同じ問題を抱えていた、問題は変数解像度 あなたがスクリプトを初めて起動解像度はそうnp.where関数内の空の変数だから を動作しません比較は空ですあなたが置く必要があります。

  • 条件(もし解像度:)
  • または例外(試してみてください... :)

除いて、私は今、私のパイを持っていないので、これはノートパソコンのカメラとOpenCVのと同じ例です。私は実際にそれを解決するために管理

import cv2 
import numpy as np 

name = 'find.png' 
template = cv2.imread(name,0) 
face_w, face_h = template.shape[::-1] 

cv2.namedWindow('image') 

cap = cv2.VideoCapture(0) 

threshold = 1 
ret = True 

while ret : 
    ret, img = cap.read() 

    #flip the image ! optional 
    img = cv2.flip(img,1) 

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) 

    if len(res): 
     location = np.where(res >= threshold) 
     for i in zip(*location[::-1]): 
      #puting rectangle on recognized erea 
      cv2.rectangle(img, pt, (pt[0] + face_w, pt[1] + face_h), (0,0,255), 2) 

    cv2.imshow('image',img) 
    k = cv2.waitKey(5) & 0xFF 
    if k == 27: 
     break 
cv2.destroyAllWindows() 
0

。私はここに質問を投稿したことを忘れていました。

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 = image 

    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(image, (pt[1]. pt[0]), (pt[1] + w, pt[0] + h), 
         (0,0,255), 2) 

    # show the frame 
    cv2.imshow("Frame", img_rbg) 
    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 
関連する問題