c++读取raw文件

最近找工作叫我笔试处理raw的图像文件,然后把图像保存成视频,简单记录一下C++的打开方式吧

#include "stdafx.h"
#include "Read_Raw.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include<iostream>
#include<fstream>
using namespace std;
using namespace cv;

Read_Raw::Read_Raw()
{
}


Read_Raw::~Read_Raw()
{
}
//主要读取程序
cv::Mat Read_Raw::R_Read(cv::String imageName)
{
		FILE *filePointer;

		fopen_s(&filePointer, imageName.c_str(), "rb+");
		if (filePointer == NULL)
		{
			std::cout << "Can not open the raw data!\n";
			return cv::Mat();
		}
		//前4个数据都是int32类型的
		int data[4];
		fread(data,2,8, filePointer);//4个字节 4个数据
		/*
		fread(
		void*  _Buffer,输出数组
		size_t _ElementSize,信息块大小(字节为单位)
		size_t _ElementCount,信息块数量
		FILE*  _Stream 文件读取流
		);
		*/

		int Width = data[0];//宽
		int Height = data[1];//高
		int Depth = data[2];//深度
		int pixelsz = data[3];//字节数

/*		fclose(filePointer);*/
		cout << data[0] << endl;
		cout << data[1] << endl;
		cout << data[2] << endl;
		cout << data[3] << endl;
		cv::Mat rawData(512, 640, CV_16UC1, cv::Scalar(0));


		if (pixelsz == 1)
		{
			cv::Mat rawData(Height, Width, CV_8UC1, cv::Scalar(0));
			for (int i = 0; i < Height; i++)
			{
				uchar *pointer = rawData.ptr<uchar>(i);
				fread(pointer, 1, Width, filePointer);
			}
			fclose(filePointer);
			return rawData;
		}
		else
		{
			
			for (int i = 0; i < 512; i++)
			{
				uint *pointer = rawData.ptr<uint>(i);
				fread(pointer, 2, 640, filePointer);
			}
			fclose(filePointer);
// 			cv::imshow("test",rawData);
// 			waitKey(0);
			return rawData;
		}
		
		
		
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
.raw文件实际上就是一种二进制文件,在C++读取.raw文件的方法与读取其他类型的二进制文件非常类似,只需要使用 ifstream 类库中的 open() 和 read() 函数即可。 假设我们要读取一个名为 "example.raw" 的.raw文件,并且文件中存储的是一个大小为 256x256 的灰度图像,每个像素点占用一个字节。那么我们可以按照下面的步骤来读取: 1. 打开文件:使用 ifstream 类库中的 open() 函数打开.raw文件,并指定文件名和打开模式(ios::binary)。 2. 读取文件:使用 ifstream 类库中的 read() 函数读取文件内容,并将其存储在一个名为 buffer 的 char 数组中,数组的大小应该等于图像大小(256x256)乘以每个像素点占用的字节数(1)。 3. 关闭文件:使用 ifstream 类库中的 close() 函数关闭文件。 下面是一个示例代码: ```C++ #include <iostream> #include <fstream> using namespace std; const int IMG_WIDTH = 256; const int IMG_HEIGHT = 256; int main() { // 打开.raw文件 ifstream infile("example.raw", ios::binary); // 读取文件内容 char buffer[IMG_WIDTH * IMG_HEIGHT]; infile.read(buffer, sizeof(buffer)); // 关闭文件 infile.close(); // 输出读取的内容 for (int i = 0; i < IMG_HEIGHT; i++) { for (int j = 0; j < IMG_WIDTH; j++) { int index = i * IMG_WIDTH + j; unsigned char pixel = (unsigned char)buffer[index]; cout << (int)pixel << " "; } cout << endl; } return 0; } ``` 在上面的代码中,我们首先定义了图像的宽度和高度(分别为 256),然后使用 ifstream 类库中的 open() 函数打开了一个名为 "example.raw" 的.raw文件,并指定了打开模式为 ios::binary。接着,我们使用 ifstream 类库中的 read() 函数读取文件内容,并将其存储在一个名为 buffer 的 char 数组中,数组的大小为图像大小(256x256)乘以每个像素点占用的字节数(1)。最后,我们使用 ifstream 类库中的 close() 函数关闭了文件,并输出了读取的内容。注意,我们在输出像素值时将 char 类型的像素值转换为了 unsigned char 类型,并将其强制转换为了 int 类型以便输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao张的da世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值