Search code examples
c++opencvfilterlaplacian

OpenCV CUDA Laplacian Filter on 3 channel image


I am wanting to apply a laplacian filter onto an image using OpenCV's CUDA (current version 4.3.0) namespace. Currently the CUDA version of laplacian filter does not accept 3 channel images:

https://docs.opencv.org/4.3.0/dc/d66/group__cudafilters.html#gabf85fe61958bb21e93211a6fcc7c5c3b

My thought on this was to split the channels, apply the filter individually, and merge back together. However I am getting an error when attempting to apply the filter to the individual channels.

My first issue arose from the type() cast in the create filter function. I manually set that to CV_8U to fix that issue but then run into another issue at applying the filter. Am I doing this incorrectly?

//Setup  GPU and GPU Mats
cv::cuda::setDevice(0);
cv::cuda::GpuMat src, dst;
std::vector <cv::cuda::GpuMat> channels, lines;
src.upload(ImageCV);

//Start stream
cv::cuda::Stream stream1;

//Split channels
cv::cuda::split(src, channels, stream1);

//Filter image
//FAILING HERE
cv::Ptr<cv::cuda::Filter> lapFilter0 = cv::cuda::createLaplacianFilter(channels[0].type(), lines[0].type(), cv::Size(Sz, Sz));
cv::Ptr<cv::cuda::Filter> lapFilter1 = cv::cuda::createLaplacianFilter(channels[1].type(), lines[1].type(), cv::Size(Sz, Sz));
cv::Ptr<cv::cuda::Filter> lapFilter0 = cv::cuda::createLaplacianFilter(channels[2].type(), lines[2].type(), cv::Size(Sz, Sz));

//Apply Filter
//FAILING HERE
lapFilter0 ->apply(channels[0], lines[0], stream1);
lapFilter1 ->apply(channels[1], lines[1], stream1);
lapFilter2 ->apply(channels[2], lines[2], stream1);

//Merge channels
cv::cuda::merge(lines, dst, stream1);
dst.download(ImageCV);

Solution

  • It was a simple fix for a simple mistake :)

    Needed to allocate channels and lines:

    std::vector <cv::cuda::GpuMat> channels(3), lines(3);