python - OpenCV & Python - 实时图像(帧)处理

标签 python opencv raspberry-pi3

我们在学校做一个项目,需要进行基本的图像处理。我们的目标是为 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/

相关文章:

python - 从图像中提取数字和字母特征

windows - Windows 8 可以构建 Windows 10 IoT Core 应用程序吗?

python - AttributeError : 'str' object has no attribute 'view' in Seaborn , 散点图

python - 函数名称在 python 类中未定义

python - 如何在 App Engine Python 上创建谷歌云存储签名 URL

python - 将共享层的输出传递给一个层

opencv - 用opencv突出地板上白色胶带的图像处理技巧

c++ - OpenCV - 加速 3x3 补丁的 SSD 计算

linux-kernel - 覆盆子 3 : booting a Kernel by using U-Boot

audio - 将多个蓝牙扬声器与 Raspberry Pi 连接