【问题标题】:How to convert CV_8UC1 to normal int type?如何将 CV_8UC1 转换为普通的 int 类型?
【发布时间】:2011-07-17 15:55:37
【问题描述】:

我在理解 opencv 类型时遇到了问题。我有 CV_8UC1 类型的矩阵,但是如何读取矩阵中元素的值?

我知道我必须使用 at 方法,然后传递 <here> 我的类型,但 CV_8UC1 是什么类型? 8个无符号位单通道并不能告诉我太多。

我可以这样做吗: unsigned int a = mat->at(0,0);

【问题讨论】:

  • CV_8UC1 是一个unsigned char。就投吧。

标签: c++ opencv


【解决方案1】:

来自 OpenCV 参考手册:

CV_8UC1 – unsigned 8-bit single-channel data; can be used for grayscale image or binary image – mask.

所以它只是单通道 8 位灰度,值为 0..255。

【讨论】:

  • 是的,我知道,但是我怎样才能将 0 到 255 之间的值变为正常的 int?因为当我像上面那样做时,我得到了一些疯狂的数字,比如 30044234
  • 您可能想要编辑您的问题以反映您当时真正要问的内容,因为标题说:“opencv 中的 CV_8UC1 类型是什么?”,这意味着你知道 CV_8UC1 是什么意思。
【解决方案2】:

这里有几个例子:

  • P.s stackoverflow 有时不允许使用 >

.

// Char single channel matrix
Mat M1 = mat::ones(3,5,CV_8UC1);
int Val = M1{unsigned char}.at(2,3);

// int 16 matrix with 3 channels (like RGB pixel)
Mat M2 = mat::ones(3,5,CV_16UC(3));
__int16* Pix = &M2.at<__int16>(2,3);
Val = Pix[0] + Pix[1] + Pix[2];

// Example of how to find the size of each element in matrix
switch ( (M.dataend-M.datastart) / (M.cols*M.rows*M.channels())){
case sizeof(char):
     printf("This matrix has 8 bit depth\n");
     break;
case sizeof(double):
     printf("This matrix has 64 bit depth\n");
     break;
}

//Example of how to build dynamically a Matrix with desired depth
int inline CV_BUILD_MATRIX_TYPE(int elementSize, int nChannels){
    // Determine type of the matrix 
    switch (elementSize){
    case sizeof(char):
         return CV_8UC(nChannels);
         break;
    case (2*sizeof(char)):
         return CV_16UC(nChannels);
         break;
    case sizeof(float):
         return CV_32FC(nChannels);
         break;
    case sizeof(double):
         return CV_64FC(nChannels);
         break;
    }
    return -1;
}

Mat M = Mat::zeros(2,3,CV_BUILD_MATRIX_TYPE(4,2)); 
// builds matrix with 2 channels where each channel has depth of 4 bytes (float or __int32). As you wish

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2021-06-10
    相关资源
    最近更新 更多