File: retina_ocl.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (119 lines) | stat: -rw-r--r-- 3,853 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <cstring>

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ocl.hpp"
#include "opencv2/bioinspired.hpp"

using namespace cv;
using namespace cv::ocl;
using namespace std;

const int total_loop_count = 50;

static void help(CommandLineParser cmd, const String& errorMessage)
{
    cout << errorMessage << endl;
    cout << "Avaible options:" << endl;
    cmd.printMessage();
}

int main(int argc, char* argv[])
{
    //set this to save kernel compile time from second time you run
    ocl::setBinaryDiskCache();
    const char* keys =
        "{ h   | help     | false           | print help message }"
        "{ c   | cpu      | false           | use cpu (original version) or gpu(OpenCL) to process the image }"
        "{ i   | image    | cat.jpg         | specify the input image }";

    CommandLineParser cmd(argc, argv, keys);

    if(cmd.get<bool>("help"))
    {
        help(cmd, "Usage: ./retina_ocl [options]");
        return EXIT_FAILURE;
    }

    String fname = cmd.get<String>("i");
    bool useCPU = cmd.get<bool>("c");

    cv::Mat input = imread(fname);
    if(input.empty())
    {
        help(cmd, "Error opening: " + fname);
        return EXIT_FAILURE;
    }
    //////////////////////////////////////////////////////////////////////////////
    // Program start in a try/catch safety context (Retina may throw errors)
    try
    {
        // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
        cv::Ptr<cv::bioinspired::Retina> oclRetina;
        cv::Ptr<cv::bioinspired::Retina> retina;
        // declare retina output buffers
        cv::ocl::oclMat retina_parvo_ocl;
        cv::ocl::oclMat retina_magno_ocl;
        cv::Mat retina_parvo;
        cv::Mat retina_magno;

        if(useCPU)
        {
            retina = cv::bioinspired::createRetina(input.size());
            retina->clearBuffers();
        }
        else
        {
            oclRetina = cv::bioinspired::createRetina_OCL(input.size());
            oclRetina->clearBuffers();
        }

        int64 temp_time = 0, total_time = 0;

        int loop_counter = 0;
        for(; loop_counter <= total_loop_count; ++loop_counter)
        {
            if(useCPU)
            {
                temp_time = cv::getTickCount();
                retina->run(input);
                retina->getParvo(retina_parvo);
                retina->getMagno(retina_magno);
            }
            else
            {
                cv::ocl::oclMat input_ocl(input);
                temp_time = cv::getTickCount();
                oclRetina->run(input_ocl);
                oclRetina->getParvo(retina_parvo_ocl);
                oclRetina->getMagno(retina_magno_ocl);
            }
            // will not count the first loop, which is considered as warm-up period
            if(loop_counter > 0)
            {
                temp_time = (cv::getTickCount() - temp_time);
                total_time += temp_time;
                printf("Frame id %2d: %3.4fms\n", loop_counter, (double)temp_time / cv::getTickFrequency() * 1000.0);
            }
            if(!useCPU)
            {
                retina_parvo = retina_parvo_ocl;
                retina_magno = retina_magno_ocl;
            }
            cv::imshow("retina input", input);
            cv::imshow("Retina Parvo", retina_parvo);
            cv::imshow("Retina Magno", retina_magno);
            cv::waitKey(10);
        }
        printf("Average: %.4fms\n", (double)total_time / total_loop_count / cv::getTickFrequency() * 1000.0);
    }
    catch(const cv::Exception& e)
    {
        std::cerr << "Error using Retina : " << e.what() << std::endl;
    }
    // Program end message
    std::cout << "Retina demo end" << std::endl;
    return EXIT_SUCCESS;
}