0

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.

We've tried to include raspistill in our python-program but so far nothing has worked. The goal of our project is to design a RC-car which follows a blue/red/whatever coloured line with help from image processing.

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. Is there a way to do this with picamera or should we try a different way?

Thanks in advance, anthrx.

anthrx
  • 161
  • 1
  • 3
  • 12
  • Are you looking for code or just a general structure of how to go about this? – Chrispresso Jun 03 '16 at 13:58
  • This may help... http://stackoverflow.com/a/37441369/2836621 – Mark Setchell Jun 03 '16 at 14:24
  • @ZWiki hey, sorry for not responding faster. I was busy with school and stuff so I didn't really have time to check. I was just generally curious about how we should go about this, not looking for specific code. – anthrx Jun 09 '16 at 14:23
  • @ZWiki What we're doing right now is reading out pixel values of a pre recorded picture, but right now we only read the lowest row of pixel which we need for our algorithm for controlling the servos. What our main goal is right now would be to only take a picture of that bottom line so we optimize out running time, and right now we're only steering in hard code. What we're aiming for is steering and controlling our servos, dependent on our algorithm. If you want, I could hit you up with the code we're currently using, but not today, but tomorrow at this time. – anthrx Jun 09 '16 at 14:39

1 Answers1

0

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
For this I would recommend just using VideoCapture and processing each image that comes in. This can be done like so:

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)
For this you will need to define some boundaries in the RGB colorspace. 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. And yes, the format is RBG for this because of the default scheme in OpenCV

Step 3: Put it together

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. 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.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31
  • what I don't understand here, let's say we just take the frame and use it for our algorithm. How exactly do I implement it? Right now we're doing it with "img = cv2.imread('image1.jpg')" but it seems like we're not really taking with a .jpg on the end. – anthrx Jun 12 '16 at 12:41
  • When you do that you will get a `numpy` array of values between [0,255]. You need to create/find an algorithm that then allows the tracking of that red line based off the range of color pixels (such as the boundaries I listed above). – Chrispresso Jun 13 '16 at 14:07