对象跟踪延迟颜色跟踪OpenCV

时间:2015-09-16 20:02:15

标签: c++ opencv image-processing

我试图检测彩色球像ps3移动控制器球从2米距离。我有10个相机在同一个房间悬挂在天花板上。房间很暗,球已经进入里面。我有4-5个球。(红色,蓝色,绿色,黄色,粉红色)。我想用opencv跟踪他们的位置。在opencv中这样做的权利是什么?你可以给出链接,这个例子吗?

我使用此代码,但我有延迟问题。当我评论//我的trackFilteredObject行没有滞后。但是当使用此代码时我有很多延迟。我无法理解为什么发生因为我的正常CPU使用〜%15 ram使用6.3GB / 15GB(%40)运行此代码cpu使用率~20-23 ram使用率6.4GB。我认为它不是关于cpu-ram性能。我做错了什么?

视频:https://www.youtube.com/watch?v=_BKtJpPrkO4(您可以在前10秒看到延迟。在10 sen之后我会评论跟踪代码。)

注意:Kamerasayisi意味着cameracount我的追踪功能:

void trackFilteredObject(Object theObject,Mat threshold,Mat HSV, Mat &cameraFeed){
    //max number of objects to be detected in frame
    const int FRAME_WIDTH = 5120;
const int FRAME_HEIGHT = 480;

    const int MAX_NUM_OBJECTS=50;
//minimum and maximum object area
const int MIN_OBJECT_AREA = 10*10;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
    vector <Object> objects;

    Mat temp;
    threshold.copyTo(temp);
    //these two vectors needed for output of findContours
    vector< vector<Point> > contours;
    vector<Vec4i> hierarchy;
    //find contours of filtered image using openCV findContours function
    findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
    //use moments method to find our filtered object
    double refArea = 0;
    bool objectFound = false;
    if (hierarchy.size() > 0) {
        int numObjects = hierarchy.size();
        //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
        if(numObjects<MAX_NUM_OBJECTS){
            for (int index = 0; index >= 0; index = hierarchy[index][0]) {

                Moments moment = moments((cv::Mat)contours[index]);
                double area = moment.m00;

        //if the area is less than 20 px by 20px then it is probably just noise
        //if the area is the same as the 3/2 of the image size, probably just a bad filter
        //we only want the object with the largest area so we safe a reference area each
                //iteration and compare it to the area in the next iteration.
                if(area>MIN_OBJECT_AREA){

                    Object object;

                    object.setXPos(moment.m10/area);
                    object.setYPos(moment.m01/area);
                    object.setType(theObject.getType());
                    object.setColor(theObject.getColor());

                    objects.push_back(object);

                    objectFound = true;

                }else objectFound = false;
            }
            //let user know you found an object
            if(objectFound ==true){
                //draw object location on screen
                drawObject(objects,cameraFeed,temp,contours,hierarchy);}

        }else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
    }       
}
};

主要代码:

       void Run()
    {


        int w, h;

        _fps = 30;
        IplImage *pCapImage[kameraSayisi];
        IplImage *pDisplayImage;
        PBYTE pCapBuffer = NULL;
        // Create camera instance
        for(int i = 0; i < kameraSayisi; i++)
        {
            _cam[i] = CLEyeCreateCamera(_cameraGUID[i], _mode, _resolution, _fps);
            if(_cam[i] == NULL) return;
            // Get camera frame dimensions
            CLEyeCameraGetFrameDimensions(_cam[i], w, h);
            // Create the OpenCV images
            pCapImage[i] = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);

            // Set some camera parameters
            CLEyeSetCameraParameter(_cam[i], CLEYE_GAIN, 0);
            CLEyeSetCameraParameter(_cam[i], CLEYE_EXPOSURE, 511);

            // Start capturing
            CLEyeCameraStart(_cam[i]);


        }
        pDisplayImage = cvCreateImage(cvSize(w*kameraSayisi / 2, h * kameraSayisi/4 ), IPL_DEPTH_8U  ,1);

        if(_cam == NULL)        return;

  int iLastX = -1; 
 int iLastY = -1;



  //Capture a temporary image from the camera
    //program
    bool trackObjects = true;
    bool useMorphOps = true;

        Mat HSV;
  //Create a black image with the size as the camera output
 Mat imgLines;
  // imgLines = Mat::zeros( cvarrToMat(image).size(), CV_8UC3 );;
 Mat threshold;
    //x and y values for the location of the object
    int x=0, y=0;
    bool calibrationMode = false;
    if(calibrationMode){
        //create slider bars for HSV filtering
        createTrackbars();
    }

        // image capturing loop
        while(_running)
        {

            PBYTE pCapBuffer;
            // Capture camera images
            for(int i = 0; i < kameraSayisi; i++)
            {
                cvGetImageRawData(pCapImage[i], &pCapBuffer);
                CLEyeCameraGetFrame(_cam[i], pCapBuffer, (i==0)?2000:0);

            }

            // Display stereo image
                for(int i = 0; i < kameraSayisi; i++)
                {
                    cvSetImageROI(pDisplayImage, cvRect(w * (i%4) ,i/4 * h, w, h));
                    cvCopy(pCapImage[i], pDisplayImage);
                }
                cvResetImageROI(pDisplayImage);

            Mat imgOriginal;
            Mat imgConverted = cvarrToMat(pDisplayImage);
            if(calibrationMode==true)
            {

            //need to find the appropriate color range values
            // calibrationMode must be false

            //if in calibration mode, we track objects based on the HSV slider values.
                //cvtColor(imgOriginal,imgOriginal,CV_BayerRG2RGB);
                cvtColor(imgConverted,imgOriginal,CV_BayerGB2BGR);
                cvtColor(imgOriginal,HSV,CV_BGR2HSV);
                inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold);
                morphOps(threshold);
                imshow(_windowName + 'T',threshold);

            //the folowing for canny edge detec         
                /// Create a matrix of the same type and size as src (for dst)
                dst.create( imgOriginal.size(), src.type() );
                /// Convert the image to grayscale
                cvtColor( imgOriginal, src_gray, CV_BGR2GRAY );
                /// Create a window
                namedWindow( window_name, CV_WINDOW_AUTOSIZE );
                /// Create a Trackbar for user to enter threshold
            //  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
                /// Show the image
                Object a = Object(H_MIN,S_MIN,V_MIN,H_MAX,S_MAX,V_MAX);
                trackFilteredObject(a,threshold,HSV,imgOriginal);
        }

            else{

            //we can use their member functions/information
            Object blue("blue"), yellow("yellow"), red("red"), orange("orange"),white("white");
                cvtColor(imgConverted,imgOriginal,CV_BayerGB2BGR);
            //first find blue objects
            cvtColor(imgOriginal,HSV,CV_RGB2HSV);
            inRange(HSV,blue.getHSVmin(),blue.getHSVmax(),threshold);
            morphOps(threshold);

            //then yellows
            inRange(HSV,yellow.getHSVmin(),yellow.getHSVmax(),threshold);

            //then reds

            inRange(HSV,red.getHSVmin(),red.getHSVmax(),threshold);

            //then white

            inRange(HSV,white.getHSVmin(),white.getHSVmax(),threshold);

                //then orange

            inRange(HSV,orange.getHSVmin(),orange.getHSVmax(),threshold);
        trackFilteredObject(yellow,threshold,HSV,imgOriginal);
                trackFilteredObject(white,threshold,HSV,imgOriginal);
            trackFilteredObject(red,threshold,HSV,imgOriginal);
            trackFilteredObject(blue,threshold,HSV,imgOriginal);
            trackFilteredObject(orange,threshold,HSV,imgOriginal);

            }
        //delay 10ms so that screen can refresh.
        //image will not appear without this waitKey() command
                if (cvWaitKey(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; 
             }

        //  cvShowImage(_windowName, image);
            imshow(_windowName,imgOriginal);
        }
            for(int i = 0; i < kameraSayisi; i++)
        {
            // Stop camera capture
            CLEyeCameraStop(_cam[i]);
            // Destroy camera object
            CLEyeDestroyCamera(_cam[i]);
            // Destroy the allocated OpenCV image
            cvReleaseImage(&pCapImage[i]);
            _cam[i] = NULL;
        }

    }

0 个答案:

没有答案