c - 在 OpenCV 中打印 CV_8UC4 矩阵类型的值

标签 c opencv image-processing

我正在尝试打印作为 CV_8UC4 类型图像文件输入的矩阵的值。我的代码写在下面。我的问题是我得到的值只有 48 和 255,而我期望的值是 2 和 1 或其他值。有人知道为什么吗?我想我以错误的方式打印值。有没有其他方法可以打印 CV_8UC4 的值。 我的代码是:

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");

 }

我的案例是 24,我得到的连线值是 48 和 255。是否有其他方法可以打印 CV_8UC4 值?

最佳答案

正确访问您的CV_8UC4矩阵:

    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];

关于c - 在 OpenCV 中打印 CV_8UC4 矩阵类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741499/

相关文章:

c - 如何在 C 中返回十六进制字符串?

c - csc 中 spmv 的 openmp 并行化

opencv - 查找 cv::Mat 的字节大小

python - 如何从直方图均衡图像中去除噪声?

opencv - 背景减法 + MOOSE 滤波器

c - 你用什么来单元测试 C 代码?

c - 输入 unsigned long long int 是否足够 1 - 10^9 范围?

c++ - OpenCV error-cvtColor require 1-channel image 以及如何修复它?

ios - Opencv自定义编译

c++ - 避免 uint32_t 幂指数的 LUT