Ask Your Question

Revision history [back]

Here is my code:

include <iostream>

include <time.h>

include <stdio.h>

include <raspicam raspicam_cv.h="">

include <raspicam raspicam.h="">

include <opencv2 opencv.hpp="">

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include "opencv2/features2d/features2d.hpp"

include "opencv2/nonfree/features2d.hpp"

include <cstdlib>

include <fstream>

include <string>

include <sstream>

include <opencv2 stitching="" stitcher.hpp="">

include "camera.h"

include <pthread.h>

using namespace std; using namespace cv;

Mat dst;

int main(int argc,char **argv) { time_t start,end; Mat template1 = imread( "100_1.jpg");//, CV_LOAD_IMAGE_GRAYSCALE ); //loading object to be detectedimg_object Mat template2 = imread( "NoEntry.jpg"); Mat template3 = imread( "Speed80.png");

raspicam::RaspiCam_Cv Camera;
OpenPiCamera(Camera);

cv::Mat rawMat; IplImage *frame;

cvNamedWindow("Capture Frame",1);

time(&start);
int counter=0;

while(1)
{
    //if(counter%5==0)
    {

    Camera.grab();
    Camera.retrieve(rawMat);
    Mat img_scene=rawMat.clone();
            cvtColor(rawMat,rawMat,CV_RGB2HSV);
    cv::Scalar   min(220/2, 0, 0);
    cv::Scalar   max(260/2,255, 255);
    inRange(rawMat,min,max,dst);
            vector<cv::Vec3f> circles;
    HoughCircles( blur, circles, CV_HOUGH_GRADIENT,1,dst.rows/4,70, 20, 1, 40);//3, 5,400, 10, 0, 80    1, img_scene.rows/6, 100,70,0, 50    dst.rows/8
    cv::Rect borders(Point(0,0), dst.size());

for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); cv::circle( dst, center, 3, Scalar(255,255,255), -1); cv::circle( dst, center, radius, Scalar(255,255,255), 1 ); imshow("hough",dst);

        int x=cvRound(circles[i][0])-cvRound(circles[i][2]);
        int y=cvRound(circles[i][1])-cvRound(circles[i][2]);
        Rect r(abs(x),abs(y),radius*2,radius*2);
        if(r.area()>100)
        {
            Mat roi( radius*2, radius*2, CV_8UC1);

            roi=img_scene( Rect(abs(x),abs(y),radius*2,radius*2)& borders);//save ROI
            Mat mask( roi.size(),roi.type(),Scalar::all(0));
            circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);//circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);
            Mat roi_cropped=roi & mask;

            int W=0,H=0;
            W=dst.cols;
            H=dst.rows;
            int w=0,h=0;
            w=roi_cropped.cols;
            h=roi_cropped.rows;
            Mat res_32f1(W - w + 1, H - h + 1, CV_32FC3);
            Mat res_32f2(W - w + 1, H - h + 1, CV_32FC3);
            Mat res_32f3(W - w + 1, H - h + 1, CV_32FC3);

            Mat resizedTemplate1,resizedTemplate2,resizedTemplate3;
            //resizedTemplate1=template1.clone();
            resize(template1, resizedTemplate1, roi_cropped.size());//resizing template into ROI
            resize(template2, resizedTemplate2, roi_cropped.size());
            resize(template3, resizedTemplate3, roi_cropped.size());
            matchTemplate(resizedTemplate1, roi_cropped, res_32f1, CV_TM_CCORR_NORMED);
            matchTemplate(resizedTemplate2, roi_cropped, res_32f2, CV_TM_CCORR_NORMED);
            matchTemplate(resizedTemplate3, roi_cropped, res_32f3, CV_TM_CCORR_NORMED);


            double minval1, maxval1, minval2, maxval2,minval3, maxval3, minval4, maxval4,minval5, maxval5,threshold1 = 0.74;//.78
            Point minloc1, maxloc1, minloc2, maxloc2,minloc3, maxloc3,minloc4, maxloc4,minloc5, maxloc5;
            minMaxLoc(res_32f1, &minval1, &maxval1, &minloc1, &maxloc1);
            minMaxLoc(res_32f2, &minval2, &maxval2, &minloc2, &maxloc2);
            minMaxLoc(res_32f3, &minval3, &maxval3, &minloc3, &maxloc3);
            double maxInddex;
            double init[]={maxval1, maxval2,maxval3};//,maxval4,maxval5
            valarray<double> myvalarray (init,3);
            double max=(double)myvalarray.max();

            if (max >= threshold1)
            {

                if(max==maxval1)rectangle(img_scene,    Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(255,255,255), 5, 8, 0);
                if(max==maxval2)rectangle(img_scene, Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(255,0,255), 5, 8, 0);
                if(max==maxval3)rectangle(img_scene, Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(0,255,255), 5, 8, 0);

            }

        }
    }

imshow("Circle",img_scene);

    frame=new IplImage(rawMat);//
    }
    time(&end);
    ++counter;
    double sec=difftime(end,start);
    cout<<"millisec"<<sec/CLOCKS_PER_SEC*1000;
    cout<<"seconds"<<sec;
    double fps=counter/sec;
    printf("FPS: %0.2f\n",fps);

    cvShowImage("Capture Frame",frame);

    if((cvWaitKey(1) & 255) == 27 ) break;  
}

ClosePiCamera(Camera);

}

While running the above code, the result is displayed very slowly. Is there any way I can improve the above code to make it more real time? To improve the performance I tried using pthreads.

 Here is my code:  

include <iostream>

include <time.h>

include <stdio.h>

include <raspicam raspicam_cv.h="">

include <raspicam raspicam.h="">

include <opencv2 opencv.hpp="">

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include "opencv2/features2d/features2d.hpp"

include "opencv2/nonfree/features2d.hpp"

include <cstdlib>

include <fstream>

include <string>

include <sstream>

include <opencv2 stitching="" stitcher.hpp="">

include "camera.h"

include <pthread.h>

#include <iostream> #include <time.h> #include <stdio.h> #include <raspicam/raspicam_cv.h> #include <raspicam/raspicam.h> #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include "opencv2/features2d/features2d.hpp" #include "opencv2/nonfree/features2d.hpp" #include <cstdlib> #include <fstream> #include <string> #include <sstream> #include <opencv2/stitching/stitcher.hpp> #include "camera.h" #include <pthread.h> using namespace std; using namespace cv;

cv; Mat dst;

dst; int main(int argc,char **argv) { time_t start,end; Mat template1 = imread( "100_1.jpg");//, CV_LOAD_IMAGE_GRAYSCALE ); //loading object to be detectedimg_object Mat template2 = imread( "NoEntry.jpg"); Mat template3 = imread( "Speed80.png");

"Speed80.png");

    raspicam::RaspiCam_Cv Camera;
 OpenPiCamera(Camera);

cv::Mat rawMat; IplImage *frame;

*frame;

    cvNamedWindow("Capture Frame",1);

 time(&start);
 int counter=0;

 while(1)
 {
     //if(counter%5==0)
     {

     Camera.grab();
     Camera.retrieve(rawMat);
     Mat img_scene=rawMat.clone();
             cvtColor(rawMat,rawMat,CV_RGB2HSV);
     cv::Scalar   min(220/2, 0, 0);
     cv::Scalar   max(260/2,255, 255);
     inRange(rawMat,min,max,dst);
             vector<cv::Vec3f> circles;
     HoughCircles( blur, circles, CV_HOUGH_GRADIENT,1,dst.rows/4,70, 20, 1, 40);//3, 5,400, 10, 0, 80    1, img_scene.rows/6, 100,70,0, 50    dst.rows/8
     cv::Rect borders(Point(0,0), dst.size());

for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); cv::circle( dst, center, 3, Scalar(255,255,255), -1); cv::circle( dst, center, radius, Scalar(255,255,255), 1 ); imshow("hough",dst);

imshow("hough",dst);

            int x=cvRound(circles[i][0])-cvRound(circles[i][2]);
         int y=cvRound(circles[i][1])-cvRound(circles[i][2]);
         Rect r(abs(x),abs(y),radius*2,radius*2);
         if(r.area()>100)
         {
             Mat roi( radius*2, radius*2, CV_8UC1);

             roi=img_scene( Rect(abs(x),abs(y),radius*2,radius*2)& borders);//save ROI
             Mat mask( roi.size(),roi.type(),Scalar::all(0));
             circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);//circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);
             Mat roi_cropped=roi & mask;

             int W=0,H=0;
             W=dst.cols;
             H=dst.rows;
             int w=0,h=0;
             w=roi_cropped.cols;
             h=roi_cropped.rows;
             Mat res_32f1(W - w + 1, H - h + 1, CV_32FC3);
             Mat res_32f2(W - w + 1, H - h + 1, CV_32FC3);
             Mat res_32f3(W - w + 1, H - h + 1, CV_32FC3);

             Mat resizedTemplate1,resizedTemplate2,resizedTemplate3;
             //resizedTemplate1=template1.clone();
             resize(template1, resizedTemplate1, roi_cropped.size());//resizing template into ROI
             resize(template2, resizedTemplate2, roi_cropped.size());
             resize(template3, resizedTemplate3, roi_cropped.size());
             matchTemplate(resizedTemplate1, roi_cropped, res_32f1, CV_TM_CCORR_NORMED);
             matchTemplate(resizedTemplate2, roi_cropped, res_32f2, CV_TM_CCORR_NORMED);
             matchTemplate(resizedTemplate3, roi_cropped, res_32f3, CV_TM_CCORR_NORMED);


             double minval1, maxval1, minval2, maxval2,minval3, maxval3, minval4, maxval4,minval5, maxval5,threshold1 = 0.74;//.78
             Point minloc1, maxloc1, minloc2, maxloc2,minloc3, maxloc3,minloc4, maxloc4,minloc5, maxloc5;
             minMaxLoc(res_32f1, &minval1, &maxval1, &minloc1, &maxloc1);
             minMaxLoc(res_32f2, &minval2, &maxval2, &minloc2, &maxloc2);
             minMaxLoc(res_32f3, &minval3, &maxval3, &minloc3, &maxloc3);
             double maxInddex;
             double init[]={maxval1, maxval2,maxval3};//,maxval4,maxval5
             valarray<double> myvalarray (init,3);
             double max=(double)myvalarray.max();

             if (max >= threshold1)
             {

                 if(max==maxval1)rectangle(img_scene,    Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(255,255,255), 5, 8, 0);
                 if(max==maxval2)rectangle(img_scene, Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(255,0,255), 5, 8, 0);
                 if(max==maxval3)rectangle(img_scene, Point(abs(x),abs(y)),Point(abs(x)+radius*2,abs(y)+radius*2), Scalar(0,255,255), 5, 8, 0);

             }

         }
     }

imshow("Circle",img_scene);

imshow("Circle",img_scene);


        frame=new IplImage(rawMat);//
     }
     time(&end);
     ++counter;
     double sec=difftime(end,start);
     cout<<"millisec"<<sec/CLOCKS_PER_SEC*1000;
     cout<<"seconds"<<sec;
     double fps=counter/sec;
     printf("FPS: %0.2f\n",fps);

     cvShowImage("Capture Frame",frame);

     if((cvWaitKey(1) & 255) == 27 ) break;  
 }

 ClosePiCamera(Camera);

}

} While running the above code, the result is displayed very slowly. Is there any way I can improve the above code to make it more real time? To improve the performance I tried using pthreads.

pthreads.