开发者

Bug in OpenCV2.3 cv::split() function. Identical values in all 3 channels

开发者 https://www.devze.com 2023-04-04 16:23 出处:网络
After spending a couple of days trying figure out why opencv DFT would give 100% similar results for all three channels I ended up finding out that there might be a bug in the split() function that Op

After spending a couple of days trying figure out why opencv DFT would give 100% similar results for all three channels I ended up finding out that there might be a bug in the split() function that OpenCV provides for splitting a input image to 3 single channel images.

std::vector<cv::Mat> rgbChannels(3,cv::Mat(inputImage.size(),CV_64FC1));
cv::split(inputImage, rgbChannels);

After saving the image values onto disk and using a file differencing tool, I found out that all values in the split channels were identical.

Have I done something wrong?

My work around was the following function. But that also gave me the exact identical values, giving me a hint that somehow vectors were not being handled correctly by OpenCV.

SplitImage(cv::Mat inputImage开发者_高级运维)
{
    //copy original in BGR order
    std::vector<cv::Mat> splittedImage(3,cv::Mat(inputImage.size(),CV_64FC1));

        cv::Mat tempImage(inputImage.size(),CV_64FC1);
        for (int row = 0; row < inputImage.size().height; row++)
        {
            for (int col = 0; col < inputImage.size().width; col++)
            {               
splittedImage[0].at<double>(row, col) = inputImage.at<cv::Vec3d>(row, col)[0];
splittedImage[1].at<double>(row, col) = inputImage.at<cv::Vec3d>(row, col)[1];
splittedImage[2].at<double>(row, col) = inputImage.at<cv::Vec3d>(row, col)[2];
            }
        }
    return splittedImage;
}

And finally wrote the following to solve the problem

SplitImage(cv::Mat inputImage)
{
    //copy original in BGR order
    std::vector<cv::Mat> splittedImage(3,cv::Mat(inputImage.size(),CV_64FC1));
    std::vector<cv::Mat>::iterator it;
    it = splittedImage.begin();

    for(int channelNo = 0; channelNo < inputImage.channels(); channelNo++)
    {
        cv::Mat tempImage(inputImage.size(),CV_64FC1);
        for (int row = 0; row < inputImage.size().height; row++)
        {
            for (int col = 0; col < inputImage.size().width; col++)
            {
    tempImage.at<double>(row, col) = inputImage.at<cv::Vec3d>(row, col)[channelNo];
            }
        }

  it = splittedImage.insert ( it , tempImage );     
  it++;
    }
    return splittedImage;
}

Has anyone had a problem with split() function or have I done something wrong?


It is not a bug in OpenCV but there is a problem with your code.

The following line does not create a vector of 3 different Mats:

std::vector<cv::Mat> rgbChannels(3,cv::Mat(inputImage.size(),CV_64FC1));

Instead, this line produces a vector of 3 Mat headers sharing the same memory. It works this way because Mat copy constructor does not make a deep copy - it just increments an internal reference counter.

Just change your code to the following to solve your problem:

std::vector<cv::Mat> rgbChannels(3);
cv::split(inputImage, rgbChannels);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号