我们在学校做一个项目,需要进行基本的图像处理。我们的目标是为 Raspberry Pi 使用每个视频帧并进行实时图像处理。
我们已经尝试将 raspistill 包含在我们的 python 程序中,但到目前为止没有任何效果。我们项目的目标是设计一辆在图像处理的帮助下沿着蓝色/红色/任何颜色的线行驶的遥控车。
我们认为制作一个执行所有必要图像处理的 python 程序是个好主意,但我们目前正在为将记录的图像带入 python 程序的想法而苦苦挣扎。有没有办法用 picamera 做到这一点,还是我们应该尝试不同的方法?
对于任何好奇的人,这是我们程序目前的样子
while True:
#camera = picamera.PiCamera()
#camera.capture('image1.jpg')
img = cv2.imread('image1.jpg')
width = img.shape[1]
height = img.shape[0]
height=height-1
for x in range (0,width):
if x>=0 and x<(width//2):
blue = img.item(height,x,0)
green = img.item(height,x,1)
red = img.item(height,x,2)
if red>green and red>blue:
最佳答案
OpenCV 已经包含处理实时相机数据的函数。
This OpenCV documentation提供一个简单的例子:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
当然,您不想显示图像,但所有处理都可以在那里完成。
记得休眠几百毫秒,这样 pi 才不会过热。
编辑:
“不过,我到底该怎么做呢。我一直使用“img = cv2.imread('image1.jpg')”。我需要使用什么来代替此处获取“img”变量?我用什么?ret 是干什么用的?:)”
ret
表示读取是否成功。如果没有则退出程序。
读取的 frame
就是您的 img = cv2.imread('image1.jpg')
,因此您的检测代码应该完全相同。
唯一的区别是您的图像不需要保存和重新打开。同样出于调试目的,您可以保存录制的图像,例如:
import cv2, time
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret:
cv2.imwrite(time.strftime("%Y%m%d-%H%M%S"), frame)
cap.release()
关于python - OpenCV & Python - 实时图像(帧)处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37774354/