Ask Your Question
0

optimization techniques for opencv, C++

asked 2016-01-09 12:55:22 -0600

pradeep_kb gravatar image

I have written a program in which the program has to detect objects from the video. the actual duration of video is 5 hours but it is taking nearly 10 hours on raspberry pi 2. It is taking very long time, doesn't work in real time. Is there any technique to utilize all the 4 cores of raspberry pi to run the program? Are there any optimization techniques available for opencv to make my program run faster?

edit retag flag offensive close merge delete

Comments

1

what kind of objects do you detect.IMHO you don't need to proceess every frame. you can skip frames.

sturkmen gravatar imagesturkmen ( 2016-01-09 13:04:10 -0600 )edit

@sturkmen I am detecting the cars present in the video and counting no. of cars entering in certain region. I am not processing every frame. I am have tested the program with 4, 6 & 8 frame skips. But still it is taking more time and not processing in required time. The video that have is of 5 hours and when I run the program it is taking 6.5 to 7 hours for skip 8 but it is not giving accurate results. The number of cars count is reducing.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:23:44 -0600 )edit

@sturkmen I have even done re-sizing of image to 50% 75% and 25%.. did all possible thinngs to get good result.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:33:34 -0600 )edit

take a look at my other answer and here

sturkmen gravatar imagesturkmen ( 2016-01-10 04:18:06 -0600 )edit
1

Where is your program bottleneck? Did you perform profiling?

Vit gravatar imageVit ( 2016-01-10 18:45:50 -0600 )edit

@Vit, I did not do profiling, I don't know what it is. Could you please help me to know that

pradeep_kb gravatar imagepradeep_kb ( 2016-01-10 19:11:04 -0600 )edit

@sturkmen@Vit Does Background subtraction helps to speedup the program?

pradeep_kb gravatar imagepradeep_kb ( 2016-01-10 19:12:44 -0600 )edit
1
Vit gravatar imageVit ( 2016-01-10 19:18:54 -0600 )edit

@Vit in my previous comment i gave two link based on Background subtraction

sturkmen gravatar imagesturkmen ( 2016-01-10 20:22:52 -0600 )edit

I am sorry @sturkmen but @Vit is right, profiling is the only decent solution here. We need to identify which part of his code takes long on raspberry pi first!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 06:46:27 -0600 )edit

@Vit, this is manual profiling that I have done. All the results are in milliseconds.

Total time: 3.99949e+07ms  fps: 2.5e-05
END OF VIDEO
Times: 
1-Moving difference, grayscale conversion-------------->1.18404e+06
2-Image binarization----------------------------------->102496
3-Dilate and erode (Morphology)------------------------>1.35859e+07
4-Blob process(FilterByArea,UpdateTracks,RenderTracks)->983343
5-Car and person detection----------------------------->99999.6
6-Getting frames from video and frames skipped--------->1.63229e+06
(Adding 1 to 6) Total->1.75881e+07
pradeep_kb gravatar imagepradeep_kb ( 2016-01-11 15:15:28 -0600 )edit

@Vit@StevenPuttemans I have gone through the profiling, I got to know we need a tool to check the which function is taking much time. But there are lot of tools available. Which is the best tool to use for Opencv, Raspberry pi, C++?

pradeep_kb gravatar imagepradeep_kb ( 2016-01-11 15:23:00 -0600 )edit

how about this

pradeep_kb gravatar imagepradeep_kb ( 2016-01-11 15:36:13 -0600 )edit

@pradeep_kb please update you question with profiling information. Based on your manual profiling one can see the most calculation-intensive part is morphology, it takes ~80% of time. The common advise for morphology is to avoid large kernels, emulation for large kernel is simplest resize and small kernel or "small kernel N-times passing".

Vit gravatar imageVit ( 2016-01-11 16:15:26 -0600 )edit

@Vit, From the above result that I have posted I found morphology process is taking more time. I think background subtraction process will take less time then morphology process, What do you say? I am new to Opencv, this program was written by someone else. I have to optimize it now.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-11 16:27:16 -0600 )edit
  1. @pradeep_kb returning to the starting point: are you sure not all of your cores are calculating? Morphology is very parallelable and OpenCV uses parallel_for etc for such kind of calculation by default. 2. It is not question of OpenCV, just try do reduce morphology CPU-time using methods I mentioned above. 3. May be your picture is not very noisy and small morphology kernels is enough, or you can update another part of your code to reduce noise and avoid large kernels. It is hard to say what to do exactly without your video-example, your code and its output results.
Vit gravatar imageVit ( 2016-01-11 16:41:58 -0600 )edit

@Vit@StevenPuttemans@sturkmen, I have done profiling for my program using gprof tool here is the details. I am new to this, not able to analyze properly. Thanks for the help.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-12 10:24:10 -0600 )edit

This profiling i have done in my PC not on raspberry pi. On raspberry pi compiler is not generating gmon.out I am working on that. I have followed theset steps. But not working for raspbian OS.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-12 10:32:37 -0600 )edit

@Vit@StevenPuttemans@sturkmen, I have done profiling for the program on raspberry pi 2 with raspbian OS. here are the details. Need your help to analyse it in depth.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-12 14:42:20 -0600 )edit
1

"You need permission. Ask the owner for access, or switch to an account with permission."

Vit gravatar imageVit ( 2016-01-12 16:03:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-09 14:31:53 -0600

updated 2016-01-09 14:50:16 -0600

in the sample code below i tried to show how we can speed up the face detection on video.

by combination of skipping frames and resizing frame you can speed up the process. you can change skip value or resize ratio according to your expectation.

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    string cascadeName = "lbpcascade_frontalface.xml";
    VideoCapture capture;
    Mat frame;
    CascadeClassifier cascade;

    if( !cascade.load( cascadeName ) )
    {
        printf("ERROR: Could not load classifier cascade\n");
        return -1;
    }

    if(!capture.open( argv[1] ))
    {
        printf("ERROR: Video capturing has not been started\n");
        return -1;
    }

    int frame_number = 0;
    int scale = 2; // scale value could be 4 on HD videos
    for(;;)
    {
        capture >> frame;
        frame_number += 1;

        if( frame.empty() )
            break;

        if( frame_number % 5 == 0 ) // indicates skipped frame count
        {
            Mat gray;
            cvtColor( frame, gray, COLOR_BGR2GRAY );
            resize( gray, gray, Size(), (float)1/scale, (float)1/scale );
            vector<Rect> faces;
            cascade.detectMultiScale( gray, faces, 1.3, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

            for ( size_t i = 0; i < faces.size(); i++ )
            {
                faces[i].x = faces[i].x * scale;
                faces[i].y = faces[i].y * scale;
                faces[i].width = faces[i].width * scale;
                faces[i].height = faces[i].height * scale;
                rectangle( frame, faces[i], Scalar(0,255,0), 2 );
            }
        }

        imshow("Face Detection Demo", frame );
        waitKey(1);
        int c = waitKey(10);
        if( c == 27 || c == 'q' || c == 'Q' )
            break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

I am detecting the cars present in the video and counting no. of cars entering in certain region. I am not processing every frame. I am have tested the program with 4, 6 & 8 frame skips. But still it is taking more time and not processing in required time. The video that have is of 5 hours and when I run the program it is taking 6.5 to 7 hours for skip 8 but it is not giving accurate results. The number of cars count is reducing.

pradeep_kb gravatar imagepradeep_kb ( 2016-01-09 22:24:21 -0600 )edit

You need to focus on profiling your program as @Vit stated. As long as you do not know how long it takes for each processing step, we cannot help you out with decent tips!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 06:45:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-09 12:55:22 -0600

Seen: 1,772 times

Last updated: Jan 09 '16