我在不久前在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)
然后至于我的Live Camera Feed代码,我有这个:
# 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
到目前为止,这两个代码都很好地工作,彼此独立。我试过的是,在Camera Stream Code显示任何内容之前,我试图在部件中插入模板匹配代码。
以下是我的想法:
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 :(得分:0)
我已经遇到了同样的问题,问题是变量 res 当您第一次启动脚本 res 为空时,比较 np.where 功能中的空变量将无法正常工作 所以你应该把:
我现在没有我的Pi,所以这是与笔记本电脑相机和opencv相同的例子:
@Override
public int getItemCount() {
return (cursor == null) ? 0 : cursor.getCount();
}
答案 1 :(得分: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