canny.cpp 17.3 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*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
//     and/or other oclMaterials 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*/

#include "precomp.hpp"

using namespace cv;
using namespace cv::ocl;

namespace cv
{
53 54 55 56 57
    namespace ocl
    {
        ///////////////////////////OpenCL kernel strings///////////////////////////
        extern const char *imgproc_canny;
    }
yao's avatar
yao committed
58 59
}

60
cv::ocl::CannyBuf::CannyBuf(const oclMat &dx_, const oclMat &dy_) : dx(dx_), dy(dy_), counter(NULL)
yao's avatar
yao committed
61 62 63 64 65 66
{
    CV_Assert(dx_.type() == CV_32SC1 && dy_.type() == CV_32SC1 && dx_.size() == dy_.size());

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

67
void cv::ocl::CannyBuf::create(const Size &image_size, int apperture_size)
yao's avatar
yao committed
68
{
69 70
    ensureSizeIsEnough(image_size, CV_32SC1, dx);
    ensureSizeIsEnough(image_size, CV_32SC1, dy);
71 72 73

    if(apperture_size == 3)
    {
74 75
        ensureSizeIsEnough(image_size, CV_32SC1, dx_buf);
        ensureSizeIsEnough(image_size, CV_32SC1, dy_buf);
76 77
    }
    else if(apperture_size > 0)
yao's avatar
yao committed
78
    {
79
        Mat kx, ky;
yao's avatar
yao committed
80
        if (!filterDX)
81 82 83
        {
            filterDX = createDerivFilter_GPU(CV_8U, CV_32S, 1, 0, apperture_size, BORDER_REPLICATE);
        }
yao's avatar
yao committed
84
        if (!filterDY)
85 86 87
        {
            filterDY = createDerivFilter_GPU(CV_8U, CV_32S, 0, 1, apperture_size, BORDER_REPLICATE);
        }
yao's avatar
yao committed
88
    }
89 90
    ensureSizeIsEnough(image_size.height + 2, image_size.width + 2, CV_32FC1, magBuf);
    ensureSizeIsEnough(image_size.height + 2, image_size.width + 2, CV_32FC1, mapBuf);
yao's avatar
yao committed
91

92 93
    ensureSizeIsEnough(1, image_size.width * image_size.height, CV_16UC2, trackBuf1);
    ensureSizeIsEnough(1, image_size.width * image_size.height, CV_16UC2, trackBuf2);
94

95
    int counter_i [1] = { 0 };
96
    int err = 0;
97 98 99 100
    if(counter)
    {
        openCLFree(counter);
    }
peng xiao's avatar
peng xiao committed
101
    counter = clCreateBuffer( *((cl_context*)getoclContext()), CL_MEM_COPY_HOST_PTR, sizeof(int), counter_i, &err );
102
    openCLSafeCall(err);
yao's avatar
yao committed
103 104 105 106 107 108 109 110
}

void cv::ocl::CannyBuf::release()
{
    dx.release();
    dy.release();
    dx_buf.release();
    dy_buf.release();
111 112
    magBuf.release();
    mapBuf.release();
yao's avatar
yao committed
113 114
    trackBuf1.release();
    trackBuf2.release();
115 116 117 118 119
    if(counter)
    {
        openCLFree(counter);
        counter = NULL;
    }
yao's avatar
yao committed
120 121
}

122 123 124
namespace cv
{
    namespace ocl
125
    {
126 127 128
        namespace canny
        {
            void calcSobelRowPass_gpu(const oclMat &src, oclMat &dx_buf, oclMat &dy_buf, int rows, int cols);
yao's avatar
yao committed
129

130 131
            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
132

133
            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
134

135
            void edgesHysteresisLocal_gpu(oclMat &map, oclMat &st1, void *counter, int rows, int cols);
yao's avatar
yao committed
136

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

139 140
            void getEdges_gpu(oclMat &map, oclMat &dst, int rows, int cols);
        }
141
    }
142
}// cv::ocl
yao's avatar
yao committed
143 144 145

namespace
{
146
    void CannyCaller(CannyBuf &buf, oclMat &dst, float low_thresh, float high_thresh)
yao's avatar
yao committed
147 148
    {
        using namespace ::cv::ocl::canny;
149
        calcMap_gpu(buf.dx, buf.dy, buf.magBuf, buf.mapBuf, dst.rows, dst.cols, low_thresh, high_thresh);
yao's avatar
yao committed
150

151
        edgesHysteresisLocal_gpu(buf.mapBuf, buf.trackBuf1, buf.counter, dst.rows, dst.cols);
yao's avatar
yao committed
152

153
        edgesHysteresisGlobal_gpu(buf.mapBuf, buf.trackBuf1, buf.trackBuf2, buf.counter, dst.rows, dst.cols);
yao's avatar
yao committed
154

155
        getEdges_gpu(buf.mapBuf, dst, dst.rows, dst.cols);
yao's avatar
yao committed
156 157 158
    }
}

159
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
160 161 162 163 164
{
    CannyBuf buf(src.size(), apperture_size);
    Canny(src, buf, dst, low_thresh, high_thresh, apperture_size, L2gradient);
}

165
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
166 167 168 169 170 171 172 173 174
{
    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
175
    dst.setTo(Scalar::all(0));
yao's avatar
yao committed
176 177

    buf.create(src.size(), apperture_size);
178
    buf.magBuf.setTo(Scalar::all(0));
yao's avatar
yao committed
179 180 181 182 183

    if (apperture_size == 3)
    {
        calcSobelRowPass_gpu(src, buf.dx_buf, buf.dy_buf, src.rows, src.cols);

184
        calcMagnitude_gpu(buf.dx_buf, buf.dy_buf, buf.dx, buf.dy, buf.magBuf, src.rows, src.cols, L2gradient);
yao's avatar
yao committed
185 186 187
    }
    else
    {
188 189
        buf.filterDX->apply(src, buf.dx);
        buf.filterDY->apply(src, buf.dy);
yao's avatar
yao committed
190

191
        calcMagnitude_gpu(buf.dx, buf.dy, buf.magBuf, src.rows, src.cols, L2gradient);
yao's avatar
yao committed
192 193 194
    }
    CannyCaller(buf, dst, static_cast<float>(low_thresh), static_cast<float>(high_thresh));
}
195
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
196 197 198 199 200
{
    CannyBuf buf(dx, dy);
    Canny(dx, dy, buf, dst, low_thresh, high_thresh, L2gradient);
}

201
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
202 203 204 205 206 207 208 209 210
{
    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
211
    dst.setTo(Scalar::all(0));
yao's avatar
yao committed
212

213 214
    buf.dx = dx;
    buf.dy = dy;
yao's avatar
yao committed
215
    buf.create(dx.size(), -1);
216 217
    buf.magBuf.setTo(Scalar::all(0));
    calcMagnitude_gpu(buf.dx, buf.dy, buf.magBuf, dx.rows, dx.cols, L2gradient);
yao's avatar
yao committed
218 219 220 221

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

222
void canny::calcSobelRowPass_gpu(const oclMat &src, oclMat &dx_buf, oclMat &dy_buf, int rows, int cols)
yao's avatar
yao committed
223
{
224
    Context *clCxt = src.clCxt;
225
    String kernelName = "calcSobelRowPass";
226 227 228 229 230 231 232 233 234 235 236 237 238
    std::vector< std::pair<size_t, const void *> > args;

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

    size_t globalThreads[3] = {cols, rows, 1};
    size_t localThreads[3]  = {16, 16, 1};
242
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
243 244
}

245
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
246
{
247
    Context *clCxt = dx_buf.clCxt;
248
    String kernelName = "calcMagnitude_buf";
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    std::vector< std::pair<size_t, const void *> > args;

    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dx_buf.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dy_buf.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dx.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dy.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mag.data));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx_buf.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx_buf.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy_buf.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy_buf.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&mag.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&mag.offset));
268 269 270 271 272 273 274 275 276

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

    char build_options [15] = "";
    if(L2Grad)
    {
        strcat(build_options, "-D L2GRAD");
    }
277
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1, build_options);
yao's avatar
yao committed
278
}
279
void canny::calcMagnitude_gpu(const oclMat &dx, const oclMat &dy, oclMat &mag, int rows, int cols, bool L2Grad)
yao's avatar
yao committed
280
{
281
    Context *clCxt = dx.clCxt;
282
    String kernelName = "calcMagnitude";
283 284 285 286 287 288 289 290 291 292 293 294 295
    std::vector< std::pair<size_t, const void *> > args;

    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dx.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dy.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mag.data));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dx.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dy.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&mag.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&mag.offset));
296 297 298 299 300 301 302 303 304

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

    char build_options [15] = "";
    if(L2Grad)
    {
        strcat(build_options, "-D L2GRAD");
    }
305
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1, build_options);
yao's avatar
yao committed
306 307
}

308
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
309
{
310 311
    Context *clCxt = dx.clCxt;

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    std::vector< std::pair<size_t, const void *> > args;

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

331

332
    size_t globalThreads[3] = {cols, rows, 1};
333
    String kernelName = "calcMap";
334
    size_t localThreads[3]  = {16, 16, 1};
335

336
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
337 338
}

339
void canny::edgesHysteresisLocal_gpu(oclMat &map, oclMat &st1, void *counter, int rows, int cols)
yao's avatar
yao committed
340
{
341
    Context *clCxt = map.clCxt;
342
    String kernelName = "edgesHysteresisLocal";
343
    std::vector< std::pair<size_t, const void *> > args;
344

345 346 347 348 349 350 351
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&st1.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&counter));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.offset));
352 353 354 355

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

356
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
357 358
}

359
void canny::edgesHysteresisGlobal_gpu(oclMat &map, oclMat &st1, oclMat &st2, void *counter, int rows, int cols)
yao's avatar
yao committed
360
{
361
    unsigned int count;
peng xiao's avatar
peng xiao committed
362
    openCLSafeCall(clEnqueueReadBuffer(*(cl_command_queue*)getoclCommandQueue(), (cl_mem)counter, 1, 0, sizeof(float), &count, 0, NULL, NULL));
363
    Context *clCxt = map.clCxt;
364
    String kernelName = "edgesHysteresisGlobal";
365
    std::vector< std::pair<size_t, const void *> > args;
366
    size_t localThreads[3]  = {128, 1, 1};
yao's avatar
yao committed
367 368

#define DIVUP(a, b) ((a)+(b)-1)/(b)
369
    int count_i[1] = {0};
370 371
    while(count > 0)
    {
peng xiao's avatar
peng xiao committed
372
        openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)getoclCommandQueue(), (cl_mem)counter, 1, 0, sizeof(int), &count_i, 0, NULL, NULL));
373

374 375
        args.clear();
        size_t globalThreads[3] = {std::min(count, 65535u) * 128, DIVUP(count, 65535), 1};
376 377 378 379 380 381 382 383 384
        args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map.data));
        args.push_back( std::make_pair( sizeof(cl_mem), (void *)&st1.data));
        args.push_back( std::make_pair( sizeof(cl_mem), (void *)&st2.data));
        args.push_back( std::make_pair( sizeof(cl_mem), (void *)&counter));
        args.push_back( std::make_pair( sizeof(cl_int), (void *)&rows));
        args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
        args.push_back( std::make_pair( sizeof(cl_int), (void *)&count));
        args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.step));
        args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.offset));
385

386
        openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1, DISABLE);
peng xiao's avatar
peng xiao committed
387
        openCLSafeCall(clEnqueueReadBuffer(*(cl_command_queue*)getoclCommandQueue(), (cl_mem)counter, 1, 0, sizeof(int), &count, 0, NULL, NULL));
388 389
        std::swap(st1, st2);
    }
yao's avatar
yao committed
390 391 392
#undef DIVUP
}

393
void canny::getEdges_gpu(oclMat &map, oclMat &dst, int rows, int cols)
yao's avatar
yao committed
394
{
395
    Context *clCxt = map.clCxt;
396
    String kernelName = "getEdges";
397 398 399 400 401 402 403 404 405 406
    std::vector< std::pair<size_t, const void *> > args;

    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map.data));
    args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&rows));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&map.offset));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step));
    args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset));
407 408 409 410

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

411
    openCLExecuteKernel2(clCxt, &imgproc_canny, kernelName, globalThreads, localThreads, args, -1, -1);
yao's avatar
yao committed
412
}