Search code examples
javaimage-processingopencvjavacv

JavaCV Warning sign detection?


I've looked at JavaCV wrapper for OpenCV library and I saw that it is possible to use that library in Java for face detection on an image, but I was wondering is it possible to use that library for detecting traffic warning signs on an image and how?

I have pictures taken from the road that look like this: http://www.4shared.com/photo/5QxoVDwd/041.html and the result of detection should look sometning like this or similar: http://www.4shared.com/photo/z_pL0lSK/overlay-0.html

EDIT: After I detect red color I get this image:

enter image description here

And I have a problem detecting just the warning sign triangle shape and ignore all other shapes. I tried changing the cvApproxPoly parameters but with no result. This is my code:

public void myFindContour(IplImage image)
{
    IplImage grayImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
    cvCvtColor(image, grayImage, CV_BGR2GRAY);

    CvMemStorage mem;
    CvSeq contours = new CvSeq();
    CvSeq ptr = new CvSeq();
    cvThreshold(grayImage, grayImage, 150, 255, CV_THRESH_BINARY);
    mem = cvCreateMemStorage(0);

    cvFindContours(grayImage, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
    Random rand = new Random();

    while (contours != null && !contours.isNull()) {
        if (contours.elem_size() > 0) {
            CvSeq points = cvApproxPoly(contours, Loader.sizeof(CvContour.class),
                    mem, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
            Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
            CvScalar color = CV_RGB( randomColor.getRed(), randomColor.getGreen(), randomColor.getBlue());
            cvDrawContours(image, points, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
        }
        contours = contours.h_next();
    }

    cvSaveImage("myfindcontour.png", image);

}

This is the output that i get (I used different colors for every shape, but in the final output i will use only white for detected warning sign and everything other left black):

enter image description here


Solution

  • You have to do the following:

    1. Detect red color on image - you will get 1bit image where: 0=non-red, 1=red.
    2. Detect triangles on created in previous step image. You can do that using approxPoly function.