垫子类型转换和访问单个像素时出现问题
Trouble with mat type conversion and accessing individual pixel
我在用OpenCV编程时遇到了麻烦。
经过很多时间,我发现,类型转换后,cout <<垫的结果和单个像素值是不同的。
这是代码
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat a = (Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
cout << "Initial mat type: " << a.type() << endl;
cout << "Pos(1, 1): " << a.at<int>(1, 1) << endl;
a.convertTo(a, CV_8U);
cout << "CV_8U converted mat type: " << a.type() << endl;
cout << "Mat content: n" << a << endl;
cout << "Pos(1, 1): " << a.at<int>(1, 1) << endl;
return 0;
}
结果在这里:
Initial mat type: 4 // CV_32S
Pos(1, 1): 5
CV_8U converted mat type: 0 // CV_8U
Mat content:
[1, 2, 3;
4, 5, 6;
7, 8, 9]
Pos(1, 1): -1254749944
这意味着,从 CV_32S 转换为 CV_8U 后,我从 cout <<a 中获得了正确的矩阵,但是在访问单个像素时,我弄得一团糟:|
你可以帮我吗?谢谢!
由于已将值转换为其他类型,因此需要使用其他类型访问它们:
cout << "Pos(1, 1): " << static_cast<int>(a.at<uchar>(1, 1)) << endl;
相关文章:
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 如何将CMSampleBufferRef/CIImage/UIImage转换为像素,例如uint8_t[]
- 在C++中查找像素 RGB 数据的最快方法是什么?
- 是否可以从 OpenGL 缓冲区获取原始大小的像素?
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 如何使用 sdl2 快速绘制像素网格?
- 如何在C++中进行像素操作? - Linux
- 他们如何将红外锁定像素转换为镜头前方 1m 的正常平面上的位置
- 访问随机图像像素的快速方法,最多一次
- 使用 ImageMagick 从像素强度数组创建灰度图像时遇到问题
- 在 DirectX 11 中添加更多 Texture2D 作为像素着色器资源时遇到问题
- openCV中的颜色检测-像素检查问题
- 在c++中处理图像和像素值时出现问题
- 垫子类型转换和访问单个像素时出现问题
- 像素着色器中的 c++ DirectX 照明问题
- 创建1像素的纹理问题
- GLSL每像素照明问题