[英]Raspberry Pi - Open CV & C++
We're doing a project in school where we need to do basic image processing. 我们正在学校里做一个需要进行基本图像处理的项目。 What we want to do in our project is take a picture every 50ms (or faster) with our Raspberry Pi and do real time image processing.
我们在项目中要做的是使用Raspberry Pi每50毫秒(或更短的时间)拍摄一张图片,并进行实时图像处理。
We've tried to include raspistill in our python-program but so far nothing has worked. 我们试图将raspistill包含在我们的python程序中,但到目前为止没有任何效果。 The goal of our project is to design a RC-car which follows a blue/red/whatever coloured line with help from image processing.
我们项目的目标是在图像处理的帮助下设计一辆遵循蓝色/红色/任何彩色线条的RC汽车。
We thought it would be a good idea to make a python-program which does all image processing necessary, but we currently struggle with the idea of bringing recorded images into the python program. 我们认为制作一个进行所有图像处理必要的python程序是一个好主意,但是我们目前正在努力将记录的图像引入python程序中。 Is there a way to do this with picamera or should we try a different way?
有没有办法用picamera做到这一点,还是我们应该尝试其他方式?
Thanks in advance, anthrx. 预先感谢炭疽。
I'm just going to gloss over how to do this without writing up a full program. 我只是在不编写完整程序的情况下介绍如何执行此操作。 The short answer is this is VERY possible and relatively simple.
简短的答案是,这是非常可能的,并且相对简单。
Step 1 : Get an image from the camera 步骤1 :从相机获取图像
For this I would recommend just using VideoCapture
and processing each image that comes in. This can be done like so: 为此,我建议您仅使用
VideoCapture
并处理每个VideoCapture
图像。可以这样进行:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Read the current frame
Step 2 : Detecting the color (I'll give an example of red) 第2步 :检测颜色(我将举例说明红色)
For this you will need to define some boundaries in the RGB colorspace. 为此,您将需要在RGB颜色空间中定义一些边界。 For this you really just need to determine some lower and upper boundaries
为此,您实际上只需要确定一些上下边界
boundaries = [
([17, 15, 100], [50, 56, 200]),
([25, 146, 190], [62, 174, 250])
]
This is the lower and upper bounds of two boundaries. 这是两个边界的上下边界。 For instance
([17, 15, 100], [50, 56, 200])
is saying we are looking for R>=17
, B>=15
, G>=100
and R<=50
, B<=56
, G<=200
. 例如
([17, 15, 100], [50, 56, 200])
表示我们正在寻找R>=17
, B>=15
, G>=100
和R<=50
, B<=56
, G<=200
。 And yes, the format is RBG for this because of the default scheme in OpenCV 是的,由于OpenCV中的默认方案,因此格式为RBG
Step 3 : Put it together 步骤3 :放在一起
while True:
ret, frame = cap.read() # Read the current frame
for (lo, up) in boundaries:
lo = np.array(lo, dtype='uint8')
up = np.array(up, dtype='uint8')
# Find the colors within those boundaries in the image
mask = cv2.inRange(frame, lo, up)
out = cv2.bitwise_and(frame, frame, mask=mask)
out
now contains the current frame but only the red
that is in the image within those boundaries. 现在,
out
包含当前帧,但仅包含那些边界内图像中的red
。 You can then determine if there is enough red in the image to follow, or turn or what. 然后,您可以确定图像中是否有足够的红色可跟随,或者转动或转动什么。 If you want to actually determine if it's a line of some sort, you can also use
findContours
to determine the shape of the object that is in red, and then figure out if it's a line that way. 如果要实际确定它是否是某种类型的线,则还可以使用
findContours
来确定红色对象的形状,然后找出它是否是这种线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.