main.cpp 15 KB
Newer Older
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
43
#include <cstdio>
44

Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
45
#include "cvconfig.h"
46 47 48 49

#include "opencv2/ts/ts.hpp"
#include "opencv2/ts/gpu_perf.hpp"

Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
50 51 52 53 54 55
#include "opencv2/core/core.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/legacy/legacy.hpp"

56 57 58 59
static const char * impls[] = {
    "cuda",
    "plain"
};
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
60

61
CV_PERF_TEST_MAIN_WITH_IMPLS(gpu_perf4au, impls, perf::printCudaInfo())
Vladislav Vinogradov's avatar
Vladislav Vinogradov committed
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

//////////////////////////////////////////////////////////
// HoughLinesP

DEF_PARAM_TEST_1(Image, std::string);

PERF_TEST_P(Image, HoughLinesP, testing::Values(std::string("im1_1280x800.jpg")))
{
    declare.time(30.0);

    std::string fileName = GetParam();

    const float rho = 1.f;
    const float theta = 1.f;
    const int threshold = 40;
    const int minLineLenght = 20;
    const int maxLineGap = 5;

    cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);

    if (PERF_RUN_GPU())
    {
        cv::gpu::GpuMat d_image(image);
        cv::gpu::GpuMat d_lines;
        cv::gpu::HoughLinesBuf d_buf;

        cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);

        TEST_CYCLE()
        {
            cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
        }
    }
    else
    {
        cv::Mat mask;
        cv::Canny(image, mask, 50, 100);

        std::vector<cv::Vec4i> lines;
        cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);

        TEST_CYCLE()
        {
            cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
        }
    }

    SANITY_CHECK(0);
}

//////////////////////////////////////////////////////////
// GoodFeaturesToTrack

DEF_PARAM_TEST(Image_Depth, std::string, perf::MatDepth);

PERF_TEST_P(Image_Depth, GoodFeaturesToTrack,
                testing::Combine(
                testing::Values(std::string("im1_1280x800.jpg")),
                testing::Values(CV_8U, CV_16U)
                ))
{
    declare.time(60);

    const std::string fileName = std::tr1::get<0>(GetParam());
    const int depth = std::tr1::get<1>(GetParam());

    const int maxCorners = 5000;
    const double qualityLevel = 0.05;
    const int minDistance = 5;
    const int blockSize = 3;
    const bool useHarrisDetector = true;
    const double k = 0.05;

    cv::Mat src = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
    if (src.empty())
        FAIL() << "Unable to load source image [" << fileName << "]";

    if (depth != CV_8U)
        src.convertTo(src, depth);

    cv::Mat mask(src.size(), CV_8UC1, cv::Scalar::all(1));
    mask(cv::Rect(0, 0, 100, 100)).setTo(cv::Scalar::all(0));

    if (PERF_RUN_GPU())
    {
        cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(maxCorners, qualityLevel, minDistance, blockSize, useHarrisDetector, k);

        cv::gpu::GpuMat d_src(src);
        cv::gpu::GpuMat d_mask(mask);
        cv::gpu::GpuMat d_pts;

        d_detector(d_src, d_pts, d_mask);

        TEST_CYCLE()
        {
            d_detector(d_src, d_pts, d_mask);
        }
    }
    else
    {
        if (depth != CV_8U)
            FAIL() << "Unsupported depth";

        cv::Mat pts;

        cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);

        TEST_CYCLE()
        {
            cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
        }
    }

    SANITY_CHECK(0);
}

//////////////////////////////////////////////////////////
// OpticalFlowPyrLKSparse

typedef std::pair<std::string, std::string> string_pair;

DEF_PARAM_TEST(ImagePair_Depth_GraySource, string_pair, perf::MatDepth, bool);

PERF_TEST_P(ImagePair_Depth_GraySource, OpticalFlowPyrLKSparse,
                testing::Combine(
                    testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
                    testing::Values(CV_8U, CV_16U),
                    testing::Bool()
                    ))
{
    declare.time(60);

    const string_pair fileNames = std::tr1::get<0>(GetParam());
    const int depth = std::tr1::get<1>(GetParam());
    const bool graySource = std::tr1::get<2>(GetParam());

    // PyrLK params
    const cv::Size winSize(15, 15);
    const int maxLevel = 5;
    const cv::TermCriteria criteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01);

    // GoodFeaturesToTrack params
    const int maxCorners = 5000;
    const double qualityLevel = 0.05;
    const int minDistance = 5;
    const int blockSize = 3;
    const bool useHarrisDetector = true;
    const double k = 0.05;

    cv::Mat src1 = cv::imread(fileNames.first, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
    if (src1.empty())
        FAIL() << "Unable to load source image [" << fileNames.first << "]";

    cv::Mat src2 = cv::imread(fileNames.second, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
    if (src2.empty())
        FAIL() << "Unable to load source image [" << fileNames.second << "]";

    cv::Mat gray_src;
    if (graySource)
        gray_src = src1;
    else
        cv::cvtColor(src1, gray_src, cv::COLOR_BGR2GRAY);

    cv::Mat pts;
    cv::goodFeaturesToTrack(gray_src, pts, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);

    if (depth != CV_8U)
    {
        src1.convertTo(src1, depth);
        src2.convertTo(src2, depth);
    }

    if (PERF_RUN_GPU())
    {
        cv::gpu::GpuMat d_src1(src1);
        cv::gpu::GpuMat d_src2(src2);
        cv::gpu::GpuMat d_pts(pts.reshape(2, 1));
        cv::gpu::GpuMat d_nextPts;
        cv::gpu::GpuMat d_status;

        cv::gpu::PyrLKOpticalFlow d_pyrLK;
        d_pyrLK.winSize = winSize;
        d_pyrLK.maxLevel = maxLevel;
        d_pyrLK.iters = criteria.maxCount;
        d_pyrLK.useInitialFlow = false;

        d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);

        TEST_CYCLE()
        {
            d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
        }
    }
    else
    {
        if (depth != CV_8U)
            FAIL() << "Unsupported depth";

        cv::Mat nextPts;
        cv::Mat status;

        cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);

        TEST_CYCLE()
        {
            cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
        }
    }

    SANITY_CHECK(0);
}

//////////////////////////////////////////////////////////
// OpticalFlowFarneback

DEF_PARAM_TEST(ImagePair_Depth, string_pair, perf::MatDepth);

PERF_TEST_P(ImagePair_Depth, OpticalFlowFarneback,
                testing::Combine(
                    testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
                    testing::Values(CV_8U, CV_16U)
                    ))
{
    declare.time(500);

    const string_pair fileNames = std::tr1::get<0>(GetParam());
    const int depth = std::tr1::get<1>(GetParam());

    const double pyrScale = 0.5;
    const int numLevels = 6;
    const int winSize = 7;
    const int numIters = 15;
    const int polyN = 7;
    const double polySigma = 1.5;
    const int flags = cv::OPTFLOW_USE_INITIAL_FLOW;

    cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
    if (src1.empty())
        FAIL() << "Unable to load source image [" << fileNames.first << "]";

    cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
    if (src2.empty())
        FAIL() << "Unable to load source image [" << fileNames.second << "]";

    if (depth != CV_8U)
    {
        src1.convertTo(src1, depth);
        src2.convertTo(src2, depth);
    }

    if (PERF_RUN_GPU())
    {
        cv::gpu::GpuMat d_src1(src1);
        cv::gpu::GpuMat d_src2(src2);
        cv::gpu::GpuMat d_u(src1.size(), CV_32FC1, cv::Scalar::all(0));
        cv::gpu::GpuMat d_v(src1.size(), CV_32FC1, cv::Scalar::all(0));

        cv::gpu::FarnebackOpticalFlow d_farneback;
        d_farneback.pyrScale = pyrScale;
        d_farneback.numLevels = numLevels;
        d_farneback.winSize = winSize;
        d_farneback.numIters = numIters;
        d_farneback.polyN = polyN;
        d_farneback.polySigma = polySigma;
        d_farneback.flags = flags;

        d_farneback(d_src1, d_src2, d_u, d_v);

        TEST_CYCLE_N(10)
        {
            d_farneback(d_src1, d_src2, d_u, d_v);
        }
    }
    else
    {
        if (depth != CV_8U)
            FAIL() << "Unsupported depth";

        cv::Mat flow(src1.size(), CV_32FC2, cv::Scalar::all(0));

        cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);

        TEST_CYCLE_N(10)
        {
            cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
        }
    }

    SANITY_CHECK(0);
}

//////////////////////////////////////////////////////////
// OpticalFlowBM

void calcOpticalFlowBM(const cv::Mat& prev, const cv::Mat& curr,
                       cv::Size bSize, cv::Size shiftSize, cv::Size maxRange, int usePrevious,
                       cv::Mat& velx, cv::Mat& vely)
{
    cv::Size sz((curr.cols - bSize.width + shiftSize.width)/shiftSize.width, (curr.rows - bSize.height + shiftSize.height)/shiftSize.height);

    velx.create(sz, CV_32FC1);
    vely.create(sz, CV_32FC1);

    CvMat cvprev = prev;
    CvMat cvcurr = curr;

    CvMat cvvelx = velx;
    CvMat cvvely = vely;

    cvCalcOpticalFlowBM(&cvprev, &cvcurr, bSize, shiftSize, maxRange, usePrevious, &cvvelx, &cvvely);
}

DEF_PARAM_TEST(ImagePair_BlockSize_ShiftSize_MaxRange, string_pair, cv::Size, cv::Size, cv::Size);

PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, OpticalFlowBM,
                testing::Combine(
                    testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
                    testing::Values(cv::Size(16, 16)),
                    testing::Values(cv::Size(2, 2)),
                    testing::Values(cv::Size(16, 16))
                    ))
{
    declare.time(3000);

    const string_pair fileNames = std::tr1::get<0>(GetParam());
    const cv::Size block_size = std::tr1::get<1>(GetParam());
    const cv::Size shift_size = std::tr1::get<2>(GetParam());
    const cv::Size max_range = std::tr1::get<3>(GetParam());

    cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
    if (src1.empty())
        FAIL() << "Unable to load source image [" << fileNames.first << "]";

    cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
    if (src2.empty())
        FAIL() << "Unable to load source image [" << fileNames.second << "]";

    if (PERF_RUN_GPU())
    {
        cv::gpu::GpuMat d_src1(src1);
        cv::gpu::GpuMat d_src2(src2);
        cv::gpu::GpuMat d_velx, d_vely, buf;

        cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);

        TEST_CYCLE_N(10)
        {
            cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
        }
    }
    else
    {
        cv::Mat velx, vely;

        calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);

        TEST_CYCLE_N(10)
        {
            calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
        }
    }

    SANITY_CHECK(0);
}

PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, FastOpticalFlowBM,
                testing::Combine(
                    testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
                    testing::Values(cv::Size(16, 16)),
                    testing::Values(cv::Size(1, 1)),
                    testing::Values(cv::Size(16, 16))
                    ))
{
    declare.time(3000);

    const string_pair fileNames = std::tr1::get<0>(GetParam());
    const cv::Size block_size = std::tr1::get<1>(GetParam());
    const cv::Size shift_size = std::tr1::get<2>(GetParam());
    const cv::Size max_range = std::tr1::get<3>(GetParam());

    cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
    if (src1.empty())
        FAIL() << "Unable to load source image [" << fileNames.first << "]";

    cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
    if (src2.empty())
        FAIL() << "Unable to load source image [" << fileNames.second << "]";

    if (PERF_RUN_GPU())
    {
        cv::gpu::GpuMat d_src1(src1);
        cv::gpu::GpuMat d_src2(src2);
        cv::gpu::GpuMat d_velx, d_vely;

        cv::gpu::FastOpticalFlowBM fastBM;

        fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);

        TEST_CYCLE_N(10)
        {
            fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
        }
    }
    else
    {
        cv::Mat velx, vely;

        calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);

        TEST_CYCLE_N(10)
        {
            calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
        }
    }

    SANITY_CHECK(0);
}