2012-10-17 07:18:30 +08:00
|
|
|
/*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.
|
|
|
|
//
|
|
|
|
//
|
2013-03-21 17:31:51 +08:00
|
|
|
// License Agreement
|
2012-10-17 07:18:30 +08:00
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
2013-03-21 17:31:51 +08:00
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
2012-10-17 07:18:30 +08:00
|
|
|
// 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.
|
|
|
|
//
|
2013-03-21 17:31:51 +08:00
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
2012-10-17 07:18:30 +08:00
|
|
|
// 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*/
|
|
|
|
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_CUDA
|
|
|
|
|
2013-03-15 18:09:10 +08:00
|
|
|
using namespace cvtest;
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
//#define DUMP
|
|
|
|
|
2015-01-14 23:18:51 +08:00
|
|
|
struct HOG : testing::TestWithParam<cv::cuda::DeviceInfo>
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::DeviceInfo devInfo;
|
2015-01-14 23:18:51 +08:00
|
|
|
cv::Ptr<cv::cuda::HOG> hog;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#ifdef DUMP
|
|
|
|
std::ofstream f;
|
|
|
|
#else
|
|
|
|
std::ifstream f;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int wins_per_img_x;
|
|
|
|
int wins_per_img_y;
|
|
|
|
int blocks_per_win_x;
|
|
|
|
int blocks_per_win_y;
|
|
|
|
int block_hist_size;
|
|
|
|
|
|
|
|
virtual void SetUp()
|
|
|
|
{
|
|
|
|
devInfo = GetParam();
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::setDevice(devInfo.deviceID());
|
2015-01-14 23:18:51 +08:00
|
|
|
|
|
|
|
hog = cv::cuda::HOG::create();
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DUMP
|
2015-01-14 23:18:51 +08:00
|
|
|
void dump(const std::vector<cv::Point>& locations)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
int nlocations = locations.size();
|
|
|
|
f.write((char*)&nlocations, sizeof(nlocations));
|
|
|
|
|
|
|
|
for (int i = 0; i < locations.size(); ++i)
|
|
|
|
f.write((char*)&locations[i], sizeof(locations[i]));
|
|
|
|
}
|
|
|
|
#else
|
2015-01-14 23:18:51 +08:00
|
|
|
void compare(const std::vector<cv::Point>& locations)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2015-01-14 23:18:51 +08:00
|
|
|
// skip block_hists check
|
2012-10-17 07:18:30 +08:00
|
|
|
int rows, cols;
|
|
|
|
f.read((char*)&rows, sizeof(rows));
|
|
|
|
f.read((char*)&cols, sizeof(cols));
|
2015-01-14 23:18:51 +08:00
|
|
|
for (int i = 0; i < rows; ++i)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2015-01-14 23:18:51 +08:00
|
|
|
for (int j = 0; j < cols; ++j)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
float val;
|
|
|
|
f.read((char*)&val, sizeof(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nlocations;
|
|
|
|
f.read((char*)&nlocations, sizeof(nlocations));
|
|
|
|
ASSERT_EQ(nlocations, static_cast<int>(locations.size()));
|
|
|
|
|
|
|
|
for (int i = 0; i < nlocations; ++i)
|
|
|
|
{
|
|
|
|
cv::Point location;
|
|
|
|
f.read((char*)&location, sizeof(location));
|
|
|
|
ASSERT_EQ(location, locations[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void testDetect(const cv::Mat& img)
|
|
|
|
{
|
2015-01-14 23:18:51 +08:00
|
|
|
hog->setGammaCorrection(false);
|
|
|
|
hog->setSVMDetector(hog->getDefaultPeopleDetector());
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
std::vector<cv::Point> locations;
|
|
|
|
|
|
|
|
// Test detect
|
2015-01-14 23:18:51 +08:00
|
|
|
hog->detect(loadMat(img), locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#ifdef DUMP
|
2015-01-14 23:18:51 +08:00
|
|
|
dump(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#else
|
2015-01-14 23:18:51 +08:00
|
|
|
compare(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Test detect on smaller image
|
|
|
|
cv::Mat img2;
|
|
|
|
cv::resize(img, img2, cv::Size(img.cols / 2, img.rows / 2));
|
2015-01-14 23:18:51 +08:00
|
|
|
hog->detect(loadMat(img2), locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#ifdef DUMP
|
2015-01-14 23:18:51 +08:00
|
|
|
dump(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#else
|
2015-01-14 23:18:51 +08:00
|
|
|
compare(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Test detect on greater image
|
|
|
|
cv::resize(img, img2, cv::Size(img.cols * 2, img.rows * 2));
|
2015-01-14 23:18:51 +08:00
|
|
|
hog->detect(loadMat(img2), locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#ifdef DUMP
|
2015-01-14 23:18:51 +08:00
|
|
|
dump(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#else
|
2015-01-14 23:18:51 +08:00
|
|
|
compare(locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// desabled while resize does not fixed
|
2013-12-30 21:30:59 +08:00
|
|
|
CUDA_TEST_P(HOG, DISABLED_Detect)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
cv::Mat img_rgb = readImage("hog/road.png");
|
|
|
|
ASSERT_FALSE(img_rgb.empty());
|
|
|
|
|
|
|
|
f.open((std::string(cvtest::TS::ptr()->get_data_path()) + "hog/expected_output.bin").c_str(), std::ios_base::binary);
|
|
|
|
ASSERT_TRUE(f.is_open());
|
|
|
|
|
|
|
|
// Test on color image
|
|
|
|
cv::Mat img;
|
2013-04-17 19:24:53 +08:00
|
|
|
cv::cvtColor(img_rgb, img, cv::COLOR_BGR2BGRA);
|
2012-10-17 07:18:30 +08:00
|
|
|
testDetect(img);
|
|
|
|
|
|
|
|
// Test on gray image
|
2013-04-17 19:24:53 +08:00
|
|
|
cv::cvtColor(img_rgb, img, cv::COLOR_BGR2GRAY);
|
2012-10-17 07:18:30 +08:00
|
|
|
testDetect(img);
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
CUDA_TEST_P(HOG, GetDescriptors)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
// Load image (e.g. train data, composed from windows)
|
|
|
|
cv::Mat img_rgb = readImage("hog/train_data.png");
|
|
|
|
ASSERT_FALSE(img_rgb.empty());
|
|
|
|
|
|
|
|
// Convert to C4
|
|
|
|
cv::Mat img;
|
2013-04-17 19:24:53 +08:00
|
|
|
cv::cvtColor(img_rgb, img, cv::COLOR_BGR2BGRA);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::GpuMat d_img(img);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
// Convert train images into feature vectors (train table)
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::GpuMat descriptors, descriptors_by_cols;
|
2015-01-14 23:18:51 +08:00
|
|
|
|
|
|
|
hog->setWinStride(Size(64, 128));
|
|
|
|
|
|
|
|
hog->setDescriptorFormat(cv::cuda::HOG::DESCR_FORMAT_ROW_BY_ROW);
|
|
|
|
hog->compute(d_img, descriptors);
|
|
|
|
|
|
|
|
hog->setDescriptorFormat(cv::cuda::HOG::DESCR_FORMAT_COL_BY_COL);
|
|
|
|
hog->compute(d_img, descriptors_by_cols);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
// Check size of the result train table
|
|
|
|
wins_per_img_x = 3;
|
|
|
|
wins_per_img_y = 2;
|
|
|
|
blocks_per_win_x = 7;
|
|
|
|
blocks_per_win_y = 15;
|
|
|
|
block_hist_size = 36;
|
|
|
|
cv::Size descr_size_expected = cv::Size(blocks_per_win_x * blocks_per_win_y * block_hist_size,
|
|
|
|
wins_per_img_x * wins_per_img_y);
|
|
|
|
ASSERT_EQ(descr_size_expected, descriptors.size());
|
|
|
|
|
|
|
|
// Check both formats of output descriptors are handled correctly
|
|
|
|
cv::Mat dr(descriptors);
|
|
|
|
cv::Mat dc(descriptors_by_cols);
|
|
|
|
for (int i = 0; i < wins_per_img_x * wins_per_img_y; ++i)
|
|
|
|
{
|
|
|
|
const float* l = dr.rowRange(i, i + 1).ptr<float>();
|
|
|
|
const float* r = dc.rowRange(i, i + 1).ptr<float>();
|
|
|
|
for (int y = 0; y < blocks_per_win_y; ++y)
|
|
|
|
for (int x = 0; x < blocks_per_win_x; ++x)
|
|
|
|
for (int k = 0; k < block_hist_size; ++k)
|
|
|
|
ASSERT_EQ(l[(y * blocks_per_win_x + x) * block_hist_size + k],
|
|
|
|
r[(x * blocks_per_win_y + y) * block_hist_size + k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
INSTANTIATE_TEST_CASE_P(CUDA_ObjDetect, HOG, ALL_DEVICES);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
//============== caltech hog tests =====================//
|
2013-01-23 19:31:10 +08:00
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
struct CalTech : public ::testing::TestWithParam<std::tr1::tuple<cv::cuda::DeviceInfo, std::string> >
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::DeviceInfo devInfo;
|
2012-10-17 07:18:30 +08:00
|
|
|
cv::Mat img;
|
|
|
|
|
|
|
|
virtual void SetUp()
|
|
|
|
{
|
|
|
|
devInfo = GET_PARAM(0);
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::setDevice(devInfo.deviceID());
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
img = readImage(GET_PARAM(1), cv::IMREAD_GRAYSCALE);
|
|
|
|
ASSERT_FALSE(img.empty());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
CUDA_TEST_P(CalTech, HOG)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::GpuMat d_img(img);
|
2012-10-17 07:18:30 +08:00
|
|
|
cv::Mat markedImage(img.clone());
|
|
|
|
|
2015-01-14 23:18:51 +08:00
|
|
|
cv::Ptr<cv::cuda::HOG> d_hog = cv::cuda::HOG::create();
|
|
|
|
d_hog->setSVMDetector(d_hog->getDefaultPeopleDetector());
|
|
|
|
d_hog->setNumLevels(d_hog->getNumLevels() + 32);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
std::vector<cv::Rect> found_locations;
|
2015-01-14 23:18:51 +08:00
|
|
|
d_hog->detectMultiScale(d_img, found_locations);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#if defined (LOG_CASCADE_STATISTIC)
|
|
|
|
for (int i = 0; i < (int)found_locations.size(); i++)
|
|
|
|
{
|
|
|
|
cv::Rect r = found_locations[i];
|
|
|
|
|
|
|
|
std::cout << r.x << " " << r.y << " " << r.width << " " << r.height << std::endl;
|
|
|
|
cv::rectangle(markedImage, r , CV_RGB(255, 0, 0));
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:18:51 +08:00
|
|
|
cv::imshow("Res", markedImage);
|
|
|
|
cv::waitKey();
|
2012-10-17 07:18:30 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(detect, CalTech, testing::Combine(ALL_DEVICES,
|
|
|
|
::testing::Values<std::string>("caltech/image_00000009_0.png", "caltech/image_00000032_0.png",
|
|
|
|
"caltech/image_00000165_0.png", "caltech/image_00000261_0.png", "caltech/image_00000469_0.png",
|
|
|
|
"caltech/image_00000527_0.png", "caltech/image_00000574_0.png")));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// LBP classifier
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
PARAM_TEST_CASE(LBP_Read_classifier, cv::cuda::DeviceInfo, int)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::DeviceInfo devInfo;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
virtual void SetUp()
|
|
|
|
{
|
|
|
|
devInfo = GET_PARAM(0);
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::setDevice(devInfo.deviceID());
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
CUDA_TEST_P(LBP_Read_classifier, Accuracy)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
std::string classifierXmlPath = std::string(cvtest::TS::ptr()->get_data_path()) + "lbpcascade/lbpcascade_frontalface.xml";
|
2015-01-15 00:48:58 +08:00
|
|
|
|
|
|
|
cv::Ptr<cv::cuda::CascadeClassifier> d_cascade;
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(
|
|
|
|
d_cascade = cv::cuda::CascadeClassifier::create(classifierXmlPath);
|
|
|
|
);
|
|
|
|
|
|
|
|
ASSERT_FALSE(d_cascade.empty());
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
INSTANTIATE_TEST_CASE_P(CUDA_ObjDetect, LBP_Read_classifier,
|
2012-10-17 07:18:30 +08:00
|
|
|
testing::Combine(ALL_DEVICES, testing::Values<int>(0)));
|
|
|
|
|
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
PARAM_TEST_CASE(LBP_classify, cv::cuda::DeviceInfo, int)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::DeviceInfo devInfo;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
virtual void SetUp()
|
|
|
|
{
|
|
|
|
devInfo = GET_PARAM(0);
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::setDevice(devInfo.deviceID());
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
CUDA_TEST_P(LBP_classify, Accuracy)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
std::string classifierXmlPath = std::string(cvtest::TS::ptr()->get_data_path()) + "lbpcascade/lbpcascade_frontalface.xml";
|
|
|
|
std::string imagePath = std::string(cvtest::TS::ptr()->get_data_path()) + "lbpcascade/er.png";
|
|
|
|
|
|
|
|
cv::CascadeClassifier cpuClassifier(classifierXmlPath);
|
|
|
|
ASSERT_FALSE(cpuClassifier.empty());
|
|
|
|
|
|
|
|
cv::Mat image = cv::imread(imagePath);
|
|
|
|
image = image.colRange(0, image.cols/2);
|
|
|
|
cv::Mat grey;
|
2013-04-17 19:24:53 +08:00
|
|
|
cvtColor(image, grey, cv::COLOR_BGR2GRAY);
|
2012-10-17 07:18:30 +08:00
|
|
|
ASSERT_FALSE(image.empty());
|
|
|
|
|
|
|
|
std::vector<cv::Rect> rects;
|
|
|
|
cpuClassifier.detectMultiScale(grey, rects);
|
|
|
|
cv::Mat markedImage = image.clone();
|
|
|
|
|
|
|
|
std::vector<cv::Rect>::iterator it = rects.begin();
|
|
|
|
for (; it != rects.end(); ++it)
|
2013-04-17 19:24:53 +08:00
|
|
|
cv::rectangle(markedImage, *it, cv::Scalar(255, 0, 0));
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2015-01-15 00:48:58 +08:00
|
|
|
cv::Ptr<cv::cuda::CascadeClassifier> gpuClassifier =
|
|
|
|
cv::cuda::CascadeClassifier::create(classifierXmlPath);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-08-28 19:45:13 +08:00
|
|
|
cv::cuda::GpuMat tested(grey);
|
2015-01-15 00:48:58 +08:00
|
|
|
cv::cuda::GpuMat gpu_rects_buf;
|
|
|
|
gpuClassifier->detectMultiScale(tested, gpu_rects_buf);
|
|
|
|
|
|
|
|
std::vector<cv::Rect> gpu_rects;
|
|
|
|
gpuClassifier->convert(gpu_rects_buf, gpu_rects);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#if defined (LOG_CASCADE_STATISTIC)
|
2015-01-15 00:48:58 +08:00
|
|
|
for (size_t i = 0; i < gpu_rects.size(); i++)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2015-01-15 00:48:58 +08:00
|
|
|
cv::Rect r = gpu_rects[i];
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
std::cout << r.x << " " << r.y << " " << r.width << " " << r.height << std::endl;
|
|
|
|
cv::rectangle(markedImage, r , CV_RGB(255, 0, 0));
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:48:58 +08:00
|
|
|
cv::imshow("Res", markedImage);
|
|
|
|
cv::waitKey();
|
2012-10-17 07:18:30 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:55:18 +08:00
|
|
|
INSTANTIATE_TEST_CASE_P(CUDA_ObjDetect, LBP_classify,
|
2012-10-17 07:18:30 +08:00
|
|
|
testing::Combine(ALL_DEVICES, testing::Values<int>(0)));
|
|
|
|
|
|
|
|
#endif // HAVE_CUDA
|