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

asked 2020-10-07 05:14:28 -0500

v.suri gravatar image

updated 2020-10-07 07:18:55 -0500

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());
                      }
               }
     }
edit retag flag offensive close merge delete

Comments

i don't think took a look at the importer, and 2 channel images are not supported (you also cannot save one)

berak gravatar imageberak ( 2020-10-07 05:43:01 -0500 )edit

but cv::split() doesn't split the images properly. I would like to know if I am doing anything wrong here

please show code

berak gravatar imageberak ( 2020-10-07 07:06:43 -0500 )edit
1

@berak : Added the snippet of code

v.suri gravatar imagev.suri ( 2020-10-07 07:18:58 -0500 )edit

and btw, what's the depth ? uchar ? (importer takes different code-paths for different depths)

berak gravatar imageberak ( 2020-10-07 07:29:35 -0500 )edit
1

@berak: the depth of the image is 8 bit. don't understand what exactly is uchar?

v.suri gravatar imagev.suri ( 2020-10-07 07:33:57 -0500 )edit

uchar == 8bit unsigned, and thank you ;)

berak gravatar imageberak ( 2020-10-07 07:45:13 -0500 )edit