Detecting presence of an object in an image

Viewed 1898

I'm fairly new to OpenCV and C/C++ (been coding in Java and C# though). I am working on a project(robot rather) which detects traffic lights and signs(stop sign only). Now I found a great tutorial here and used the code provided in the tutorial as it is for red coloured object detection. Since its configurable I can set it to see the traffic light just fine.

Link to the tutorial- http://opencv-srf.blogspot.in/2010/09/object-detection-using-color-seperation.html

Here is the code so far -

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  VideoCapture cap(0); //capture the video from web cam

if ( !cap.isOpened() )  // if not success, exit program
{
     cout << "Cannot open the web cam" << endl;
     return -1;
}

namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

int iLowH = 0;
int iHighH = 179;

int iLowS = 0; 
int iHighS = 255;

int iLowV = 0;
int iHighV = 255;

//Create trackbars in "Control" window
cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
cvCreateTrackbar("HighH", "Control", &iHighH, 179);

cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
cvCreateTrackbar("HighS", "Control", &iHighS, 255);

cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
cvCreateTrackbar("HighV", "Control", &iHighV, 255);

while (1)
{
    Mat imgOriginal;

    bool bSuccess = cap.read(imgOriginal); // read a new frame from video

     if (!bSuccess) //if not success, break loop
    {
         cout << "Cannot read a frame from video stream" << endl;
         break;
    }

Mat imgHSV;

cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

Mat imgThresholded;

inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV),  imgThresholded); //Threshold the image

 //morphological opening (remove small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

 //morphological closing (fill small holes in the foreground)
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );

imshow("Thresholded Image", imgThresholded); //show the thresholded image
imshow("Original", imgOriginal); //show the original image
    if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
   {
        cout << "esc key is pressed by user" << endl;
        break; 
   }
}

return 0;

}

I've spent entire nights looking up the OpenCV documentation and other possible questions on StackOverflow but none that I found address my issue. Trust me, I'm asking this after quite a lot of stumbling here and there in my quest for answers.

So my questions are-

Q1. Since I'm new to C/C++(the tutorial code is written in C++), what possible ways are there to signal when the light is on or of? I atleast need to show on the screen that the light went off and when it came back on. I tried a few ways but none worked.(Doesn't matter if I have to convert the image to greyscale) (Example - Stop msg displayed on screen when light is on and go when off)

Q2. How do I detect the signs and take appropriate action. Here detection is not much of a problem(lot of tutorials available, I can manage that but any tips would be appreciated), taking action upon the detection is.

Q3.This might be off topic but is crucial to my project. Currently I am developing the code on my Windows PC but I intend to use this on a Raspberry Pi 3, which will in turn control a slave Arduino UNO board which controls motors. From all my research until now I've come to know that OpenCV on the RPi is used in python whereas I am coding in C/C++. So can I run my code as it was written on my PC on the RPi or do I have to first rewrite it in python and then put it on the Pi.( And oh, if you can give me further ideas on how to go on with the rest of my project that'll be great!)

I'm obviously not asking you all to teach me C++. But a few keywords or a sample code would be great. And please keep in mind that since the ultimate use of this code would be on a RPi interfacing with an Arduino, if you can keep the code in that context it would be better. Otherwise just please answer with respect to Windows, I'll modify the code one way or the other and make it work on RPi. I'm a completely self taught programmer and I'm only 17 so I'm a bit outsourced too but help from folks like you all is more than enough to keep me running.

Thanks.

2 Answers
Related