descriptor.cpp 12.2 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
//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
//                       (3-clause BSD License)
//
//Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
//Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
//Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
//Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
//Copyright (C) 2015, OpenCV Foundation, all rights reserved.
//Copyright (C) 2015, Itseez 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:
//
//  * Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
//  * Redistributions 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.
//
//  * Neither the names of the copyright holders nor the names of the contributors
//    may 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 copyright holders 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.

/*****************************************************************************************************************\
44
*                             The file contains the implemented descriptors                                       *
45
\******************************************************************************************************************/
46
#include "precomp.hpp"
47

48
namespace cv
49
{
50
    namespace stereo
Muresan Mircea Paul's avatar
Muresan Mircea Paul committed
51
    {
52 53
        //function that performs the census transform on two images.
        //Two variants of census are offered a sparse version whcih takes every second pixel as well as dense version
54
        CV_EXPORTS void censusTransform(const Mat &image1, const Mat &image2, int kernelSize, Mat &dist1, Mat &dist2, const int type)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        {
            CV_Assert(image1.size() == image2.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(image1.type() == CV_8UC1 && image2.type() == CV_8UC1);
            CV_Assert(type != CV_DENSE_CENSUS || type != CV_SPARSE_CENSUS);
            CV_Assert(kernelSize <= ((type == 0) ? 5 : 11));
            int n2 = (kernelSize) / 2;
            uint8_t *images[] = {image1.data, image2.data};
            int *costs[] = {(int *)dist1.data,(int *)dist2.data};
            int stride = (int)image1.step;
            if(type == CV_DENSE_CENSUS)
            {
                parallel_for_(  Range(n2, image1.rows - n2),
                    CombinedDescriptor<1,1,1,2,CensusKernel<2> >(image1.cols, image1.rows,stride,n2,costs,CensusKernel<2>(images),n2));
            }
            else if(type == CV_SPARSE_CENSUS)
            {
                parallel_for_( Range(n2, image1.rows - n2),
                    CombinedDescriptor<2,2,1,2,CensusKernel<2> >(image1.cols, image1.rows, stride,n2,costs,CensusKernel<2>(images),n2));
            }
        }
        //function that performs census on one image
77
        CV_EXPORTS void censusTransform(const Mat &image1, int kernelSize, Mat &dist1, const int type)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        {
            CV_Assert(image1.size() == dist1.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(image1.type() == CV_8UC1);
            CV_Assert(type != CV_DENSE_CENSUS || type != CV_SPARSE_CENSUS);
            CV_Assert(kernelSize <= ((type == 0) ? 5 : 11));
            int n2 = (kernelSize) / 2;
            uint8_t *images[] = {image1.data};
            int *costs[] = {(int *)dist1.data};
            int stride = (int)image1.step;
            if(type == CV_DENSE_CENSUS)
            {
                parallel_for_( Range(n2, image1.rows - n2),
                    CombinedDescriptor<1,1,1,1,CensusKernel<1> >(image1.cols, image1.rows,stride,n2,costs,CensusKernel<1>(images),n2));
            }
            else if(type == CV_SPARSE_CENSUS)
            {
                parallel_for_( Range(n2, image1.rows - n2),
                    CombinedDescriptor<2,2,1,1,CensusKernel<1> >(image1.cols, image1.rows,stride,n2,costs,CensusKernel<1>(images),n2));
            }
        }
        //in a 9x9 kernel only certain positions are choosen for comparison
100
        CV_EXPORTS void starCensusTransform(const Mat &img1, const Mat &img2, int kernelSize, Mat &dist1, Mat &dist2)
101 102 103 104 105 106 107 108 109 110 111
        {
            CV_Assert(img1.size() == img2.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1 && img2.type() == CV_8UC1);
            CV_Assert(kernelSize >= 7);
            int n2 = (kernelSize) >> 1;
            Mat images[] = {img1, img2};
            int *date[] = { (int *)dist1.data, (int *)dist2.data};
            parallel_for_(Range(n2, img1.rows - n2), StarKernelCensus<2>(images, n2,date));
        }
        //single version of star census
112
        CV_EXPORTS void starCensusTransform(const Mat &img1, int kernelSize, Mat &dist)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        {
            CV_Assert(img1.size() == dist.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1);
            CV_Assert(kernelSize >= 7);
            int n2 = (kernelSize) >> 1;
            Mat images[] = {img1};
            int *date[] = { (int *)dist.data};
            parallel_for_(Range(n2, img1.rows - n2), StarKernelCensus<1>(images, n2,date));
        }
        //Modified census transforms
        //the first one deals with small illumination changes
        //the sencond modified census transform is invariant to noise; i.e.
        //if the current pixel with whom we are dooing the comparison is a noise, this descriptor will provide a better result by comparing with the mean of the window
        //otherwise if the pixel is not noise the information is strengthend
128
        CV_EXPORTS void modifiedCensusTransform(const Mat &img1, const Mat &img2, int kernelSize, Mat &dist1,Mat &dist2, const int type, int t, const Mat &IntegralImage1, const Mat &IntegralImage2 )
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
        {
            CV_Assert(img1.size() == img2.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1 && img2.type() == CV_8UC1);
            CV_Assert(type != CV_MODIFIED_CENSUS_TRANSFORM || type != CV_MEAN_VARIATION);
            CV_Assert(kernelSize <= 9);
            int n2 = (kernelSize - 1) >> 1;
            uint8_t *images[] = {img1.data, img2.data};
            int *date[] = { (int *)dist1.data, (int *)dist2.data};
            int stride = (int)img1.cols;
            if(type == CV_MODIFIED_CENSUS_TRANSFORM)
            {
                //MCT
                parallel_for_(  Range(n2, img1.rows - n2),
                    CombinedDescriptor<2,4,2, 2,MCTKernel<2> >(img1.cols, img1.rows,stride,n2,date,MCTKernel<2>(images,t),n2));
            }
            else if(type == CV_MEAN_VARIATION)
            {
                //MV
                int *integral[2];
                integral[0] = (int *)IntegralImage1.data;
                integral[1] = (int *)IntegralImage2.data;
                parallel_for_(  Range(n2, img1.rows - n2),
                    CombinedDescriptor<2,3,2,2, MVKernel<2> >(img1.cols, img1.rows,stride,n2,date,MVKernel<2>(images,integral),n2));
            }
        }
155
        CV_EXPORTS void modifiedCensusTransform(const Mat &img1, int kernelSize, Mat &dist, const int type, int t , Mat const &IntegralImage)
156 157 158 159 160 161 162 163 164 165 166 167 168 169
        {
            CV_Assert(img1.size() == dist.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1);
            CV_Assert(type != CV_MODIFIED_CENSUS_TRANSFORM || type != CV_MEAN_VARIATION);
            CV_Assert(kernelSize <= 9);
            int n2 = (kernelSize - 1) >> 1;
            uint8_t *images[] = {img1.data};
            int *date[] = { (int *)dist.data};
            int stride = (int)img1.step;
            if(type == CV_MODIFIED_CENSUS_TRANSFORM)
            {
                //MCT
                parallel_for_(Range(n2, img1.rows - n2),
170
                    CombinedDescriptor<2,4,2, 1,MCTKernel<1> >(img1.cols, img1.rows,stride,n2,date,MCTKernel<1>(images,t),n2));
171 172 173 174 175 176 177 178 179 180 181
            }
            else if(type == CV_MEAN_VARIATION)
            {
                //MV
                int *integral[] = { (int *)IntegralImage.data};
                parallel_for_(Range(n2, img1.rows - n2),
                    CombinedDescriptor<2,3,2,1, MVKernel<1> >(img1.cols, img1.rows,stride,n2,date,MVKernel<1>(images,integral),n2));
            }
        }
        //different versions of simetric census
        //These variants since they do not compare with the center they are invariant to noise
182
        CV_EXPORTS void symetricCensusTransform(const Mat &img1, const Mat &img2, int kernelSize, Mat &dist1, Mat &dist2, const int type)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        {
            CV_Assert(img1.size() ==  img2.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1 && img2.type() == CV_8UC1);
            CV_Assert(type != CV_CS_CENSUS || type != CV_MODIFIED_CS_CENSUS);
            CV_Assert(kernelSize <= 7);
            int n2 = kernelSize >> 1;
            uint8_t *images[] = {img1.data, img2.data};
            Mat imag[] = {img1, img2};
            int *date[] = { (int *)dist1.data, (int *)dist2.data};
            int stride = (int)img1.step;
            if(type == CV_CS_CENSUS)
            {
                parallel_for_(Range(n2, img1.rows - n2), SymetricCensus<2>(imag, n2,date));
            }
            else if(type == CV_MODIFIED_CS_CENSUS)
            {
                parallel_for_(Range(n2, img1.rows - n2),
                    CombinedDescriptor<1,1,1,2,ModifiedCsCensus<2> >(img1.cols, img1.rows,stride,n2,date,ModifiedCsCensus<2>(images,n2),1));
            }
        }
204
        CV_EXPORTS void symetricCensusTransform(const Mat &img1, int kernelSize, Mat &dist1, const int type)
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
        {
            CV_Assert(img1.size() ==  dist1.size());
            CV_Assert(kernelSize % 2 != 0);
            CV_Assert(img1.type() == CV_8UC1);
            CV_Assert(type != CV_MODIFIED_CS_CENSUS || type != CV_CS_CENSUS);
            CV_Assert(kernelSize <= 7);
            int n2 = kernelSize >> 1;
            uint8_t *images[] = {img1.data};
            Mat imag[] = {img1};
            int *date[] = { (int *)dist1.data};
            int stride = (int)img1.step;
            if(type == CV_CS_CENSUS)
            {
                parallel_for_( Range(n2, img1.rows - n2), SymetricCensus<1>(imag, n2,date));
            }
            else if(type == CV_MODIFIED_CS_CENSUS)
            {
                parallel_for_( Range(n2, img1.rows - n2),
                    CombinedDescriptor<1,1,1,1,ModifiedCsCensus<1> >(img1.cols, img1.rows,stride,n2,date,ModifiedCsCensus<1>(images,n2),1));
            }
        }
        //integral image computation used in the Mean Variation Census Transform
        void imageMeanKernelSize(const Mat &image, int windowSize, Mat &cost)
        {
            CV_Assert(image.size > 0);
            CV_Assert(cost.size > 0);
            CV_Assert(windowSize % 2 != 0);
            int win = windowSize / 2;
            float scalling = ((float) 1) / (windowSize * windowSize);
            int height = image.rows;
            cost.setTo(0);
            int *c = (int *)cost.data;
            parallel_for_(Range(win + 1, height - win - 1),MeanKernelIntegralImage(image,win,scalling,c));
        }
Muresan Mircea Paul's avatar
Muresan Mircea Paul committed
239
    }
240
}