[英]Python OpenCV - Template Matching using the live camera feed frame as input
前段時間在 Android 中嘗試過 OpenCV 后,我又開始使用它了。 現在,我正在嘗試使用 Python 2 的 OpenCV 2。到目前為止,我已經能夠使用它來獲取實時相機饋送,並且在一個單獨的項目中,我已經能夠實現模板匹配,我將提供一個父圖像和一個存在於父圖像中的小圖像,並匹配父圖像中的子圖像,然后輸出另一個在圖像匹配的圖像上繪制紅色矩形的圖像。
這是模板匹配的代碼。 沒什么特別的,它與 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。
我已經有同樣的問題,這個問題是可變的資源,當你開始你的腳本第一次水庫是空的,所以在np.where功能比較空的變量將不會工作,那么你應該把一個:
我現在沒有我的 Pi,所以這是筆記本電腦攝像頭和 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()
我實際上設法解決了它。 我忘了我在這里發布了一個問題。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.