OpenCV error-cvtColor require 1-channel image and how to fix it?

I am new to C++ and OpenCV, my current project require some convert from Matrix to grayscale image.
Here i used the function cvtColor, but for some reason it keep crashing cause the error : “OpenCV error: Assertion failed(scn==3 | scn==4) in cv::cvtColor”.
Here my source code:

for (int i = 0; i < 2048; i++)
{
    for (int j = 0; j < 2048; j++)
    {
        t[i][j] = abs((L3[i][j] - L_blurred_3[i][j]) / (sqrt(L2_blurred[i][j] - L_blurred_4[i][j])));
    }
}
Mat tt(2048, 2048, CV_64F, Scalar(0));

for (int i = 0; i < 2048; i++)
{
    for (int j = 0; j < 2048; j++)
    {
        tt.at<double>(i, j) = t[i][j];
    }
}
tt.convertTo(tt, CV_32F);
Mat tgray(2048, 2048, CV_32F, Scalar(0));
cvtColor(tt, tgray, COLOR_BGR2GRAY);

i did make some search on site and google and i have tried many different ways but the result still the same, i know that the problem is my matrix “tt” is a one channel image, so i have 2 main questions:

  1. How to fix this assertion error? Do i need to convert tt to 3 or 4 channels? and how?
  2. Is there any other function that do the same work for 1 channel image?


Source: c++

Leave a Reply