【问题标题】:OpenCV 2D-Array to Image ConversionOpenCV 2D-Array 到图像的转换
【发布时间】:2017-03-06 04:20:58
【问题描述】:

当我尝试将 2D 数组转换为图像时,行和列都被扭曲了。无法制作相同的图像。

using namespace cv;
using namespace std;


int main()
{
     float val;
     int val1[3][3];

     Mat imm = (Mat_<float>(3,3) << 12, 13, 45, 11, 191, 255, 62, 27, 0);
     if (imm.empty()) //check whether the image is loaded or not
     {
         cout << "Error : Image cannot be loaded..!!" << endl;
         //system("pause"); //wait for a key press
         return -1;
     }

     for (int y = 0; y < imm.rows; ++y)
     {
         float* row_ptr = imm.ptr<float>(y);
         for (int x = 0; x < imm.cols; ++x)
         {
              val = row_ptr[x];
              val1[y][x]=val;
              if(x%3==0)
              cout<<"\n";
              cout<<val<<"\t";

         }
     }


     Mat out_img(3,3,CV_8U ,val1);
     cout<<"\n\n\n output= "<<out_img;
     cout<<"\n\n\n\n"<<"Input= "<<imm;
 }

我们得到的输出是,

12  13  45  
11  191 255 
62  27  0   


 output= [ 12,   0,   0;
   0,  13,   0;
   0,   0,  45]



Input= [12, 13, 45;
 11, 191, 255;
 62, 27, 0]

我们可以看到输出和输入不一样。因此需要帮助重建相同的数据。

谢谢。

【问题讨论】:

    标签: c++ arrays opencv pointers


    【解决方案1】:

    输出失真的原因是您试图将 2d int(32 位)数组放入 unsigned char(8 位)垫中。要更正此问题,您必须将 mat type(CV_8U) 更改为 CV_32S 或将 2d 数组更改为 unsigned char

    【讨论】:

      【解决方案2】:
      Mat im2;
      imm.convertTo(CV_8U, im2);
      

      这将为您提供与 uchar 相同的图像。无需使用双 for 循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-02
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-04
        • 2012-05-09
        相关资源
        最近更新 更多