Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

image.channels() not counting correctly for 2 channel tif images.

Hi, I am newbie to using opencv with c++. I have a code where I read tif images and count the number of channels and number of layers in the tif image. I have used Mat.channels() to calculate the number of channels in the image and if the channels are more than 1, then I have used cv::split() to split the image into corresponsing channel images. The code works perfectly for 1 channel and 3 channel images. But for 2 channel images (I have also cross verified and confirmed that the image is 2 channel with Halcon), Mat.channels() gives the output as 1 and for 4 channel images, Mat.channels() gives 4 but cv::split() doesn't split the images properly. I would like to know if I am doing anything wrong here and why this is happening.

Any help or input would be greatly appreciated.

image.channels() not counting correctly for 2 channel tif images.

Hi, I am newbie to using opencv with c++. I have a code where I read tif images and count the number of channels and number of layers in the tif image. I have used Mat.channels() to calculate the number of channels in the image and if the channels are more than 1, then I have used cv::split() to split the image into corresponsing channel images. The code works perfectly for 1 channel and 3 channel images. But for 2 channel images (I have also cross verified and confirmed that the image is 2 channel with Halcon), Mat.channels() gives the output as 1 and for 4 channel images, Mat.channels() gives 4 but cv::split() doesn't split the images properly. I would like to know if I am doing anything wrong here and why this is happening.

Any help or input would be greatly appreciated.

Edit: Code snippet added

       for (int i = 0; i < count; i++) //Count is the number of tif images in the folder
       {
           images.push_back(imread(fn[i], cv::IMREAD_UNCHANGED)) //fn takes the tif images from folder using glob
           fileName = getFileExtension(fn[i], false); //fnc to get filename
           imshow("Picture", images[i]);
           cout << "The total number of channels in the image " << fileName << " is: " << images[i].channels() << endl; 
           fs::create_directory(path / "Converted_Images"); //folder to store converted images

           if (images[i].channels() > 1) // only if greater than 1 split the images
           {
             split(images[i], destination);
             for (int j = 0; j < images[i].channels(); j++)
             {
                 name << filePath << "\\" << "Converted_Images" << "\\" << fileName << "_L" << j + 1 << ".tif";
                 cout << name.str() << endl;
                 imwrite(name.str(), destination[j]);
                 name.str(std::string());
                      }
               }
     }