为OpenCV编写自定义OpenCL函数

时间:2016-12-17 10:25:12

标签: c++ opencv opencl

我正在尝试在OpenCV库中添加自定义OpenCL函数。

我的第一个目标是创建一个简单的OpenCL内核,使白色图像变黑。

我已将以下函数添加到opencv库

#include "precomp.hpp"
#include "opencl_kernels_imgproc.hpp"
#include "cvconfig.h"

namespace cv
{

    void PeronaMalik(InputArray src, OutputArray dst, double timeStepSize, int noOfTimeSteps)
    {

        Size size = src.size();
        UMat _dst = dst.getUMat();
        UMat _src = src.getUMat();

        //Uncomment to make image black
        //_dst.setTo(Scalar(0)); 

        String sourceCode =        
"__kernel void PeronaMalik"
"("
"    __global const uchar * src, "
"    int src_step, "
"    int src_offset, "
"    int src_rows,"
"    int src_cols,"
"    __global uchar * dst, " 
"    int dst_step, "
"    int dst_offset, "
"    int dst_rows, "
"    int dst_cols "
") "
"{ "
"    const int x = get_global_id(0); "
"    const int y = get_global_id(1); "
"    dst[x + (y*src_cols)] = 0; " //Black
"} ";

        ocl::ProgramSource peronaMalikSource(sourceCode);

        ocl::Kernel peronaMalikKernel
        (
            "PeronaMalik", 
            peronaMalikSource,
            ""
        );

        peronaMalikKernel.args
        (
            ocl::KernelArg::ReadOnly(_src),
            ocl::KernelArg::WriteOnly(_dst)
        );

        size_t globalsize[2] = { (size_t)size.width, (size_t)size.height };

        peronaMalikKernel.run(2, globalsize, NULL, false);

    };
}

我编译了这个并使用以下程序对其进行了测试

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

Mat src, src_gray;
Mat dst;

char* window_name = "Output Window";

int main( int argc, char** argv )
{
  src = imread( argv[1] );

  dst.create( src.size(), src.type() );

  cvtColor( src, src_gray, CV_BGR2GRAY );

  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  dst = Scalar::all(255); //White image

  PeronaMalik(src_gray, dst, 1.0, 1); //Should make image black

  imshow( window_name, dst );

  waitKey(0);

  return 0;
}

我用测试图像作为输入来运行它,但我仍然得到一个白色图像

white

0 个答案:

没有答案