user227666
user227666

Reputation: 774

print values of CV_8UC4 matrix type in OpenCV

I am trying to print the values of a matrix taken as input from an image file, which is of type CV_8UC4. My code is written below. My problem is I am getting values of only 48 and 255 whereas I was expecting values in 2 and 1 or something.Does anybody knows why? I think I am printing the values in wrong way. Is there any other way to print values of CV_8UC4. My code is:

CvMat* m1 = cvLoadImageM(argv[1], CV_LOAD_IMAGE_UNCHANGED); // load the matrix from an image file
cv::Mat m1cpp(m1);
int type = m1cpp.type();
printf( " type is %d  ", type );

switch( type ) {

 case 24:
   printf("\n --> Matrix type is CV_8U \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %d  ", m1cpp.at<uchar>(i,j) );
     } printf("\n");
   }
 break;


 case 25:
   printf("\n --> Matrix type is CV_8S \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %d  ", m1cpp.at<schar>(i,j) );
     } printf("\n");
   }
 break;


 case 18:
   printf("\n --> Matrix type is CV_16U \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %d  ", m1cpp.at<ushort>(i,j) );
     } printf("\n");
   }
 break;


 case 27:
   printf("\n --> Matrix type is CV_16S \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %d  ", m1cpp.at<short>(i,j) );
     } printf("\n");
   }
 break;


 case 28:
   printf("\n --> Matrix type is CV_32S \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %d  ", m1cpp.at<int>(i,j) );
     } printf("\n");
   }
 break;

 case 29:
   printf("\n --> Matrix type is CV_32F \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %.3f  ", m1cpp.at<float>(i,j) );
     } printf("\n");
   }
 break;


 case 30:
   printf("\n --> Matrix type is CV_64F \n");

   for( size_t i = 0; i < m1cpp.rows; i++ ) {
     for( size_t j = 0; j < m1cpp.cols; j++ ) {
       printf( " %.3f  ", m1cpp.at<double>(i,j) );
     } printf("\n");
   }
  break;

   default:
printf("\n --> Matrix type not found \n");

 }

My case is coming as 24 and I am getting wired values as 48 and 255. Is there some other way to print CV_8UC4 values?

Upvotes: 1

Views: 1322

Answers (1)

LovaBill
LovaBill

Reputation: 5139

Access your CV_8UC4 matrix properly:

    image.at<cv::Vec4b>(j,i)[0];
    image.at<cv::Vec4b>(j,i)[1];
    image.at<cv::Vec4b>(j,i)[2];
    image.at<cv::Vec4b>(j,i)[3];

Upvotes: 5

Related Questions