回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>前一段时间我在Android上尝试过OpenCV之后,我又开始使用它了。现在,我正在用Python 2尝试opencv2。到目前为止,我已经能够使用它来获取实时的相机提要,在一个单独的项目中,我已经能够实现模板匹配,在模板匹配中,我将给出父图像和父图像中存在的小图像,并匹配父图像中的子图像,然后输出另一个在图像匹配上绘制红色矩形的图像。</p>
<p>这是模板匹配的代码。没什么特别的,这和OpenCV网站上的一样:</p>
<pre><code>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)
</code></pre>
<p>至于我的直播视频源代码,我有:</p>
<pre><code># 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
</code></pre>
<p>到目前为止,这两个代码都工作得很好,彼此独立。我尝试的是在相机流代码显示任何内容之前,在部件中插入模板匹配代码。</p>
<p>我想到的是:</p>
<pre><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
</code></pre>
<p>我要做的是,我尝试使用相机输入的图像,而不是<code>cv2.imread(sample.png)</code>,并在我之前使用的模板匹配算法中使用它。</p>
<p>但发生的情况是,相机打开一秒钟(由灯光指示),然后关闭,程序停止。</p>
<p>我真的不知道发生了什么。有没有人知道如何在模板匹配中使用直播相机源作为输入?</p>
<p>我用的是覆盆子皮2和v1.3相机。</p>