如何将CV_8UC1转换为普通int类型
How to convert CV_8UC1 to normal int type?
我在理解opencv类型时遇到了问题。我有矩阵在CV_8UC1类型,但我怎么能读取矩阵元素的值?
我知道我必须使用at方法,然后通过<here>
我的类型,但CV_8UC1是什么类型?8位无符号位单通道不能告诉我太多。
我可以这样做吗?Unsigned int a = mat->at(0,0);
来自OpenCV参考手册:
CV_8UC1 – unsigned 8-bit single-channel data; can be used for grayscale image or binary image – mask.
所以它只是单通道8位灰度值0到255
下面是几个例子:
- P。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 depthn");
break;
case sizeof(double):
printf("This matrix has 64 bit depthn");
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
相关文章:
- 为什么 int 类型的枚举类值不能用作 int
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 比较两个整数在C++中与未知 int 类型的相等性
- 二进制'operator+' 'int*'和'int*'类型的无效操作数
- 使用元编程选择 int 类型,将生成错误
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 复制初始化 - 从 'int' 类型转换为非标量类型
- 初始化cv :: mat ::具有int类型的定义大小的人
- 处理代码重新定义int类型(uint16_t,int16_t等),并不喜欢它
- 为什么没有"int"类型声明时输出不同?
- 在大型程序中将长变量更改为 int 类型
- 无法将 int 类型转换为时间类型(我的类类型)
- C++:无法在按位函数后将 int 类型设置为非标量
- 无法分配给INT类型的成员[10]
- C++ cin 将字符串读取到 int 类型中返回 0
- 为什么 decltype((i)) 是引用类型,而 decltype(i+0) 是 int 类型
- 使用4用于循环的4矩阵上的数组下标的“ int [int]类型”
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- 正在施放带有“ Int”类型的临时性