meanshift.cpp 8.49 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 43 44 45 46
/*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.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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*/

#include "gputest.hpp"
#include <iostream>
#include <string>


Anatoly Baksheev's avatar
Anatoly Baksheev committed
47
struct CV_GpuMeanShiftTest : public CvTest
48
{
Anatoly Baksheev's avatar
Anatoly Baksheev committed
49
    CV_GpuMeanShiftTest(): CvTest( "GPU-MeanShift", "MeanShift" ){}
50

Anatoly Baksheev's avatar
Anatoly Baksheev committed
51
    void run(int)
52
    {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
53 54
        int spatialRad = 30;
        int colorRad = 30;
55

Anatoly Baksheev's avatar
Anatoly Baksheev committed
56
        cv::Mat img = cv::imread(std::string(ts->get_data_path()) + "meanshift/cones.png");
57
        cv::Mat img_template;       
58
        
59
        if (cv::gpu::TargetArchs::hasEqualOrGreater(2, 0) && cv::gpu::DeviceInfo().major() >= 2)
60
            img_template = cv::imread(std::string(ts->get_data_path()) + "meanshift/con_result.png");
61 62
        else
            img_template = cv::imread(std::string(ts->get_data_path()) + "meanshift/con_result_CC1X.png");
63

Anatoly Baksheev's avatar
Anatoly Baksheev committed
64
        if (img.empty() || img_template.empty())
65
        {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
66
            ts->set_failed_test_info(CvTS::FAIL_MISSING_TEST_DATA);
67 68 69
            return;
        }

Anatoly Baksheev's avatar
Anatoly Baksheev committed
70 71
        cv::Mat rgba;
        cvtColor(img, rgba, CV_BGR2BGRA);
72

Anatoly Baksheev's avatar
Anatoly Baksheev committed
73
        try
74
        {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
75 76
            cv::gpu::GpuMat res;
            cv::gpu::meanShiftFiltering( cv::gpu::GpuMat(rgba), res, spatialRad, colorRad );
77

Anatoly Baksheev's avatar
Anatoly Baksheev committed
78 79 80 81 82
            if (res.type() != CV_8UC4)
            {
                ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);
                return;
            }
83

Anatoly Baksheev's avatar
Anatoly Baksheev committed
84 85 86 87 88
            cv::Mat result;
            res.download(result);

            uchar maxDiff = 0;
            for (int j = 0; j < result.rows; ++j)
89
            {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
90 91 92 93
                const uchar* res_line = result.ptr<uchar>(j);
                const uchar* ref_line = img_template.ptr<uchar>(j);

                for (int i = 0; i < result.cols; ++i)
94
                {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
95 96 97 98 99 100 101 102
                    for (int k = 0; k < 3; ++k)
                    {
                        const uchar& ch1 = res_line[result.channels()*i + k];
                        const uchar& ch2 = ref_line[img_template.channels()*i + k];
                        uchar diff = static_cast<uchar>(abs(ch1 - ch2));
                        if (maxDiff < diff)
                            maxDiff = diff;
                    }
103 104
                }
            }
Anatoly Baksheev's avatar
Anatoly Baksheev committed
105 106 107 108 109 110
            if (maxDiff > 0) 
            {
                ts->printf(CvTS::LOG, "\nMeanShift maxDiff = %d\n", maxDiff);
                ts->set_failed_test_info(CvTS::FAIL_GENERIC);
                return;
            }
111
        }
Anatoly Baksheev's avatar
Anatoly Baksheev committed
112
        catch(const cv::Exception& e)
113
        {
Anatoly Baksheev's avatar
Anatoly Baksheev committed
114 115
            if (!check_and_treat_gpu_exception(e, ts))
                throw;
116 117
            return;
        }
Anatoly Baksheev's avatar
Anatoly Baksheev committed
118 119

        ts->set_failed_test_info(CvTS::OK);
120 121
    }

Anatoly Baksheev's avatar
Anatoly Baksheev committed
122 123
};

124 125
/////////////////////////////////////////////////////////////////////////////
/////////////////// tests registration  /////////////////////////////////////
Anatoly Baksheev's avatar
Anatoly Baksheev committed
126
/////////////////////////////////////////////////////////////////////////////
127

128
CV_GpuMeanShiftTest CV_GpuMeanShift_test;
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

struct CV_GpuMeanShiftProcTest : public CvTest
{
    CV_GpuMeanShiftProcTest(): CvTest( "GPU-MeanShiftProc", "MeanShiftProc" ){}

    void run(int)
    {
        int spatialRad = 30;
        int colorRad = 30;

        cv::Mat img = cv::imread(std::string(ts->get_data_path()) + "meanshift/cones.png");

        if (img.empty())
        {
            ts->set_failed_test_info(CvTS::FAIL_MISSING_TEST_DATA);
            return;
        }

        cv::Mat rgba;
        cvtColor(img, rgba, CV_BGR2BGRA);

        try
        {
            cv::gpu::GpuMat h_rmap_filtered;
            cv::gpu::meanShiftFiltering( cv::gpu::GpuMat(rgba), h_rmap_filtered, spatialRad, colorRad );

            cv::gpu::GpuMat d_rmap;
            cv::gpu::GpuMat d_spmap;
            cv::gpu::meanShiftProc( cv::gpu::GpuMat(rgba), d_rmap, d_spmap, spatialRad, colorRad );

            if (d_rmap.type() != CV_8UC4)
            {
                ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);
                return;
            }

            cv::Mat rmap_filtered;
            h_rmap_filtered.download(rmap_filtered);

            cv::Mat rmap;
            d_rmap.download(rmap);

            uchar maxDiff = 0;
            for (int j = 0; j < rmap_filtered.rows; ++j)
            {
                const uchar* res_line = rmap_filtered.ptr<uchar>(j);
                const uchar* ref_line = rmap.ptr<uchar>(j);

                for (int i = 0; i < rmap_filtered.cols; ++i)
                {
                    for (int k = 0; k < 3; ++k)
                    {
                        const uchar& ch1 = res_line[rmap_filtered.channels()*i + k];
                        const uchar& ch2 = ref_line[rmap.channels()*i + k];
                        uchar diff = static_cast<uchar>(abs(ch1 - ch2));
                        if (maxDiff < diff)
                            maxDiff = diff;
                    }
                }
            }
            if (maxDiff > 0) 
            {
                ts->printf(CvTS::LOG, "\nMeanShiftProc maxDiff = %d\n", maxDiff);
                ts->set_failed_test_info(CvTS::FAIL_GENERIC);
                return;
            }

            cv::Mat spmap;
            d_spmap.download(spmap);

            cv::Mat spmap_template;
200 201
            cv::FileStorage fs;

202
            if (cv::gpu::TargetArchs::hasEqualOrGreater(2, 0) && cv::gpu::DeviceInfo().major() >= 2)
203
                fs.open(std::string(ts->get_data_path()) + "meanshift/spmap.yaml", cv::FileStorage::READ);
204 205
            else
                fs.open(std::string(ts->get_data_path()) + "meanshift/spmap_CC1X.yaml", cv::FileStorage::READ);
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
            fs["spmap"] >> spmap_template;

            for (int y = 0; y < spmap.rows; ++y) {
                for (int x = 0; x < spmap.cols; ++x) {
                    cv::Point_<short> expected = spmap_template.at<cv::Point_<short> >(y, x);
                    cv::Point_<short> actual = spmap.at<cv::Point_<short> >(y, x);
                    int diff = (expected - actual).dot(expected - actual);
                    if (actual != expected) {
                        ts->printf(CvTS::LOG, "\nMeanShiftProc SpMap is bad, diff=%d\n", diff);
                        ts->set_failed_test_info(CvTS::FAIL_GENERIC);
                        return;
                    }
                }
            }

        }
        catch(const cv::Exception& e)
        {
            if (!check_and_treat_gpu_exception(e, ts))
                throw;
            return;
        }

        ts->set_failed_test_info(CvTS::OK);
    }

};

CV_GpuMeanShiftProcTest CV_GpuMeanShiftProc_test;