Exemple #1
0
int main(int argc, const char* argv[])
{
    if (argc != 2)
        return -1;

    const std::string fname(argv[1]);

    cv::namedWindow("CPU", cv::WINDOW_NORMAL);
    cv::namedWindow("GPU", cv::WINDOW_OPENGL);
    cv::cuda::setGlDevice();

    cv::Mat frame;
    cv::VideoCapture reader(fname);

    cv::cuda::GpuMat d_frame;
    cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);

    TickMeter tm;
    std::vector<double> cpu_times;
    std::vector<double> gpu_times;

    for (;;)
    {
        tm.reset();
        tm.start();
        if (!reader.read(frame))
            break;
        tm.stop();
        cpu_times.push_back(tm.getTimeMilli());

        tm.reset();
        tm.start();
        if (!d_reader->nextFrame(d_frame))
            break;
        tm.stop();
        gpu_times.push_back(tm.getTimeMilli());

        cv::imshow("CPU", frame);
        cv::imshow("GPU", d_frame);

        if (cv::waitKey(3) > 0)
            break;
    }

    if (!cpu_times.empty() && !gpu_times.empty())
    {
        std::cout << std::endl << "Results:" << std::endl;

        std::sort(cpu_times.begin(), cpu_times.end());
        std::sort(gpu_times.begin(), gpu_times.end());

        double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
        double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();

        std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
        std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
    }

    return 0;
}
bool ObjectRecognition::readDatabase(const string& dir, vector<Mat>& databaseDescriptors, vector<string>& files)
{
   TickMeter tm;
   tm.start();  
   getdir(dir,files);
   string outString = "Start Reading Directory.png";
   cout << outString << endl;
   
   string extention = ".png"; 
   vector<string>::iterator it = files.begin();
   for (unsigned int i = 0;i < files.size();i++) 
   {
       if ( files[i].size() > 4 && files[i].compare( files[i].size() - 4, 4 , extention) == 0)
       {
     	   Mat img = imread( dir + files[i] , CV_LOAD_IMAGE_GRAYSCALE );
           //if( img.empty() )  cout << "Database descriptor " << files[i] << " can not be read or has no information." << endl;
           
           //cout << files[i]  << "\tRows" << img.rows << "\t Cols" << img.cols << "\t Type/Depth: " << img.depth() << endl;    
           img.assignTo(img, 5);
           
           databaseDescriptors.push_back( img );
       }
       it++;
   }
   tm.stop();
   cout << "End reading directory in " << tm.getTimeMilli() << " ms, of size " << DB.size() << endl;
   return true;
}
Exemple #3
0
int main(int argc, char** argv) {
    using namespace std;
    using namespace cv;

    VideoCapture cap(0);
    if (!cap.isOpened())
        exit(1);

    if (argc > 2) {
        cap.set(CV_CAP_PROP_FRAME_WIDTH, atoi(argv[1]));
        cap.set(CV_CAP_PROP_FRAME_HEIGHT, atoi(argv[2]));
    }

    CascadeClassifier cascade;
    if (!cascade.load("haarcascade_frontalface_default.xml"))
        exit(2);

    const char* name = basename(argv[0]);
    namedWindow(name);
    for (int frame = 1;; frame++) {
        static double mean = 0;
        TickMeter tm;
        Mat img, gray;

        tm.start();
        cap >> img;

        cvtColor(img, gray, CV_BGR2GRAY);
        equalizeHist(gray, gray);

        vector<Rect> objects;
        cascade.detectMultiScale(gray, objects, 1.2, 9,
                CV_HAAR_DO_CANNY_PRUNING);
        typedef vector<Rect>::const_iterator RCI;
        for (RCI i = objects.begin(); i != objects.end(); ++i) {
            Point center(cvRound(i->x+i->width/2),cvRound(i->y+i->height/2));
            int radius = cvRound(i->width / 2);
            circle(img, center, radius, Scalar(128,255,128), 2, 8, 0);
        }

        imshow(name, img);

        tm.stop();
        mean += tm.getTimeMilli();
        if (frame % 25 == 0) {
            printf("avg detect time: %.2f ms\n", mean / 25);
            mean = 0;
        }

        switch (waitKey(10)) {
        case 'q': case 27:
            exit(0);
            break;
        }
    }
}
static void matchDescriptors( const Mat& queryDescriptors, const vector<Mat>& trainDescriptors,
                              vector<DMatch>& matches, Ptr<DescriptorMatcher>& descriptorMatcher )
{
    cout << "< Set train descriptors collection in the matcher and match query descriptors to them..." << endl;
    TickMeter tm;

    tm.start();
    descriptorMatcher->add( trainDescriptors );
    descriptorMatcher->train();
    tm.stop();
    double buildTime = tm.getTimeMilli();

    tm.start();
    descriptorMatcher->match( queryDescriptors, matches );
    tm.stop();
    double matchTime = tm.getTimeMilli();

    CV_Assert( queryDescriptors.rows == (int)matches.size() || matches.empty() );

    cout << "Number of matches: " << matches.size() << endl;
    cout << "Build time: " << buildTime << " ms; Match time: " << matchTime << " ms" << endl;
    cout << ">" << endl;
}
bool ObjectRecognition::loadImageDB()
{
   TickMeter tm;
   tm.start();  
   vector<string> files;
   
   getdir(DBdirName,files);
   
   string extention = ".png"; 

   vector<string>::iterator it = files.begin();
   vector<Mat> descriptorDatabase;
   for (unsigned int i = 0;i < files.size();i++) 
   {
       if ( files[i].size() > 4 && files[i].compare( files[i].size() - 4, 4 , extention) == 0)
       {
           DBobj DBentry;
           DBentry.name = files[i];
     	   DBentry.img = imread( DBdirName + files[i] );
           if( DBentry.img.empty() )  cout << "Image: " << files[i] << " can not be read or has no information." << endl;
           
           DBentry.img.assignTo(DBentry.img, CV_8U);
           //cout << files[i]  << "\tRows" << DBentry.img.rows << "\t Cols" << DBentry.img.cols << "\t Type/Depth: " << DBentry.img.depth() << endl;    
           
           detectKeypointsSingle(DBentry.img, DBentry.keypoints );
           //cout << files[i] << "\t# Keypoints:" << DBentry.keypoints.size() << endl;
           if (DBentry.keypoints.size() > 9)
           {
           
               computeDescriptorsSingle(DBentry.img, DBentry.keypoints, DBentry.description);
               //cout << files[i] << "\t# of Descriptors: " << DBentry.description.rows << "\t# of Dimensions for descriptor: " << DBentry.description.cols  
               //      << "\tType/depth: " << DBentry.description.type() << " | " << DBentry.description.depth() << endl;
           
               descriptorDatabase.push_back(DBentry.description);
               DB.push_back( DBentry );
               
           }
       }
       it++;
   }
   // Add Database to matcher program.
   matcher->add(descriptorDatabase);
   matcher->train();
   
   tm.stop();
   cout << "End reading directory in " << tm.getTimeMilli() << " ms, of size " << DB.size() << endl;
   return true;
}
int main(int argc, const char *argv[])
{
    if (argc == 1)
    {
        help();
        return -1;
    }

    if (getCudaEnabledDeviceCount() == 0)
    {
        return cerr << "No GPU found or the library is compiled without GPU support" << endl, -1;
    }

    cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());

    string cascadeName;
    string inputName;
    bool isInputImage = false;
    bool isInputVideo = false;
    bool isInputCamera = false;

    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--cascade")
            cascadeName = argv[++i];
        else if (string(argv[i]) == "--video")
        {
            inputName = argv[++i];
            isInputVideo = true;
        }
        else if (string(argv[i]) == "--camera")
        {
            inputName = argv[++i];
            isInputCamera = true;
        }
        else if (string(argv[i]) == "--help")
        {
            help();
            return -1;
        }
        else if (!isInputImage)
        {
            inputName = argv[i];
            isInputImage = true;
        }
        else
        {
            cout << "Unknown key: " << argv[i] << endl;
            return -1;
        }
    }

    CascadeClassifier_GPU cascade_gpu;
    if (!cascade_gpu.load(cascadeName))
    {
        return cerr << "ERROR: Could not load cascade classifier \"" << cascadeName << "\"" << endl, help(), -1;
    }

    CascadeClassifier cascade_cpu;
    if (!cascade_cpu.load(cascadeName))
    {
        return cerr << "ERROR: Could not load cascade classifier \"" << cascadeName << "\"" << endl, help(), -1;
    }

    VideoCapture capture;
    Mat image;

    if (isInputImage)
    {
        image = imread(inputName);
        CV_Assert(!image.empty());
    }
    else if (isInputVideo)
    {
        capture.open(inputName);
        CV_Assert(capture.isOpened());
    }
    else
    {
        capture.open(atoi(inputName.c_str()));
        CV_Assert(capture.isOpened());
    }

    namedWindow("result", 1);

    Mat frame, frame_cpu, gray_cpu, resized_cpu, faces_downloaded, frameDisp;
    vector<Rect> facesBuf_cpu;

    GpuMat frame_gpu, gray_gpu, resized_gpu, facesBuf_gpu;

    /* parameters */
    bool useGPU = true;
    double scaleFactor = 1.0;
    bool findLargestObject = false;
    bool filterRects = true;
    bool helpScreen = false;

    int detections_num;
    for (;;)
    {
        if (isInputCamera || isInputVideo)
        {
            capture >> frame;
            if (frame.empty())
            {
                break;
            }
        }

        (image.empty() ? frame : image).copyTo(frame_cpu);
        frame_gpu.upload(image.empty() ? frame : image);

        convertAndResize(frame_gpu, gray_gpu, resized_gpu, scaleFactor);
        convertAndResize(frame_cpu, gray_cpu, resized_cpu, scaleFactor);

        TickMeter tm;
        tm.start();

        if (useGPU)
        {
            cascade_gpu.visualizeInPlace = true;
            cascade_gpu.findLargestObject = findLargestObject;

            detections_num = cascade_gpu.detectMultiScale(resized_gpu, facesBuf_gpu, 1.2,
                                                          (filterRects || findLargestObject) ? 4 : 0);
            facesBuf_gpu.colRange(0, detections_num).download(faces_downloaded);
        }
        else
        {
            Size minSize = cascade_gpu.getClassifierSize();
            cascade_cpu.detectMultiScale(resized_cpu, facesBuf_cpu, 1.2,
                                         (filterRects || findLargestObject) ? 4 : 0,
                                         (findLargestObject ? CV_HAAR_FIND_BIGGEST_OBJECT : 0)
                                            | CV_HAAR_SCALE_IMAGE,
                                         minSize);
            detections_num = (int)facesBuf_cpu.size();
        }

        if (!useGPU && detections_num)
        {
            for (int i = 0; i < detections_num; ++i)
            {
                rectangle(resized_cpu, facesBuf_cpu[i], Scalar(255));
            }
        }

        if (useGPU)
        {
            resized_gpu.download(resized_cpu);
        }

        tm.stop();
        double detectionTime = tm.getTimeMilli();
        double fps = 1000 / detectionTime;

        //print detections to console
        cout << setfill(' ') << setprecision(2);
        cout << setw(6) << fixed << fps << " FPS, " << detections_num << " det";
        if ((filterRects || findLargestObject) && detections_num > 0)
        {
            Rect *faceRects = useGPU ? faces_downloaded.ptr<Rect>() : &facesBuf_cpu[0];
            for (int i = 0; i < min(detections_num, 2); ++i)
            {
                cout << ", [" << setw(4) << faceRects[i].x
                     << ", " << setw(4) << faceRects[i].y
                     << ", " << setw(4) << faceRects[i].width
                     << ", " << setw(4) << faceRects[i].height << "]";
            }
        }
        cout << endl;

        cvtColor(resized_cpu, frameDisp, CV_GRAY2BGR);
        displayState(frameDisp, helpScreen, useGPU, findLargestObject, filterRects, fps);
        imshow("result", frameDisp);

        char key = (char)waitKey(5);
        if (key == 27)
        {
            break;
        }

        switch (key)
        {
        case ' ':
            useGPU = !useGPU;
            break;
        case 'm':
        case 'M':
            findLargestObject = !findLargestObject;
            break;
        case 'f':
        case 'F':
            filterRects = !filterRects;
            break;
        case '1':
            scaleFactor *= 1.05;
            break;
        case 'q':
        case 'Q':
            scaleFactor /= 1.05;
            break;
        case 'h':
        case 'H':
            helpScreen = !helpScreen;
            break;
        }
    }
Exemple #7
0
int main(int argc, char** argv)
{
    if (argc != 3)
    {
        cerr << "Usage: stereo_multi_gpu <left_video> <right_video>" << endl;
        return -1;
    }

    const int numDevices = getCudaEnabledDeviceCount();
    if (numDevices != 2)
    {
        cerr << "Two GPUs are required" << endl;
        return -1;
    }

    for (int i = 0; i < numDevices; ++i)
    {
        DeviceInfo devInfo(i);
        if (!devInfo.isCompatible())
        {
            cerr << "CUDA module was't built for GPU #" << i << " ("
                 << devInfo.name() << ", CC " << devInfo.majorVersion()
                 << devInfo.minorVersion() << endl;
            return -1;
        }

        printShortCudaDeviceInfo(i);
    }

    VideoCapture leftVideo(argv[1]);
    VideoCapture rightVideo(argv[2]);

    if (!leftVideo.isOpened())
    {
         cerr << "Can't open " << argv[1] << " video file" << endl;
         return -1;
    }

    if (!rightVideo.isOpened())
    {
         cerr << "Can't open " << argv[2] << " video file" << endl;
         return -1;
    }

    cout << endl;
    cout << "This sample demonstrates working on one piece of data using two GPUs." << endl;
    cout << "It splits input into two parts and processes them separately on different GPUs." << endl;
    cout << endl;

    Mat leftFrame, rightFrame;
    CudaMem leftGrayFrame, rightGrayFrame;

    StereoSingleGpu gpu0Alg(0);
    StereoSingleGpu gpu1Alg(1);
    StereoMultiGpuThread multiThreadAlg;
    StereoMultiGpuStream multiStreamAlg;

    Mat disparityGpu0;
    Mat disparityGpu1;
    Mat disparityMultiThread;
    CudaMem disparityMultiStream;

    Mat disparityGpu0Show;
    Mat disparityGpu1Show;
    Mat disparityMultiThreadShow;
    Mat disparityMultiStreamShow;

    TickMeter tm;

    cout << "-------------------------------------------------------------------" << endl;
    cout << "| Frame | GPU 0 ms | GPU 1 ms | Multi Thread ms | Multi Stream ms |" << endl;
    cout << "-------------------------------------------------------------------" << endl;

    for (int i = 0;; ++i)
    {
        leftVideo >> leftFrame;
        rightVideo >> rightFrame;

        if (leftFrame.empty() || rightFrame.empty())
            break;

        if (leftFrame.size() != rightFrame.size())
        {
            cerr << "Frames have different sizes" << endl;
            return -1;
        }

        leftGrayFrame.create(leftFrame.size(), CV_8UC1);
        rightGrayFrame.create(leftFrame.size(), CV_8UC1);

        cvtColor(leftFrame, leftGrayFrame.createMatHeader(), COLOR_BGR2GRAY);
        cvtColor(rightFrame, rightGrayFrame.createMatHeader(), COLOR_BGR2GRAY);

        tm.reset(); tm.start();
        gpu0Alg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
                        disparityGpu0);
        tm.stop();

        const double gpu0Time = tm.getTimeMilli();

        tm.reset(); tm.start();
        gpu1Alg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
                        disparityGpu1);
        tm.stop();

        const double gpu1Time = tm.getTimeMilli();

        tm.reset(); tm.start();
        multiThreadAlg.compute(leftGrayFrame.createMatHeader(), rightGrayFrame.createMatHeader(),
                               disparityMultiThread);
        tm.stop();

        const double multiThreadTime = tm.getTimeMilli();

        tm.reset(); tm.start();
        multiStreamAlg.compute(leftGrayFrame, rightGrayFrame, disparityMultiStream);
        tm.stop();

        const double multiStreamTime = tm.getTimeMilli();

        cout << "| " << setw(5) << i << " | "
             << setw(8) << setprecision(1) << fixed << gpu0Time << " | "
             << setw(8) << setprecision(1) << fixed << gpu1Time << " | "
             << setw(15) << setprecision(1) << fixed << multiThreadTime << " | "
             << setw(15) << setprecision(1) << fixed << multiStreamTime << " |" << endl;

        resize(disparityGpu0, disparityGpu0Show, Size(1024, 768), 0, 0, INTER_AREA);
        resize(disparityGpu1, disparityGpu1Show, Size(1024, 768), 0, 0, INTER_AREA);
        resize(disparityMultiThread, disparityMultiThreadShow, Size(1024, 768), 0, 0, INTER_AREA);
        resize(disparityMultiStream.createMatHeader(), disparityMultiStreamShow, Size(1024, 768), 0, 0, INTER_AREA);

        imshow("disparityGpu0", disparityGpu0Show);
        imshow("disparityGpu1", disparityGpu1Show);
        imshow("disparityMultiThread", disparityMultiThreadShow);
        imshow("disparityMultiStream", disparityMultiStreamShow);

        const int key = waitKey(30) & 0xff;
        if (key == 27)
            break;
    }

    cout << "-------------------------------------------------------------------" << endl;

    return 0;
}
void App::run(int argc, char **argv)
{
    parseCmdArgs(argc, argv);
    if (help_showed) 
        return;

    if (getCudaEnabledDeviceCount() == 0)
        throw runtime_error("No GPU found or the library is compiled without GPU support");    

    if (cascade_name.empty())
    {
        cout << "Using default cascade file...\n";
        cascade_name = "data/face_detect/haarcascade_frontalface_alt.xml";
    }      

    if (!cascade_gpu.load(cascade_name) || !cascade_cpu.load(cascade_name))
    {
        stringstream msg;
        msg << "Could not load cascade classifier \"" << cascade_name << "\"";
        throw runtime_error(msg.str());
    }

    if (sources.size() != 1)
    {
        cout << "Loading default frames source...\n";
        sources.resize(1);
        sources[0] = new VideoSource("data/face_detect/browser.flv");
    }

    Mat frame, frame_cpu, gray_cpu, resized_cpu, faces_downloaded, frameDisp;
    vector<Rect> facesBuf_cpu;

    GpuMat frame_gpu, gray_gpu, resized_gpu, facesBuf_gpu;

    int detections_num;
    while (!exited)
    {
        sources[0]->next(frame_cpu);
        frame_gpu.upload(frame_cpu);

        convertAndResize(frame_gpu, gray_gpu, resized_gpu, scaleFactor);
        convertAndResize(frame_cpu, gray_cpu, resized_cpu, scaleFactor);

        TickMeter tm;
        tm.start();

        if (useGPU)
        {
            cascade_gpu.visualizeInPlace = true;
            cascade_gpu.findLargestObject = findLargestObject;

            detections_num = cascade_gpu.detectMultiScale(resized_gpu, facesBuf_gpu, 1.2,
                                                          (filterRects || findLargestObject) ? 4 : 0);
            facesBuf_gpu.colRange(0, detections_num).download(faces_downloaded);
        }
        else
        {
            Size minSize = cascade_gpu.getClassifierSize();
            cascade_cpu.detectMultiScale(resized_cpu, facesBuf_cpu, 1.2,
                                         (filterRects || findLargestObject) ? 4 : 0,
                                         (findLargestObject ? CV_HAAR_FIND_BIGGEST_OBJECT : 0)
                                            | CV_HAAR_SCALE_IMAGE,
                                         minSize);
            detections_num = (int)facesBuf_cpu.size();
        }

        if (!useGPU && detections_num)
        {
            for (int i = 0; i < detections_num; ++i)
            {
                rectangle(resized_cpu, facesBuf_cpu[i], Scalar(255));
            }
        }

        if (useGPU)
        {
            resized_gpu.download(resized_cpu);
        }

        tm.stop();
        double detectionTime = tm.getTimeMilli();
        double fps = 1000 / detectionTime;

        /*//print detections to console
        cout << setfill(' ') << setprecision(2);
        cout << setw(6) << fixed << fps << " FPS, " << detections_num << " det";
        if ((filterRects || findLargestObject) && detections_num > 0)
        {
            Rect *faceRects = useGPU ? faces_downloaded.ptr<Rect>() : &facesBuf_cpu[0];
            for (int i = 0; i < min(detections_num, 2); ++i)
            {
                cout << ", [" << setw(4) << faceRects[i].x
                     << ", " << setw(4) << faceRects[i].y
                     << ", " << setw(4) << faceRects[i].width
                     << ", " << setw(4) << faceRects[i].height << "]";
            }
        }
        cout << endl;*/

        cvtColor(resized_cpu, frameDisp, CV_GRAY2BGR);
        displayState(frameDisp, helpScreen, useGPU, findLargestObject, filterRects, fps);
        imshow("face_detect_demo", frameDisp);

        processKey(waitKey(3));
    }   
}
Exemple #9
0
void bingQdpmRocTest(vector<string> &dirs,
					 int windowLimit = -1, double timeLimitMs = -1, float ratioThreshold = -1)
{
	size_t imageCount = 0;
	size_t personCount = 0;
	size_t matchCount = 0;
	vector<ScoreTp> pScores;
	TickMeter tm;
	vector<std::string>::const_iterator it = dirs.begin();
	char buf[512];

	for (; it != dirs.end(); it++) {
		string dir = *it;
		DataSetVOC voc(dir, true, true);
		voc.loadAnnotations();
		const size_t testNum = voc.testSet.size();
		const char *imgPath =_S(voc.imgPathW);

		// Objectness
		double base = 2;
		double intUionThr = 0.5;
		int W = 8;
		int NSS = 2;

#ifdef WINDOW_GUESS
		Objectness objNess(voc, base, intUionThr, W, NSS);

		objNess.loadTrainedModel(TRAIN_MODEL);
#endif

		// LSVM DPM
		string dpmPersonModel = "../ExtraData/latentsvmXml/person.xml";
		vector<string> models;
		models.push_back(dpmPersonModel);
		QUniLsvmDetector detector(models);
		float overlapThreshold = 0.2f;

		if (ratioThreshold > 0)
			detector.setRatioThreshold(ratioThreshold);

		printf("%d: \n", testNum);
		for (int i = 0; i < testNum; i++) {
			const vector<Vec4i> &boxesGT = voc.gtTestBoxes[i];
			const size_t gtNumCrnt = boxesGT.size();
			if (gtNumCrnt <= 0)
				continue;

			imageCount++;
			personCount += gtNumCrnt;

			Mat image = imread(format(imgPath, _S(voc.testSet[i])));
			if (image.ptr() == NULL) {
				fprintf(stderr, "No JPG Image !\n");
				exit(1);
			}

			int numPerSz = 130;
			ValStructVec<float, Vec4i> boxes;
			double preObj = tm.getTimeMilli();
			double objTime = 0.;

#ifdef WINDOW_GUESS // window guess
			tm.start();
			objNess.getObjBndBoxes(image, boxes, numPerSz);
			tm.stop();
			objTime = tm.getTimeMilli() - preObj;
#endif

			double localTimeLimitMs = timeLimitMs;
			if (timeLimitMs > 0) {
				localTimeLimitMs -= objTime;
				if (localTimeLimitMs < 0.)
					localTimeLimitMs = 0.;
			}

			vector<QRect> searchBoxes;
			if (windowLimit > 0) {
				for (int j = 0; j < (int)boxes.size() && j < windowLimit; j++) {
					const Vec4i &bb = boxes[j];
					QRect rt(bb[0], bb[1], bb[2], bb[3]);
					searchBoxes.push_back(rt);
				}
			} else {
				for (int j = 0; j < (int)boxes.size(); j++) {
					const Vec4i &bb = boxes[j];
					QRect rt(bb[0], bb[1], bb[2], bb[3]);
					searchBoxes.push_back(rt);
				}
			}

			tm.start();
			detector.setup(image, overlapThreshold, localTimeLimitMs);
			tm.stop();

			vector<FeatureMapCoord> ftrMapCoords;
#ifdef WINDOW_GUESS
			detector.cvtBox2FtrMapCoord(&searchBoxes, &ftrMapCoords);
#else
			detector.genFullFtrMapCoord(&ftrMapCoords);

			preObj = tm.getTimeMilli();
			tm.start();
#ifdef SHUFFLE_WINDOW
			random_shuffle(ftrMapCoords.begin(), ftrMapCoords.end());
#endif
			tm.stop();
			double randGenTime = tm.getTimeMilli() - preObj;

			if (localTimeLimitMs > 0 && localTimeLimitMs - preObj >= 0.)
					detector.setTimeLimit(localTimeLimitMs - preObj);
#endif

			vector<QUniLsvmDetector::ObjectDetection> detections;
			vector<vector<FeatureMapCoord> *> fmcss;
			fmcss.push_back(&ftrMapCoords);
			tm.start();
			detector.detect(detections, fmcss);
			tm.stop();

			vector<DetectedInfo> di(detections.size());
			vector<int> gtIdx(gtNumCrnt, -1);

			int detectCount = 0;
			for (size_t j = 0; j < detections.size(); j++) {
				const QUniLsvmDetector::ObjectDetection& od = detections[j];

				if (od.score < RECOMMENDABLE_THRESHOLD)
					continue;

				detectCount++;
				Vec4i bb(od.rect.x + 1, od.rect.y + 1, od.rect.x + od.rect.width, od.rect.y + od.rect.height);

				// score matchScore for the ROC curve
				double maxMatchScore = 0;
				int maxMatchId = -1;
				for (int k = 0; k < gtNumCrnt; k++) {
					double matchScore = DataSetVOC::interUnio(bb, boxesGT[k]);
					if (matchScore > maxMatchScore) {
						maxMatchScore = matchScore;
						maxMatchId = k;
					}
				}

				uchar match = maxMatchScore > 0.5 ? 1 : 0;
				if (match) {
					int preDetectedIdx = gtIdx[maxMatchId];
					if (preDetectedIdx >= 0) {
						if (maxMatchScore > di[preDetectedIdx].matchScore) {
							di[preDetectedIdx].matched = false;
							gtIdx[maxMatchId] = int(j);
							di[j].matchScore = maxMatchScore;
							di[j].matched = true;
						}
					} else {
						gtIdx[maxMatchId] = int(j);
						di[j].matchScore = maxMatchScore;
						di[j].matched = true;
					}
				}

#ifdef SAVE_IMAGE_RESULT
				// save the result image
				char buf[256];
				sprintf(buf, "%2f", od.score);
				Point pt(max((bb[2] + bb[0] - 85) / 2, 0), (bb[1] + bb[3]) / 2);
				putText(image, buf, pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar::all(255), 3, CV_AA);
				putText(image, buf, pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar::all(0), 1, CV_AA);
				rectangle(image, od.rect, cv::Scalar(0, 255, 0), 2);
#endif
			}

			for (size_t j = 0; j < detectCount; j++) { // detections are sorted in descending order
				const QUniLsvmDetector::ObjectDetection& od = detections[j];
				if (di[j].matched)
					matchCount++;
				pScores.push_back(ScoreTp(od.score, di[j].matched));
			}

#ifdef SAVE_IMAGE_RESULT
			imwrite((voc.testSet[i] + "_DpmResult.png").c_str(), image);
#endif
			printf("%d ", i + 1);
		}
		printf("\n");
	}
	printf("BingQdpmRocTest time = %f sec\n", tm.getTimeSec());
	printf("GT %d, Matched %d/%d \n", personCount, matchCount, pScores.size());

	// Calculate log-average miss rate
	stable_sort(begin(pScores), end(pScores),
		[](const ScoreTp &p1, const ScoreTp &p2) { return p1.first > p2.first; });

	vector<float> fp(pScores.size());
	for (size_t i = 0; i < fp.size(); i++)
		fp[i] = !pScores[i].second;

	vector<float> tp(pScores.size());
	tp[0] = pScores[0].second;
	for (size_t i = 1; i < tp.size(); i++)
		tp[i] = tp[i - 1] + pScores[i].second;
	for (size_t i = 0; i < tp.size(); i++)
		tp[i] /= personCount;

	for (size_t i = 1; i < fp.size(); i++)
		fp[i] += fp[i - 1];
	for (size_t i = 0; i < fp.size(); i++)
		fp[i] /= imageCount;

	sprintf(buf, "%s%03d_%03.fms_6137gt_%04dtp_%04ddt_%.0fs.m",
		METHOD_NAME, max(windowLimit, 0), timeLimitMs, matchCount, pScores.size(), tm.getTimeSec());
	FILE *matlabFile = fopen(buf, "w");
	printVector(matlabFile, tp, "tp");
	printVector(matlabFile, fp, "fp");

	char *matlabContent = "tp = tp'; fp = fp';\n"
		"addpath(genpath('d:/81_dataset/03_Caltech/piotr_toolbox/')); savepath;\n"
		"xs1=[-inf; fp];\n"
		"ys1=[0; tp];\n"
		"ref=10.^(-2:.25:0);\n"
		"lims=[3.1e-4 1e1 .5 1];\n"
		"m=length(ref);\n"
		"for i=1:m\n"
		"\tj=find(xs1<=ref(i));\n"
		"\tmiss(i)=ys1(j(end));\n"
		"end\n"
		"miss=exp(mean(log(max(1e-10,1-miss))));\n"
		"show=figure();\n"
		"plotRoc([fp tp],'logx',1,'logy',1,'xLbl','fppi',...\n"
		"\t'lims',lims,'color','g','smooth',1,'fpTarget',ref);\n"
		"title(sprintf('log-average miss rate = %.2f%%',miss*100));\n"
		"savefig(['MORU' 'Roc'],show,'png');\n";
	fwrite(matlabContent, strlen(matlabContent), 1, matlabFile);

	fclose(matlabFile);
}
Exemple #10
0
int main(int argc, const char *argv[])
{
    if (getCudaEnabledDeviceCount() == 0)
    {
        return cerr << "No GPU found or the library is compiled without CUDA support" << endl, -1;
    }

    cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());

    ////////////// CAFFE /////////////////////

//    Caffe::set_mode(Caffe::GPU);
//    Caffe::SetDevice(0);
//
//	caffe::Datum* datum = new caffe::Datum();
//	CVMatToDatum(cropImg, datum);
//
//    // Load net
//	// Assume you are in Caffe master directory
//	caffe::Net<float> net("/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/OpenCV249U1404/Debug/bvlc_googlenet.prototxt", TEST);
//
//	// Load pre-trained net (binary proto)
//	// Assume you are already trained the cifar10 example.
//	net.CopyTrainedLayersFrom("/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/OpenCV249U1404/Debug/bvlc_googlenet.caffemodel");
//
//	caffe::Blob<float>* input_blob = new caffe::Blob<float>(1, datum->channels(), datum->height(), datum->width());
//	 //get the blobproto
//	caffe::BlobProto blob_proto;
//	blob_proto.set_num(1);
//	blob_proto.set_channels(datum->channels());
//	blob_proto.set_height(datum->height());
//	blob_proto.set_width(datum->width());
//
//	const string& data = datum->data();
//	for (uint32_t i = 0; i < data.length(); ++i) {
//		blob_proto.add_data((uint8_t)data[i]);
//	}
//
//	//set data into blob
//	input_blob->FromProto(blob_proto);
//
//	std::vector<caffe::Blob<float>*> input_cnn;
//	input_cnn.push_back(input_blob);
//
//	float loss;
//	std::vector<caffe::Blob<float>*> input_blobs = net.input_blobs();
//	for (int i = 0; i < input_cnn.size(); ++i) {
//		input_blobs[i]->CopyFrom(*input_cnn[i]);
//	}
//	const std::vector<caffe::Blob<float>*>& result = net.ForwardPrefilled(&loss);
//
//	std::cout << "loss: " << loss << "\n";
//	// read the 'prob' layer and get the result
//
//	shared_ptr<caffe::Blob<float> > prob = net.blob_by_name("prob");
//
//	float maxval= 0;
//	int   maxinx= 0;
//	for (int i = 0; i < prob->count(); i++)
//	{
//		float val = (prob->cpu_data()[i]) * 100;
//		if (val> maxval)
//		{
//			maxval = val;
//			maxinx = i;
//		}
//		std::cout << "[" << i << "]" << val<< "\n";
//	}
//	std::cout << "Max value = " << maxval << ", Max index = " << maxinx<< "\n";

//    Mat cropImg = cv::imread("/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/80.jpg",CV_LOAD_IMAGE_COLOR);
//	imshow("crop",cropImg);
//	cv::resize(cropImg, cropImg, cv::Size(224, 224));

    string model_file   = "/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/deploy.prototxt";
    string trained_file = "/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/snapshot_iter_14640.caffemodel";
    string mean_file    = "/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/mean.binaryproto";
    string label_file   = "/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/labels.txt";

    CaffeClassifier CaffeClassifier(model_file, trained_file, mean_file, label_file);
//
//	string file = "/home/k1y0sh1/DeveloperZone/Project/EclipseWorkplace/CaffePredictTest/Debug/80.jpg";
//
//	std::cout << "---------- Prediction for "
//			<< file << " ----------" << std::endl;
//
//	std::vector<Prediction> predictions = CaffeClassifier.Classify(cropImg,1);
//
//	/* Print the top N predictions. */
//	for (size_t i = 0; i < predictions.size(); ++i)
//	{
//		Prediction p = predictions[i];
//		std::cout << std::fixed << std::setprecision(4) << p.second << " - \"" << p.first << "\"" << std::endl;
//	}


    ////////////// END CAFFE /////////////////////

    ////////////// HAAR /////////////////////

    string cascadeName = "/home/k1y0sh1/DeveloperZone/HaarTraining/classifiers/cascade.xml";

    Ptr<cuda::CascadeClassifier> cascade_gpu = cuda::CascadeClassifier::create(cascadeName);

    Mat image;

    namedWindow("result", 1);

    Mat frame, frame_cpu, gray_cpu, resized_cpu, frameDisp;
    vector<Rect> faces;

    GpuMat frame_gpu, gray_gpu, resized_gpu, facesBuf_gpu, k_rgb_gpu;

    /* parameters */
    bool useGPU = true;
    double scaleFactor = 0.5;
    bool findLargestObject = true;
    bool filterRects = true;
    bool helpScreen = false;
    bool predictObject = false;

    ////////////// END HAAR /////////////////////

    //////////////////// KINECT /////////////////////

    std::string program_path(argv[0]);
    size_t executable_name_idx = program_path.rfind("OpenCVKinectGPU");

    std::string binpath = "/";

    if(executable_name_idx != std::string::npos)
    {
        binpath = program_path.substr(0, executable_name_idx);
    }

    libfreenect2::Freenect2 freenect2;
    libfreenect2::Freenect2Device *dev = 0;
    libfreenect2::PacketPipeline *pipeline = 0;

    if(freenect2.enumerateDevices() == 0)
    {
        std::cout << "no device connected!" << std::endl;
        return -1;
    }

    std::string serial = freenect2.getDefaultDeviceSerialNumber();

    for(int argI = 1; argI < argc; ++argI)
    {
    const std::string arg(argv[argI]);

    if(arg == "cpu")
    {
      if(!pipeline)
        pipeline = new libfreenect2::CpuPacketPipeline();
    }
    else if(arg == "gl")
    {
    #ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
      if(!pipeline)
        pipeline = new libfreenect2::OpenGLPacketPipeline();
    #else
      std::cout << "OpenGL pipeline is not supported!" << std::endl;
    #endif
    }
    else if(arg == "cl")
    {
    #ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT
      if(!pipeline)
        pipeline = new libfreenect2::OpenCLPacketPipeline();
    #else
      std::cout << "OpenCL pipeline is not supported!" << std::endl;
    #endif
    }
    else if(arg.find_first_not_of("0123456789") == std::string::npos) //check if parameter could be a serial number
    {
      serial = arg;
    }
    else
    {
      std::cout << "Unknown argument: " << arg << std::endl;
    }
    }

    if(pipeline)
    {
    dev = freenect2.openDevice(serial, pipeline);
    }
    else
    {
    dev = freenect2.openDevice(serial);
    }

    if(dev == 0)
    {
    std::cout << "failure opening device!" << std::endl;
    return -1;
    }

    signal(SIGINT,sigint_handler);
    protonect_shutdown = false;

    libfreenect2::SyncMultiFrameListener listener(libfreenect2::Frame::Color);
    libfreenect2::FrameMap frames;

    dev->setColorFrameListener(&listener);
    dev->start();

    std::cout << "device serial: " << dev->getSerialNumber() << std::endl;
    std::cout << "device firmware: " << dev->getFirmwareVersion() << std::endl;

    libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams());

    /////////////////// END KINECT /////////////////

    while(!protonect_shutdown)
    {
        listener.waitForNewFrame(frames);
        libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color];

        cv::Mat k_rgb = cv::Mat(rgb->height, rgb->width, CV_8UC4, rgb->data);

        image = Mat(k_rgb);
        frame_gpu.upload(k_rgb);

        cuda::flip(frame_gpu,frame_gpu,1);
        cv::flip(image,image,1);
        cuda::cvtColor(frame_gpu,k_rgb_gpu,CV_BGRA2BGR);
        convertAndResizeGPU(k_rgb_gpu, gray_gpu, resized_gpu, scaleFactor);
        convertAndResizeCPU(image,image,scaleFactor);

        TickMeter tm;
        tm.start();

        //cascade_gpu->setMaxNumObjects(2);
        //cascade_gpu->setMaxObjectSize(cv::Size(224,224));
        //cascade_gpu->setMinObjectSize(cv::Size(0,0));
        cascade_gpu->setFindLargestObject(findLargestObject);
        cascade_gpu->setScaleFactor(1.2);
        cascade_gpu->setMinNeighbors((filterRects || findLargestObject) ? 4 : 0);
        cascade_gpu->detectMultiScale(resized_gpu, facesBuf_gpu);
        cascade_gpu->convert(facesBuf_gpu, faces);

        for (size_t i = 0; i < faces.size(); ++i)
        {
            //cout<< "object [" << i << "]: " << faces[i].width << " x " << faces[i].height <<endl;
            rectangle(image, faces[i], Scalar(255));
            cropRect = Rect(image.cols / 2, image.rows / 2,224,224);
            Mat cropImg = image(cropRect).clone();

            if(predictObject == true)
            {
                std::vector<Prediction> predictions = CaffeClassifier.Classify(cropImg,1);

                /* Print the top N predictions. */
                for (size_t i = 0; i < predictions.size(); ++i)
                {
                    Prediction p = predictions[i];
                    std::cout << std::fixed << std::setprecision(4) << p.second << " - \"" << p.first << "\"" << std::endl;
                }

                predictObject = false;
            }
        }


        tm.stop();
        double detectionTime = tm.getTimeMilli();
        double fps = 1000 / detectionTime;

        displayState(image, helpScreen, useGPU, findLargestObject, filterRects, fps,scaleFactor);
        imshow("result", image);

        char key = (char)waitKey(5);
        if (key == 27)
        {
            break;
        }

        switch (key)
        {
        case ' ':
            useGPU = !useGPU;
            break;
        case 'm':
        case 'M':
            findLargestObject = !findLargestObject;
            break;
        case 'f':
        case 'F':
            filterRects = !filterRects;
            break;
        case '1':
            scaleFactor *= 1.05;
            break;
        case 'q':
        case 'Q':
            scaleFactor /= 1.05;
            break;
        case 'h':
        case 'H':
            helpScreen = !helpScreen;
            break;
        case 'p':
        case 'P':
            predictObject = !predictObject;
            break;
        }
        protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape

        listener.release(frames);
        //libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(100));
     }

    resized_gpu.release();

    // TODO: restarting ir stream doesn't work!
    // TODO: bad things will happen, if frame listeners are freed before dev->stop() :(
    dev->stop();
    dev->close();

    delete registration;

    return 0;
}
Exemple #11
0
int main(int argc, const char* argv[])
{
    CommandLineParser cmd(argc, argv,
        "{ image i        | ../data/pic1.png  | input image }"
        "{ template t     | templ.png | template image }"
        "{ full           |           | estimate scale and rotation }"
        "{ gpu            |           | use gpu version }"
        "{ minDist        | 100       | minimum distance between the centers of the detected objects }"
        "{ levels         | 360       | R-Table levels }"
        "{ votesThreshold | 30        | the accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected }"
        "{ angleThresh    | 10000     | angle votes treshold }"
        "{ scaleThresh    | 1000      | scale votes treshold }"
        "{ posThresh      | 100       | position votes threshold }"
        "{ dp             | 2         | inverse ratio of the accumulator resolution to the image resolution }"
        "{ minScale       | 0.5       | minimal scale to detect }"
        "{ maxScale       | 2         | maximal scale to detect }"
        "{ scaleStep      | 0.05      | scale step }"
        "{ minAngle       | 0         | minimal rotation angle to detect in degrees }"
        "{ maxAngle       | 360       | maximal rotation angle to detect in degrees }"
        "{ angleStep      | 1         | angle step in degrees }"
        "{ maxBufSize     | 1000      | maximal size of inner buffers }"
        "{ help h ?       |           | print help message }"
    );

    cmd.about("This program demonstrates arbitary object finding with the Generalized Hough transform.");

    if (cmd.has("help"))
    {
        cmd.printMessage();
        return 0;
    }

    const string templName = cmd.get<string>("template");
    const string imageName = cmd.get<string>("image");
    const bool full = cmd.has("full");
    const bool useGpu = cmd.has("gpu");
    const double minDist = cmd.get<double>("minDist");
    const int levels = cmd.get<int>("levels");
    const int votesThreshold = cmd.get<int>("votesThreshold");
    const int angleThresh = cmd.get<int>("angleThresh");
    const int scaleThresh = cmd.get<int>("scaleThresh");
    const int posThresh = cmd.get<int>("posThresh");
    const double dp = cmd.get<double>("dp");
    const double minScale = cmd.get<double>("minScale");
    const double maxScale = cmd.get<double>("maxScale");
    const double scaleStep = cmd.get<double>("scaleStep");
    const double minAngle = cmd.get<double>("minAngle");
    const double maxAngle = cmd.get<double>("maxAngle");
    const double angleStep = cmd.get<double>("angleStep");
    const int maxBufSize = cmd.get<int>("maxBufSize");

    if (!cmd.check())
    {
        cmd.printErrors();
        return -1;
    }

    Mat templ = loadImage(templName);
    Mat image = loadImage(imageName);

    Ptr<GeneralizedHough> alg;

    if (!full)
    {
        Ptr<GeneralizedHoughBallard> ballard = useGpu ? cuda::createGeneralizedHoughBallard() : createGeneralizedHoughBallard();

        ballard->setMinDist(minDist);
        ballard->setLevels(levels);
        ballard->setDp(dp);
        ballard->setMaxBufferSize(maxBufSize);
        ballard->setVotesThreshold(votesThreshold);

        alg = ballard;
    }
    else
    {
        Ptr<GeneralizedHoughGuil> guil = useGpu ? cuda::createGeneralizedHoughGuil() : createGeneralizedHoughGuil();

        guil->setMinDist(minDist);
        guil->setLevels(levels);
        guil->setDp(dp);
        guil->setMaxBufferSize(maxBufSize);

        guil->setMinAngle(minAngle);
        guil->setMaxAngle(maxAngle);
        guil->setAngleStep(angleStep);
        guil->setAngleThresh(angleThresh);

        guil->setMinScale(minScale);
        guil->setMaxScale(maxScale);
        guil->setScaleStep(scaleStep);
        guil->setScaleThresh(scaleThresh);

        guil->setPosThresh(posThresh);

        alg = guil;
    }

    vector<Vec4f> position;
    TickMeter tm;

    if (useGpu)
    {
        cuda::GpuMat d_templ(templ);
        cuda::GpuMat d_image(image);
        cuda::GpuMat d_position;

        alg->setTemplate(d_templ);

        tm.start();

        alg->detect(d_image, d_position);
        d_position.download(position);

        tm.stop();
    }
    else
    {
        alg->setTemplate(templ);

        tm.start();

        alg->detect(image, position);

        tm.stop();
    }

    cout << "Found : " << position.size() << " objects" << endl;
    cout << "Detection time : " << tm.getTimeMilli() << " ms" << endl;

    Mat out;
    cv::cvtColor(image, out, COLOR_GRAY2BGR);

    for (size_t i = 0; i < position.size(); ++i)
    {
        Point2f pos(position[i][0], position[i][1]);
        float scale = position[i][2];
        float angle = position[i][3];

        RotatedRect rect;
        rect.center = pos;
        rect.size = Size2f(templ.cols * scale, templ.rows * scale);
        rect.angle = angle;

        Point2f pts[4];
        rect.points(pts);

        line(out, pts[0], pts[1], Scalar(0, 0, 255), 3);
        line(out, pts[1], pts[2], Scalar(0, 0, 255), 3);
        line(out, pts[2], pts[3], Scalar(0, 0, 255), 3);
        line(out, pts[3], pts[0], Scalar(0, 0, 255), 3);
    }

    imshow("out", out);
    waitKey();

    return 0;
}
Exemple #12
0
int main(int argc, char **argv)
{
    CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }

    String modelFile = parser.get<String>("model");
    String imageFile = parser.get<String>("image");

    if (!parser.check())
    {
        parser.printErrors();
        return 0;
    }

    String classNamesFile = parser.get<String>("c_names");
    String resultFile = parser.get<String>("result");

    //! [Read model and initialize network]
    dnn::Net net = dnn::readNetFromTorch(modelFile);

    //! [Prepare blob]
    Mat img = imread(imageFile), input;
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }

    Size origSize = img.size();
    Size inputImgSize = cv::Size(1024, 512);

    if (inputImgSize != origSize)
        resize(img, img, inputImgSize);       //Resize image to input size

    Mat inputBlob = blobFromImage(img, 1./255);   //Convert Mat to image batch
    //! [Prepare blob]

    //! [Set input blob]
    net.setInput(inputBlob, "");        //set the network input
    //! [Set input blob]

    TickMeter tm;

    String oBlob = net.getLayerNames().back();
    if (!parser.get<String>("o_blob").empty())
    {
        oBlob = parser.get<String>("o_blob");
    }

    //! [Make forward pass]
    tm.start();
    Mat result = net.forward(oBlob);
    tm.stop();

    if (!resultFile.empty()) {
        CV_Assert(result.isContinuous());

        ofstream fout(resultFile.c_str(), ios::out | ios::binary);
        fout.write((char*)result.data, result.total() * sizeof(float));
        fout.close();
    }

    std::cout << "Output blob: " << result.size[0] << " x " << result.size[1] << " x " << result.size[2] << " x " << result.size[3] << "\n";
    std::cout << "Inference time, ms: " << tm.getTimeMilli()  << std::endl;

    if (parser.has("show"))
    {
        std::vector<String> classNames;
        vector<cv::Vec3b> colors;
        if(!classNamesFile.empty()) {
            colors = readColors(classNamesFile, classNames);
        }
        Mat segm, legend;
        colorizeSegmentation(result, segm, legend, classNames, colors);

        Mat show;
        addWeighted(img, 0.1, segm, 0.9, 0.0, show);

        cv::resize(show, show, origSize, 0, 0, cv::INTER_NEAREST);
        imshow("Result", show);
        if(classNames.size())
            imshow("Legend", legend);
        waitKey();
    }

    return 0;
} //main
void ObjectRecognition::matchObsvToDB(const Mat &img, string& ObjName)
{
    //img.assignTo(img, CV_8U);
    vector<DMatch> matches;
    vector<vector<DMatch> > total_matches;
    TickMeter tm;
    tm.start();
    vector<KeyPoint> imgKp;
    Mat imgDesc;
    detectKeypointsSingle(img, imgKp);
    computeDescriptorsSingle(img, imgKp, imgDesc);
    matcher->match( imgDesc, matches );
    
    
    
    /*/ 
     //Match each item in database to pic (problem is it then matches to best keypoint and need to find a way to see which image in database is best)
         // I tried variance of distances but that wasn't reliable, didn't try finding var of angle but probably wouldn't be reliable either
    for ( vector<DBobj>::iterator DBiter = DB.begin() ; DBiter != DB.end(); DBiter++ )
    {
  	matcher->match( imgDesc, DBiter->description, matches);
        total_matches.push_back(matches);
        
        float mean = 0, var = 0;
        
        for (vector<DMatch>::iterator DMiter = matches.begin(); DMiter != matches.end(); DMiter++) mean += DMiter->distance; 
        mean = mean / matches.size();
        for (vector<DMatch>::iterator DMiter = matches.begin(); DMiter != matches.end(); DMiter++) var += (DMiter->distance - mean) * (DMiter->distance - mean);
        cout << "# of Observed Matches to " << DBiter->name << " is " << matches.size() << " with a sd of: " << var <<  endl; 
    } /*/
    
    tm.stop();
    double matchTime = tm.getTimeMilli();
   
    
    //for finding which picture has most matches
    
    int numMatchesToDB [(int)DB.size()], bestMatchIdx=0;
    //init array
    for (int i = 0; i < (int)DB.size(); i++)  numMatchesToDB[i] = 0;
    //bin for finding which pic has most matches
    for (vector<DMatch>::iterator DMiter = matches.begin(); DMiter != matches.end(); DMiter++)
    {
        numMatchesToDB[DMiter->imgIdx]++; 
        if ( numMatchesToDB[bestMatchIdx] < numMatchesToDB[DMiter->imgIdx] ) bestMatchIdx = DMiter->imgIdx;
        //cout << "bestMatchIdx: " << numMatchesToDB[bestMatchIdx] << "\t" << numMatchesToDB[DMiter->imgIdx] << "\t" << bestMatchIdx << "\t" << DMiter->imgIdx << endl;
        //cout << "Match information (queryIDx/trainIDx/imgIDx/distance): " << 
        //    DMiter->queryIdx << "\t" << DMiter->trainIdx << "\t" << DMiter->imgIdx << "\t" << DMiter->distance << endl;
    }
    
    cout << "Match time: " << matchTime << " ms with the best match at " << DB.at(bestMatchIdx).name << " with " << numMatchesToDB[bestMatchIdx] << " matching keypoints" << endl;
    
    
    //cout << "Observed Descriptors " << imgDesc.rows << " and number of matches " << (int)matches.size() << endl;
    CV_Assert( imgDesc.rows == (int)matches.size() || matches.empty() );
    
    
    ObjName = DB.at(bestMatchIdx).name;
    
    
    //*/ Show only bestMatchIdx pic
    
    //preparing mask so not all keypoints are shown, only links where imgIdx (DB image position) 
    vector<char> mask;
    mask.resize( matches.size() );
    fill( mask.begin(), mask.end(), 0 );
    for( size_t i = 0; i < matches.size(); i++ )
    {
        if( matches[i].imgIdx == bestMatchIdx )
            mask[i] = 1;
    }
    
    
    Mat drawImg;
    drawMatches( img, imgKp, DB.at(bestMatchIdx).img, DB.at(bestMatchIdx).keypoints, matches, drawImg, Scalar(255, 0, 0), Scalar(0, 255, 255), mask ); 
    
    imshow(DB.at(bestMatchIdx).name, drawImg);
    waitKey();
    //*/
        

    /*/  Show each match by pic    
    bool running = true;
    Mat drawImg;
    vector<char> mask;
    vector<DBobj>::iterator DBiter = DB.begin();   
    for( size_t i = 0; running ;  )
    {
        maskMatchesByTrainImgIdx( matches, (int)i, mask );
        drawMatches( img, imgKp, DBiter->img, DBiter->keypoints, matches, drawImg, Scalar(255, 0, 0), Scalar(0, 255, 255), mask );
        imshow("Matchs", drawImg);
                    
        switch ( (char) waitKey(5))
        {
           case 'q': case 'Q':
               running = false; 
               break;
           case 'i': case 'I':
                //cout <<  (bool) DBiter != DB.end()  << endl;
                if (( DBiter != DB.end() ) && ( i < DB.size()-1 )) 
        	{
            	DBiter++;
            	i++;
            	}
            	else
            	{
           	DBiter = DB.begin();
            	i = 0;
            	}
                cout << DBiter->name << "\tNumber of Matches: " << numMatchesToDB[i] << endl;
                break;       
        }
    }
    //*/
}
Exemple #14
0
int main(int argc, const char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage : video_writer <input video file>" << std::endl;
        return -1;
    }

    const double FPS = 25.0;

    cv::VideoCapture reader(argv[1]);

    if (!reader.isOpened())
    {
        std::cerr << "Can't open input video file" << std::endl;
        return -1;
    }

    cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());

    cv::VideoWriter writer;
    cv::Ptr<cv::cudacodec::VideoWriter> d_writer;

    cv::Mat frame;
    cv::cuda::GpuMat d_frame;

    std::vector<double> cpu_times;
    std::vector<double> gpu_times;
    TickMeter tm;

    for (int i = 1;; ++i)
    {
        std::cout << "Read " << i << " frame" << std::endl;

        reader >> frame;

        if (frame.empty())
        {
            std::cout << "Stop" << std::endl;
            break;
        }

        if (!writer.isOpened())
        {
            std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;

            std::cout << "Open CPU Writer" << std::endl;

            if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
                return -1;
        }

        if (d_writer.empty())
        {
            std::cout << "Open CUDA Writer" << std::endl;

            const cv::String outputFilename = "output_gpu.avi";
            d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS);
        }

        d_frame.upload(frame);

        std::cout << "Write " << i << " frame" << std::endl;

        tm.reset(); tm.start();
        writer.write(frame);
        tm.stop();
        cpu_times.push_back(tm.getTimeMilli());

        tm.reset(); tm.start();
        d_writer->write(d_frame);
        tm.stop();
        gpu_times.push_back(tm.getTimeMilli());
    }

    std::cout << std::endl << "Results:" << std::endl;

    std::sort(cpu_times.begin(), cpu_times.end());
    std::sort(gpu_times.begin(), gpu_times.end());

    double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
    double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();

    std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
    std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;

    return 0;
}
/** @function main */
int main(int argc, char** argv)
{
	TickMeter tm;

	string detectorType = defaultDetectorType;
	string descriptorType = defaultDescriptorType;
	string matcherType = defaultMatcherType;
	string queryFileName = defaultQueryFileName;
	string trainFileName = defaultTrainFileName;
	
	if(argc != 1 && argc != 4 && argc != 6)
	{
		readme(argv[0]);
		return -1;
	}
	std::cout << argc << std::endl;
	if(argc != 1)
	{

		detectorType = argv[1];
		descriptorType = argv[2];
		matcherType = argv[3];
		if(argc != 4)
		{
			queryFileName = argv[4];
			trainFileName = argv[5];
		}
	}

	Mat trainImage = imread(trainFileName, CV_LOAD_IMAGE_GRAYSCALE);
	Mat queryImage = imread(queryFileName, CV_LOAD_IMAGE_GRAYSCALE);

	if(!trainImage.data || !queryImage.data)
	{
		std::cout << " --(!) Error reading images " << std::endl;
		return -1;
	}

	//Create Detector Phase
	Ptr<FeatureDetector> featureDetector;
	Ptr<DescriptorExtractor> descriptorExtractor;
	Ptr<DescriptorMatcher> descriptorMatcher;
	initModule_nonfree();
	if(!createDetectorDescriptorMatcher(detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher))
	{
		readme(argv[0]);
		return -1;
	}
	
	//get keypoints phase
	vector<KeyPoint> queryKeypoints;
	vector<KeyPoint> trainKeypoints;
	tm.start();
	detectKeypoints(queryImage, queryKeypoints, featureDetector);
	detectKeypoints(trainImage, trainKeypoints, featureDetector);
	tm.stop();
	double keypointTime = tm.getTimeMilli();

	//get descriptor phase
	Mat queryDescriptors;
	Mat trainDescriptors;
	tm.start();
	computeDescriptors(queryImage, queryKeypoints, queryDescriptors, descriptorExtractor);
	//computeDescriptors(trainImage, trainKeypoints, trainDescriptors, descriptorExtractor);
	cv::FileStorage fs2("data.xml", cv::FileStorage::READ);
	fs2["trainDescriptors"] >> trainDescriptors;
	fs2.release();
	tm.stop();
	double descriptorTime = tm.getTimeMilli();

	//matching Phase
	vector<DMatch> matches;
	tm.start();
	matchDescriptors(trainDescriptors, queryDescriptors, matches, descriptorMatcher);
	tm.stop();
	double matcherTime = tm.getTimeMilli();
	
	//show result Phase
	double max_dist = 0; double min_dist = 100;

	for( int i = 0; i < trainDescriptors.rows; i++ )
	{
		double dist = matches[i].distance;
		if( dist < min_dist ) min_dist = dist;
		if( dist > max_dist ) max_dist = dist;
	}

	vector<DMatch> good_matches;

	for( int i = 0; i < trainDescriptors.rows; i++ )
	{
		if( matches[i].distance < 3*min_dist )
		{
			good_matches.push_back( matches[i]);
		}
	}

	Mat matchesImage = showResultImages(trainImage, trainKeypoints, queryImage, queryKeypoints, matches);
	Mat goodmatchesImage = showResultImages(trainImage, trainKeypoints, queryImage, queryKeypoints, good_matches);
	namedWindow("matches", 1);
	namedWindow("good_matches", 1);
	imshow("matches", matchesImage);
	imshow("good_matches", goodmatchesImage);

	std::cout << "\n" << detectorType << " + " << descriptorType << " + " << matcherType << std::endl;
	std::cout << "detecting time : " << keypointTime << " ms" << std::endl;
	std::cout << "computing descriptor time : " << descriptorTime << " ms" << std::endl;
	std::cout << "matching time : " << matcherTime << " ms" << std::endl;
	std::cout << "total time : " << keypointTime + descriptorTime + matcherTime << " ms" << std::endl;

	cv::FileStorage fs1("data.xml", cv::FileStorage::WRITE);
	fs1 << "trainDescriptors" << trainDescriptors;
	fs1.release();

	cvWaitKey(0);

	return 0;
}