canny.cpp 16.4 KB
Newer Older
yao's avatar
yao committed
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
/*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) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Peng Xiao, pengxiao@multicorewareinc.com
//
// 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
Andrey Pavlenko's avatar
Andrey Pavlenko committed
28
//     and/or other materials provided with the distribution.
yao's avatar
yao committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//
//   * 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*/

#include "precomp.hpp"
47
#include "opencl_kernels.hpp"
yao's avatar
yao committed
48 49 50 51

using namespace cv;
using namespace cv::ocl;

52
cv::ocl::CannyBuf::CannyBuf(const oclMat &dx_, const oclMat &dy_) : dx(dx_), dy(dy_), counter(1, 1, CV_32SC1)
yao's avatar
yao committed
53 54 55 56 57 58
{
    CV_Assert(dx_.type() == CV_32SC1 && dy_.type() == CV_32SC1 && dx_.size() == dy_.size());

    create(dx_.size(), -1);
}

59
void cv::ocl::CannyBuf::create(const Size &image_size, int apperture_size)
yao's avatar
yao committed
60
{
61 62
    ensureSizeIsEnough(image_size, CV_32SC1, dx);
    ensureSizeIsEnough(image_size, CV_32SC1, dy);
63 64 65

    if(apperture_size == 3)
    {
66 67
        ensureSizeIsEnough(image_size, CV_32SC1, dx_buf);
        ensureSizeIsEnough(image_size, CV_32SC1, dy_buf);
68 69
    }
    else if(apperture_size > 0)
yao's avatar
yao committed
70
    {
71
        Mat kx, ky;
yao's avatar
yao committed
72
        if (!filterDX)
73 74 75
        {
            filterDX = createDerivFilter_GPU(CV_8U, CV_32S, 1, 0, apperture_size, BORDER_REPLICATE);
        }
yao's avatar
yao committed
76
        if (!filterDY)
77 78 79
        {
            filterDY = createDerivFilter_GPU(CV_8U, CV_32S, 0, 1, apperture_size, BORDER_REPLICATE);
        }
yao's avatar
yao committed
80
    }
peng xiao's avatar
peng xiao committed
81
    ensureSizeIsEnough(2 * (image_size.height + 2), image_size.width + 2, CV_32FC1, edgeBuf);
yao's avatar
yao committed
82

83 84
    ensureSizeIsEnough(1, image_size.area(), CV_16UC2, trackBuf1);
    ensureSizeIsEnough(1, image_size.area(), CV_16UC2, trackBuf2);
yao's avatar
yao committed
85 86 87 88 89 90 91 92 93 94 95 96 97
}

void cv::ocl::CannyBuf::release()
{
    dx.release();
    dy.release();
    dx_buf.release();
    dy_buf.release();
    edgeBuf.release();
    trackBuf1.release();
    trackBuf2.release();
}

98 99 100
namespace cv
{
    namespace ocl
101
    {
102 103 104
        namespace canny
        {
            void calcSobelRowPass_gpu(const oclMat &src, oclMat &dx_buf, oclMat &dy_buf, int rows, int cols);
yao's avatar
yao committed
105

106 107
            void calcMagnitude_gpu(const oclMat &dx_buf, const oclMat &dy_buf, oclMat &dx, oclMat &dy, oclMat &mag, int rows, int cols, bool L2Grad);
            void calcMagnitude_gpu(const oclMat &dx, const oclMat &dy, oclMat &mag, int rows, int cols, bool L2Grad);
yao's avatar
yao committed
108

109
            void calcMap_gpu(oclMat &dx, oclMat &dy, oclMat &mag, oclMat &map, int rows, int cols, float low_thresh, float high_thresh);
yao's avatar
yao committed
110

111
            void edgesHysteresisLocal_gpu(oclMat &map, oclMat &st1, oclMat& counter, int rows, int cols);
yao's avatar
yao committed
112

113
            void edgesHysteresisGlobal_gpu(oclMat &map, oclMat &st1, oclMat &st2, oclMat& counter, int rows, int cols);
yao's avatar
yao committed
114

115 116
            void getEdges_gpu(oclMat &map, oclMat &dst, int rows, int cols);
        }
117
    }
118
}// cv::ocl
yao's avatar
yao committed
119 120 121

namespace
{
122
    void CannyCaller(CannyBuf &buf, oclMat &dst, float low_thresh, float high_thresh)
yao's avatar
yao committed
123 124
    {
        using namespace ::cv::ocl::canny;
peng xiao's avatar
peng xiao committed
125 126
        oclMat magBuf = buf.edgeBuf(Rect(0, 0, buf.edgeBuf.cols, buf.edgeBuf.rows / 2));
        oclMat mapBuf = buf.edgeBuf(Rect(0, buf.edgeBuf.rows / 2, buf.edgeBuf.cols, buf.edgeBuf.rows / 2));
yao's avatar
yao committed
127

peng xiao's avatar
peng xiao committed
128
        calcMap_gpu(buf.dx, buf.dy, magBuf, mapBuf, dst.rows, dst.cols, low_thresh, high_thresh);
yao's avatar
yao committed
129

peng xiao's avatar
peng xiao committed
130
        edgesHysteresisLocal_gpu(mapBuf, buf.trackBuf1, buf.counter, dst.rows, dst.cols);
yao's avatar
yao committed
131

peng xiao's avatar
peng xiao committed
132 133 134
        edgesHysteresisGlobal_gpu(mapBuf, buf.trackBuf1, buf.trackBuf2, buf.counter, dst.rows, dst.cols);

        getEdges_gpu(mapBuf, dst, dst.rows, dst.cols);
yao's avatar
yao committed
135 136 137
    }
}

138
void cv::ocl::Canny(const oclMat &src, oclMat &dst, double low_thresh, double high_thresh, int apperture_size, bool L2gradient)
yao's avatar
yao committed
139 140 141 142 143
{
    CannyBuf buf(src.size(), apperture_size);
    Canny(src, buf, dst, low_thresh, high_thresh, apperture_size, L2gradient);
}

144
void cv::ocl::Canny(const oclMat &src, CannyBuf &buf, oclMat &dst, double low_thresh, double high_thresh, int apperture_size, bool L2gradient)
yao's avatar
yao committed
145 146 147 148 149 150 151 152 153
{
    using namespace ::cv::ocl::canny;

    CV_Assert(src.type() == CV_8UC1);

    if( low_thresh > high_thresh )
        std::swap( low_thresh, high_thresh );

    dst.create(src.size(), CV_8U);
niko's avatar
niko committed
154
    dst.setTo(Scalar::all(0));
yao's avatar
yao committed
155 156

    buf.create(src.size(), apperture_size);
niko's avatar
niko committed
157
    buf.edgeBuf.setTo(Scalar::all(0));
yao's avatar
yao committed
158

peng xiao's avatar
peng xiao committed
159 160
    oclMat magBuf = buf.edgeBuf(Rect(0, 0, buf.edgeBuf.cols, buf.edgeBuf.rows / 2));

yao's avatar
yao committed
161 162 163 164
    if (apperture_size == 3)
    {
        calcSobelRowPass_gpu(src, buf.dx_buf, buf.dy_buf, src.rows, src.cols);

peng xiao's avatar
peng xiao committed
165
        calcMagnitude_gpu(buf.dx_buf, buf.dy_buf, buf.dx, buf.dy, magBuf, src.rows, src.cols, L2gradient);
yao's avatar
yao committed
166 167 168
    }
    else
    {
169 170
        buf.filterDX->apply(src, buf.dx);
        buf.filterDY->apply(src, buf.dy);
yao's avatar
yao committed
171

peng xiao's avatar
peng xiao committed
172
        calcMagnitude_gpu(buf.dx, buf.dy, magBuf, src.rows, src.cols, L2gradient);
yao's avatar
yao committed
173 174 175
    }
    CannyCaller(buf, dst, static_cast<float>(low_thresh), static_cast<float>(high_thresh));
}
176
void cv::ocl::Canny(const oclMat &dx, const oclMat &dy, oclMat &dst, double low_thresh, double high_thresh, bool L2gradient)
yao's avatar
yao committed
177 178 179 180 181
{
    CannyBuf buf(dx, dy);
    Canny(dx, dy, buf, dst, low_thresh, high_thresh, L2gradient);
}

182
void cv::ocl::Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &dst, double low_thresh, double high_thresh, bool L2gradient)
yao's avatar
yao committed
183 184 185 186 187 188 189 190 191
{
    using namespace ::cv::ocl::canny;

    CV_Assert(dx.type() == CV_32SC1 && dy.type() == CV_32SC1 && dx.size() == dy.size());

    if( low_thresh > high_thresh )
        std::swap( low_thresh, high_thresh);

    dst.create(dx.size(), CV_8U);
niko's avatar
niko committed
192
    dst.setTo(Scalar::all(0));
yao's avatar
yao committed
193

194 195
    buf.dx = dx;
    buf.dy = dy;
yao's avatar
yao committed
196
    buf.create(dx.size(), -1);
niko's avatar
niko committed
197
    buf.edgeBuf.setTo(Scalar::all(0));
peng xiao's avatar
peng xiao committed
198 199 200 201

    oclMat magBuf = buf.edgeBuf(Rect(0, 0, buf.edgeBuf.cols, buf.edgeBuf.rows / 2));

    calcMagnitude_gpu(buf.dx, buf.dy, magBuf, dx.rows, dx.cols, L2gradient);
yao's avatar
yao committed
202 203 204 205

    CannyCaller(buf, dst, static_cast<float>(low_thresh), static_cast<float>(high_thresh));
}

206
void canny::calcSobelRowPass_gpu(const oclMat &src, oclMat &dx_buf, oclMat &dy_buf, int rows, int cols)
yao's avatar
yao committed
207
{
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    Context *clCxt = src.clCxt;
    string kernelName = "calcSobelRowPass";
    vector< pair<size_t, const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dx_buf.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dy_buf.data));
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( make_pair( sizeof(cl_int), (void *)&src.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&src.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx_buf.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx_buf.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy_buf.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy_buf.offset));

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};
peng xiao's avatar
peng xiao committed
226
    openCLExecuteKernel(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
227 228
}

229
void canny::calcMagnitude_gpu(const oclMat &dx_buf, const oclMat &dy_buf, oclMat &dx, oclMat &dy, oclMat &mag, int rows, int cols, bool L2Grad)
yao's avatar
yao committed
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
    Context *clCxt = dx_buf.clCxt;
    string kernelName = "calcMagnitude_buf";
    vector< pair<size_t, const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&dx_buf.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dy_buf.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dx.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dy.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&mag.data));
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx_buf.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx_buf.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy_buf.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy_buf.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.offset));

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};

peng xiao's avatar
peng xiao committed
256 257
    const char * build_options = L2Grad ? "-D L2GRAD":"";
    openCLExecuteKernel(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1, build_options);
yao's avatar
yao committed
258
}
259
void canny::calcMagnitude_gpu(const oclMat &dx, const oclMat &dy, oclMat &mag, int rows, int cols, bool L2Grad)
yao's avatar
yao committed
260
{
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    Context *clCxt = dx.clCxt;
    string kernelName = "calcMagnitude";
    vector< pair<size_t, const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&dx.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dy.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&mag.data));
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.offset));

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};

peng xiao's avatar
peng xiao committed
280 281
    const char * build_options = L2Grad ? "-D L2GRAD":"";
    openCLExecuteKernel(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1, build_options);
yao's avatar
yao committed
282 283
}

284
void canny::calcMap_gpu(oclMat &dx, oclMat &dy, oclMat &mag, oclMat &map, int rows, int cols, float low_thresh, float high_thresh)
yao's avatar
yao committed
285
{
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    Context *clCxt = dx.clCxt;

    vector< pair<size_t, const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&dx.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dy.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&mag.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&map.data));
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( make_pair( sizeof(cl_float), (void *)&low_thresh));
    args.push_back( make_pair( sizeof(cl_float), (void *)&high_thresh));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dx.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dy.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&mag.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&map.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&map.offset));
yao's avatar
yao committed
306

307

308 309 310
    size_t globalThreads[3] = {cols, rows, 1};
    string kernelName = "calcMap";
    size_t localThreads[3]  = {16, 16, 1};
311

peng xiao's avatar
peng xiao committed
312
    openCLExecuteKernel(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
313 314
}

315
void canny::edgesHysteresisLocal_gpu(oclMat &map, oclMat &st1, oclMat& counter, int rows, int cols)
yao's avatar
yao committed
316
{
317 318 319
    Context *clCxt = map.clCxt;
    vector< pair<size_t, const void *> > args;

320 321 322 323
    Mat counterMat(counter.rows, counter.cols, counter.type());
    counterMat.at<int>(0, 0) = 0;
    counter.upload(counterMat);

324 325
    args.push_back( make_pair( sizeof(cl_mem), (void *)&map.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&st1.data));
326
    args.push_back( make_pair( sizeof(cl_mem), (void *)&counter.data));
327 328
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
329 330 331 332
    cl_int stepBytes = map.step;
    args.push_back( make_pair( sizeof(cl_int), (void *)&stepBytes));
    cl_int offsetBytes = map.offset;
    args.push_back( make_pair( sizeof(cl_int), (void *)&offsetBytes));
333 334 335 336

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};

337
    openCLExecuteKernel(clCxt, &imgproc_canny, "edgesHysteresisLocal", globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
338 339
}

340
void canny::edgesHysteresisGlobal_gpu(oclMat &map, oclMat &st1, oclMat &st2, oclMat& counter, int rows, int cols)
yao's avatar
yao committed
341
{
342 343 344
    Context *clCxt = map.clCxt;
    vector< pair<size_t, const void *> > args;
    size_t localThreads[3]  = {128, 1, 1};
yao's avatar
yao committed
345

346
    while(1 > 0)
347
    {
348
        Mat counterMat; counter.download(counterMat);
349 350 351 352 353 354 355
        int count = counterMat.at<int>(0, 0);
        CV_Assert(count >= 0);
        if (count == 0)
            break;

        counterMat.at<int>(0, 0) = 0;
        counter.upload(counterMat);
356

357
        args.clear();
358
        size_t globalThreads[3] = {std::min((unsigned)count, 65535u) * 128, divUp(count, 65535), 1};
359 360 361
        args.push_back( make_pair( sizeof(cl_mem), (void *)&map.data));
        args.push_back( make_pair( sizeof(cl_mem), (void *)&st1.data));
        args.push_back( make_pair( sizeof(cl_mem), (void *)&st2.data));
362
        args.push_back( make_pair( sizeof(cl_mem), (void *)&counter.data));
363 364 365 366 367 368
        args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
        args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
        args.push_back( make_pair( sizeof(cl_int), (void *)&count));
        args.push_back( make_pair( sizeof(cl_int), (void *)&map.step));
        args.push_back( make_pair( sizeof(cl_int), (void *)&map.offset));

369
        openCLExecuteKernel(clCxt, &imgproc_canny, "edgesHysteresisGlobal", globalThreads, localThreads, args, -1, -1);
370 371
        std::swap(st1, st2);
    }
yao's avatar
yao committed
372 373
}

374
void canny::getEdges_gpu(oclMat &map, oclMat &dst, int rows, int cols)
yao's avatar
yao committed
375
{
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    Context *clCxt = map.clCxt;
    string kernelName = "getEdges";
    vector< pair<size_t, const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&map.data));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
    args.push_back( make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( make_pair( sizeof(cl_int), (void *)&map.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&map.offset));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dst.step));
    args.push_back( make_pair( sizeof(cl_int), (void *)&dst.offset));

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};

peng xiao's avatar
peng xiao committed
392
    openCLExecuteKernel(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
393
}