Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

convertTo only changes the depth of the image, not the channels, so rather call it like:

imgA.convertTo(imgB, CV_8U);

then, unfortunately the builtin << operator for cv::Mat can only handle up to 3 channels, so you will need to iterate over the Mat to print it out:

// first, let's add a type to access the Mat:
typedef Vec<uchar,5> Vec5b;

for (int r=0; r<imgB.rows; r++) {
    for (int c=0; c<imgB.cols; c++) {
         Vec5b &pix = imgB.at<Vec5b>(r,c);
         cout << pix[0] << "," << pix[1] << "," << pix[2] << "," <<pix[3] << "," <<pix[4] << ";";
    } 
    cout << endl;
}

convertTo only changes the depth of the image, not the channels, so rather call it like:

imgA.convertTo(imgB, CV_8U);

then, unfortunately the builtin << operator for cv::Mat can only handle up to 3 channels, so if you will need to iterate over the Mat to print it out:access single pixels, you can add a new type for this:

// first, let's add a type to access the Mat:
typedef Vec<uchar,5> Vec5b;

for (int r=0; r<imgB.rows; r++) {
    for (int c=0; c<imgB.cols; c++) {
         Vec5b &pix = imgB.at<Vec5b>(r,c);
         cout << pix[0] << "," << pix[1] << "," << pix[2] << "," <<pix[3] << "," <<pix[4] << ";";
    } 
    cout << endl;
}

but again, you also could simply print the whole Mat:

cout << imgB << endl;

convertTo only changes the depth of the image, not the channels, so rather call it like:

imgA.convertTo(imgB, CV_8U);

if you need to access single pixels, you can add a new type for this:

typedef Vec<uchar,5> Vec5b;

for (int r=0; r<imgB.rows; r++) {
    for (int c=0; c<imgB.cols; c++) {
         Vec5b &pix = imgB.at<Vec5b>(r,c);
         cout << pix[0] <<pix[0] << "," << pix[1] << "," << pix[2] << "," <<pix[3] << "," <<pix[4] << ";";
    } 
    cout << endl;
}

but again, you also could simply print the whole Mat:

cout << imgB << endl;

convertTo only changes the depth of the image, not the channels, so rather call it like:

imgA.convertTo(imgB, CV_8U);

if you need to access single pixels, you can add a new type for this:

typedef Vec<uchar,5> Vec5b;

for (int r=0; r<imgB.rows; r++) {
    for (int c=0; c<imgB.cols; c++) {
         Vec5b &pix = imgB.at<Vec5b>(r,c);
         cout <<pix[0] << "," << pix[1] << "," << pix[2] << "," <<pix[3] << "," <<pix[4] << int(pix[0]) << ","; // without the cast, cout would try to print ascii bytes ;(
         cout << int(pix[1]) << ","
         cout << int(pix[2]) << ","
         cout << int(pix[3]) << ","
         cout << int(pix[4]) << ";";
    } 
    cout << endl;
}

but again, you also could simply print the whole Mat:

cout << imgB << endl;