Ask Your Question

Revision history [back]

descriptorMatcher (BruteForce-Hamming) with OpenCL

Good Day!

The program works(OpenCV 3), but uses only the CPU. How to use GPU(The system determines the graphics card)?C:\fakepath\2017-08-20_17-13-57.png

ocl::setUseOpenCL(true);
if (!ocl::haveOpenCL())
{
    cout << "OpenCL is not available..." << endl;
    //return;
}

cv::ocl::Context context;
if (!context.create(cv::ocl::Device::TYPE_GPU))
{
    cout << "Failed creating the context..." << endl;
    //return;
}

cout << context.ndevices() << " GPU devices are detected." << endl; //This bit provides an overview of the OpenCL devices you have in your computer
for (int i = 0; i < context.ndevices(); i++)
{
    int tmp = 0;
    if (cv::ocl::useOpenCL() == true) tmp = 1;

    cv::ocl::Device device = context.device(i);
    cout << "name:              " << device.name() << endl;
    cout << "available:         " << device.available() << endl;
    cout << "imageSupport:      " << device.imageSupport() << endl;
    cout << "OpenCL_C_Version:  " << device.OpenCL_C_Version() << endl;
    cout << "Use OpenCL:  " <<  tmp << endl;
    cout << endl;
}
//UMat img1 = imread("F:\\Project\\Data\\X.jpg", IMREAD_GRAYSCALE).getUMat(ACCESS_READ);
Mat img1 = imread("F:\\Project\\Data\\X.jpg", IMREAD_GRAYSCALE);

float fTimeStartAll = clock() / (float)CLOCKS_PER_SEC;

vector<double> desMethCmp;
Ptr<Feature2D> b = BRISK::create();

Ptr<DescriptorMatcher> descriptorMatcher;
// Match between img1 and img2
vector<DMatch> matches;
// keypoint  for img1 and img2
vector<KeyPoint> keyImg1, keyImg2;
// Descriptor for img1 and img2
Mat descImg1, descImg2;


// We can detect keypoint with detect method
b->detect(img1, keyImg1, Mat());
// and compute their descriptors with method  compute
b->compute(img1, keyImg1, descImg1);

int tmp = 0;
sprintf(pathCatalog, "F:\\Project\\");
numberPage = 509;


    tmp = 0;
    int bestList = 0;
    double bestCurrent = 10000000;

    for (int j = 1; j < numberPage; j++)
    {
        tmp++;
        char pathFile[256];
        sprintf(pathFile, "%s%d.mat", pathCatalog, j);
        LoadMatBinary(pathFile, descImg2);

        // Match method loop
        if (b->descriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR)
        {
            printf("It's strange. You should use Hamming distance only for a binary descriptor");
        };

        UMat UdescImg1 = descImg1.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);
        UMat UdescImg2 = descImg2.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);


        descriptorMatcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
        descriptorMatcher->match(UdescImg1, UdescImg2, matches, noArray());
click to hide/show revision 2
No.2 Revision

updated 2017-08-21 08:39:52 -0600

berak gravatar image

descriptorMatcher (BruteForce-Hamming) with OpenCL

Good Day!

The program works(OpenCV 3), but uses only the CPU. How to use GPU(The system determines the graphics card)?C:\fakepath\2017-08-20_17-13-57.pngcard)? C:\fakepath\2017-08-20_17-13-57.png

ocl::setUseOpenCL(true);
if (!ocl::haveOpenCL())
{
    cout << "OpenCL is not available..." << endl;
    //return;
}

cv::ocl::Context context;
if (!context.create(cv::ocl::Device::TYPE_GPU))
{
    cout << "Failed creating the context..." << endl;
    //return;
}

cout << context.ndevices() << " GPU devices are detected." << endl; //This bit provides an overview of the OpenCL devices you have in your computer
for (int i = 0; i < context.ndevices(); i++)
{
    int tmp = 0;
    if (cv::ocl::useOpenCL() == true) tmp = 1;

    cv::ocl::Device device = context.device(i);
    cout << "name:              " << device.name() << endl;
    cout << "available:         " << device.available() << endl;
    cout << "imageSupport:      " << device.imageSupport() << endl;
    cout << "OpenCL_C_Version:  " << device.OpenCL_C_Version() << endl;
    cout << "Use OpenCL:  " <<  tmp << endl;
    cout << endl;
}
//UMat img1 = imread("F:\\Project\\Data\\X.jpg", IMREAD_GRAYSCALE).getUMat(ACCESS_READ);
Mat img1 = imread("F:\\Project\\Data\\X.jpg", IMREAD_GRAYSCALE);

float fTimeStartAll = clock() / (float)CLOCKS_PER_SEC;

vector<double> desMethCmp;
Ptr<Feature2D> b = BRISK::create();

Ptr<DescriptorMatcher> descriptorMatcher;
// Match between img1 and img2
vector<DMatch> matches;
// keypoint  for img1 and img2
vector<KeyPoint> keyImg1, keyImg2;
// Descriptor for img1 and img2
Mat descImg1, descImg2;


// We can detect keypoint with detect method
b->detect(img1, keyImg1, Mat());
// and compute their descriptors with method  compute
b->compute(img1, keyImg1, descImg1);

int tmp = 0;
sprintf(pathCatalog, "F:\\Project\\");
numberPage = 509;


    tmp = 0;
    int bestList = 0;
    double bestCurrent = 10000000;

    for (int j = 1; j < numberPage; j++)
    {
        tmp++;
        char pathFile[256];
        sprintf(pathFile, "%s%d.mat", pathCatalog, j);
        LoadMatBinary(pathFile, descImg2);

        // Match method loop
        if (b->descriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR)
        {
            printf("It's strange. You should use Hamming distance only for a binary descriptor");
        };

        UMat UdescImg1 = descImg1.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);
        UMat UdescImg2 = descImg2.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);


        descriptorMatcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
        descriptorMatcher->match(UdescImg1, UdescImg2, matches, noArray());