opencv c++读取raw文件格式的深度图,提取某像素点的深度信息

opencv读取raw文件的深度图,并提取深度信息

	int width = 1536;
	int height = 1280;
	const int CHANNELS = 1; // 1个通道,因为是单通道深度图

	cv::Mat depthImage(height, width, CV_16SC1);

	// 打开raw文件
	std::ifstream file("./image/Depth/20240730195736_W[1536]_H[1280].raw", std::ios::binary);
	if (!file) {
		std::cerr << "无法打开文件: " << "./image/Depth.raw" << std::endl;
		return;
	}

	std::cout << "sizeof(int16_t): " << sizeof(int16_t) << std::endl;

	// 读取数据到Mat对象
	file.read(reinterpret_cast<char*>(depthImage.data), width * height * sizeof(int16_t));

	file.close();

	if (depthImage.empty())
	{
		return;
	}

	// 打印图像的基本信息
	std::cout << "Image size: " << depthImage.size() << std::endl;
	std::cout << "Image type: " << depthImage.type() << std::endl;

	int16_t depth = depthImage.at<int16_t>(753, 793);
	std::cout << "Depth at (" << 753 << ", " << 793 << "): " << depth << " mm" << std::endl;
可以使用 C++ 编程语言结合 Qt 和 OpenCV 库来实现对 raw 文件长、宽、深度信息获取。以下是一个简单的代码示例: ```c++ #include <iostream> #include <fstream> #include <opencv2/opencv.hpp> #include <QCoreApplication> using namespace std; using namespace cv; const int WIDTH = 512; // 宽度 const int HEIGHT = 512; // 高度 const int DEPTH = 40; // 深度 int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 读取数据 ifstream fin("data.raw", ios::binary); // 打开二进制文件 if (!fin) { cerr << "Failed to open file!" << endl; return 1; } unsigned short data[WIDTH][HEIGHT][DEPTH]; fin.read(reinterpret_cast<char*>(&data[0][0][0]), WIDTH * HEIGHT * DEPTH * sizeof(unsigned short)); fin.close(); // 关闭文件 // 转换为 OpenCV Mat 格式 Mat volume(DEPTH, HEIGHT, WIDTH, CV_16UC1, reinterpret_cast<void*>(&data[0][0][0])); // 输出信息 cout << "Width: " << volume.size[2] << endl; // 宽度 cout << "Height: " << volume.size[1] << endl; // 高度 cout << "Depth: " << volume.size[0] << endl; // 深度 return a.exec(); } ``` 这段代码假设了数据类型为无符号 16 位整型,数据尺寸为 512x512x40,数据存储顺序为行优先。首先打开文件,然后使用 ifstream 类的 read 函数读取数据。接着使用 OpenCV 中的 Mat 类来将数据转换为 OpenCV Mat 格式。最后输出长、宽、深度信息。 这段代码需要在 Qt 项目中使用,需要将 OpenCV 库和相关头文件添加到项目中,并使用 QCoreApplication 类来运行程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值