2017-03-02 64 views
1

美好的一天,我开始在OpenCV之后再次尝试它在Android的前一段时间。现在,我正在使用Python 2试用OpenCV 2.到目前为止,我已经能够使用它来获取实时相机提要,并且在一个单独的项目中,我已经能够实现模板匹配,我将给它父图像和存在于父图像中的小图像,并匹配父图像中的子图像,然后输出在图像匹配上绘制红色矩形的另一图像。Python OpenCV - 模板匹配使用实时相机的进纸框作为输入

这里是模板匹配的代码。这没什么特别的,它是来自OpenCV的网站的同一个:

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) 

然后为我的直播相机饲料的代码,我有这样的:

# 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 

到目前为止,这两个代码工作很好,彼此独立。我试过的是我试图在模型匹配代码插入到相机流代码显示任何东西之前的部分。

这里就是我想出了:

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),我试图从相机使用的图像输入和使用,在我之前有过的模板匹配算法。

但是会发生什么事是相机打开一秒钟(由光线指示),然后它关闭,程序停止。

我真的不知道发生了什么事。我想这是疲惫不堪,因为我有这种唠叨的感觉,我总是看起来很简单。

如果任何人有任何线索如何使用实时相机饲料作为输入模板匹配,我会很感激。

如果有帮助,我使用的是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