序列帧视频raw格式数据的存储和读取

常用的raw格式有12位和14位之分。本次实验是将16位的帧序列图片存储到raw二进制文件中,在从读取raw文件成16位的序列图片。

直接上代码!!

raw格式文件的保存:

#include<opencv2\opencv.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include<math.h>
using namespace std;
using namespace cv;
int num = 0;
int main()
{
    std::vector<int>compression_params;
	compression_params.push_back(IMWRITE_PNG_COMPRESSION);   //CV_IMWRITE_PNG_COMPRESSION
	compression_params.push_back(9);///无压缩png
    
    ofstream out_RAW;
	out_RAW.open(rawFile_name, ios::out | ios::app | ios::binary);

	if (!out_RAW.is_open())
	{
		cout << "read file error!" << endl;
		return 0;
	}
    int imageh = 200;  //图片大小
	int imagew = 200;
    string file_name="...\\image"+to_string(num)+".png";
    string Raw_Name="...\\imageRaw.raw";
    for(;num<1000;num++)
    {
         string file_name="...\\image"+to_string(num)+".png";
         Mat Image=imread(file_name,2);
        out_RAW.write((char*)Image.data, 2 * imageh*imagew);
		out_RAW.flush();
		Sleep(1);
    }
   return 0;
}

raw文件的读取到png图片

#include<opencv2\opencv.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;
int num = 0;
int main()
{
    std::vector<int>compression_params;
	compression_params.push_back(IMWRITE_PNG_COMPRESSION);   //CV_IMWRITE_PNG_COMPRESSION
	compression_params.push_back(9);///无压缩png

    int imageh = 200;
	int imagew = 200;
    string filename = "...\\image_Raw.raw";
    ifstream inFile(filename , ios::in | ios::binary);
	if (!inFile)
	{
		cout << "read file error!" << endl;
		return 1;
	}
    for(;num<1000;num++)
    {
        string image_name="...\\image_"+to_string(num)+".png";
        Mat image(imagew, imageh, CV_16UC1);
		inFile.read((char*)image.data, 2 * imageh*imagew);
        imwrite(image_name,image,compression_params);
    }
    return 0;
}

 

### 如何使用Python解析RAW格式数据 #### 使用 `rawpy` 解析相机 RAW 图像数据 对于相机的 RAW 数据文件,可以利用 `rawpy` 库来读取处理这些文件。该库支持多种 RAW 文件格式,并提供了丰富的接口用于调整图像参数。以下是基本的操作方法: ```python import rawpy import imageio # 打开 RAW 文件 with rawpy.imread('example.nef') as raw: # 调整白平衡其他参数 rgb = raw.postprocess(gamma=(1, 1), no_auto_bright=True, output_bps=8) # 将处理后的 RGB 数组保存为 PNG 文件 imageio.imwrite('output.png', rgb) ``` 上述代码展示了如何加载一个 `.nef` 格式RAW 文件并将其转换为标准的 RGB 图像[^1]。 --- #### 利用 MNE-Python 处理 EEG 或其他生物信号中的 Raw 数据 MNE 是一个专门用于脑电图 (EEG) 磁脑图 (MEG) 数据分析的强大工具包。它能够轻松地从各种格式的原始数据中提取事件标记或其他元信息。下面是一个简单的例子展示如何加载打印标注信息: ```python import mne from pathlib import Path data_path = Path(mne.datasets.sample.data_path()) raw_fname = data_path / 'MEG' / 'sample' / 'sample_audvis_raw.fif' # 加载原始数据 raw = mne.io.read_raw_fif(raw_fname, preload=True) # 查看 annotations print(raw.annotations) ``` 此脚本会输出存储在 FIF 文件内的所有时间戳及其对应的描述标签[^2]。 --- #### 提取视频帧作为像素级时序数据集 当涉及到视频流或者连续捕获的画面序列时,可能需要逐帧抽取特定位置上的亮度变化情况形成训练样本集合。这里给出一种实现方式: ```python class VideoDataset(torch.utils.data.Dataset): def __init__(self, video_file, target_pixel_coords=None): self.video_capture = cv2.VideoCapture(video_file) self.target_pixel_coords = target_pixel_coords or [(0, 0)] frame_count = int(self.video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) self.samples = np.zeros((frame_count, len(self.target_pixel_coords))) def __len__(self): return len(self.samples) def __getitem__(self, idx): ret, frame = self.video_capture.read() if not ret: raise IndexError(f"Frame index {idx} out of range.") values_at_points = [ frame[y][x].astype(float).mean() for x, y in self.target_pixel_coords] self.samples[idx] = values_at_points label_value = sum(values_at_points)/len(values_at_points) return torch.tensor(self.samples[idx]), torch.tensor([label_value]) ``` 通过自定义类继承 PyTorch 的 Dataset 类型,我们可以灵活选取关心区域进而构建适合机器学习模型输入形式的时间维度特征向量[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值