MATLAB随手记

1. 读写文件

简单读写

fp = fopen('record.txt', 'r');
while ~feof(fp) % 只要没读完
line = fgetl(fp); % 读下一行
if contains(line, 'Hello')
pass
end
end

将rgb保存为yuv文件

%mov2yuv creates a Matlab-Movie from a YUV-File.
% mov2yuv('Filename',mov, format) writes the specified file
% using format for YUV-subsampling.
%
% Filename --> Name of File (e.g. 'Test.yuv')
% mov --> Matlab-Movie
% format --> subsampling rate ('400','411','420','422' or '444')
%
%example: mov2yuv('Test.yuv',mov,'420'); function mov2yuv(File,mov,format) %set factor for UV-sampling
fwidth = 0.5;
fheight= 0.5;
if strcmp(format,'400')
fwidth = 0;
fheight= 0;
elseif strcmp(format,'411')
fwidth = 0.25;
fheight= 1;
elseif strcmp(format,'420')
fwidth = 0.5;
fheight= 0.5;
elseif strcmp(format,'422')
fwidth = 0.5;
fheight= 1;
elseif strcmp(format,'444')
fwidth = 1;
fheight= 1;
else
display('Error: wrong format');
end
%get Resolution and Framenumber
resolution = size(mov(1).cdata);
framenumber = size(mov);
framenumber = framenumber(2); fclose(fopen(File,'w')); %Init File
h = waitbar(0,'Please wait ... ');
%write YUV-Frames
for cntf = 1:1:framenumber
waitbar(cntf/framenumber,h);
YUV = rgb2ycbcr(mov(cntf).cdata);
save_yuv(YUV,File,resolution(2),resolution(1),fheight,fwidth);
end
close(h); %Save YUV-Data to File
function save_yuv(data,video_file,BreiteV,HoeheV,HoehenteilerV,BreitenteilerV) %get Resolution od Data
datasize = size(data);
datasizelength = length(datasize); %open File
fid = fopen(video_file,'a'); %subsampling of U and V
if datasizelength == 2 | HoehenteilerV == 0
%4:0:0
y(1:HoeheV,1:BreiteV) = data(:,:,1);
elseif datasizelength == 3
y(1:HoeheV,1:BreiteV) = double(data(:,:,1));
u(1:HoeheV,1:BreiteV) = double(data(:,:,2));
v(1:HoeheV,1:BreiteV) = double(data(:,:,3));
if BreitenteilerV == 1
%4:1:1
u2 = u;
v2 = v;
elseif HoehenteilerV == 0.5
%4:2:0
u2(1:HoeheV/2,1:BreiteV/2) = u(1:2:end,1:2:end)+u(2:2:end,1:2:end)+u(1:2:end,2:2:end)+u(2:2:end,2:2:end);
u2 = u2/4;
v2(1:HoeheV/2,1:BreiteV/2) = v(1:2:end,1:2:end)+v(2:2:end,1:2:end)+v(1:2:end,2:2:end)+v(2:2:end,2:2:end);
v2 = v2/4;
elseif BreitenteilerV == 0.25
%4:1:1
u2(1:HoeheV,1:BreiteV/4) = u(:,1:4:end)+u(:,2:4:end)+u(:,3:4:end)+u(:,4:4:end);
u2 = u2/4;
v2(1:HoeheV,1:BreiteV/4) = v(:,1:4:end)+v(:,2:4:end)+v(:,3:4:end)+v(:,4:4:end);
v2 = v2/4;
elseif BreitenteilerV == 0.5 & HoehenteilerV == 1
%4:2:2
u2(1:HoeheV,1:BreiteV/2) = u(:,1:2:end)+u(:,2:2:end);
u2 = u2/2;
v2(1:HoeheV,1:BreiteV/2) = v(:,1:2:end)+v(:,2:2:end);
v2 = v2/2;
end
end fwrite(fid,uint8(y'),'uchar'); %writes Y-Data if HoehenteilerV ~= 0
%writes U- and V-Data if no 4:0:0 format
fwrite(fid,uint8(u2'),'uchar');
fwrite(fid,uint8(v2'),'uchar');
end fclose(fid);

从yuv文件中读取Y通道

function [y] = y_import(filename,dims,range_frames,yuvformat)
% Import Y channel from a yuv video.
% dims: [width, height]
% range_frames: 1:nf
% return: a cell (nfs * height * width) %% Read yuv
fid=fopen(filename,'r');
if (fid < 0)
error('File does not exist!');
end %% Fill up default
if (nargin < 4)
yuvformat = 'YUV420_8';
end %% Calculate inprec and sampl
inprec = 'ubit8';
sampl = 420;
if (strcmp(yuvformat,'YUV420_16'))
inprec = 'uint16'; %'ubit16=>uint16'
elseif (strcmp(yuvformat,'YUV444_8'))
sampl = 444;
end %% Calculate frelem
if (sampl == 420)
dimsUV = dims / 2;
else
dimsUV = dims;
end Yd = zeros(dims(1),dims(2));
frelem = numel(Yd) + 2 * dimsUV(1) * dimsUV(2); %% extraction num_frames = length(range_frames);
y = cell(1,num_frames); for ite_frame = 1:num_frames startfrm = range_frames(ite_frame) - 1; % the first frame start from 0 frame.
fseek(fid, startfrm * frelem , -1); % go to the starting frame Yd = fread(fid,dims,inprec);
y{ite_frame} = Yd'; % height * width end fclose(fid); return

将TIFF图片拼接为yuv文件

dir_img = "G:\SCI\Database\RAISE_8k";
image_list = dir(fullfile(dir_img, "*.TIF"));
image_list = {image_list.name}'; num_image = length(image_list);
random_order = randperm(num_image); % randperm(N)将会使得[1 2 ... N]序列打乱并返回该行向量。
index_list_tra = random_order(1:4900);
index_list_val = random_order(4900+1:4900+1628);
index_list_test = random_order(4900+1628+1:8156); tar_w = 1920;
tar_h = 1080; dir_store = ".\Database"; %% Training set
filename = "RAISE-8156_raw_1920x1080_4900_tra.yuv";
fid= fopen(fullfile(dir_store, filename),'w');
nfs = length(index_list_tra);
for i = 1:nfs
disp(string(i) + " | " + string(nfs)); index = index_list_tra(i);
img = Tiff(fullfile(dir_img, image_list(index)),'r');
img = read(img); YUVimg = rgb2ycbcr(img);
%imshow(YUVimg) [imgHeight imgWidth imgDim] = size(YUVimg);
Y = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,1); % Y 矩阵 % Cut to 832x480
U = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,2); % U 矩阵
V = YUVimg(imgHeight/2-(tar_h/2-1):imgHeight/2+tar_h/2,imgWidth/2-(tar_w/2-1):imgWidth/2+tar_w/2,3); % V 矩阵
[imgHeight imgWidth] = size(Y); y = double(Y);
u = double(U);
v = double(V); u2(1:imgHeight/2,1:imgWidth/2) = u(1:2:end,1:2:end)+u(2:2:end,1:2:end)+u(1:2:end,2:2:end)+u(2:2:end,2:2:end);
u2 = u2/4;
v2(1:imgHeight/2,1:imgWidth/2) = v(1:2:end,1:2:end)+v(2:2:end,1:2:end)+v(1:2:end,2:2:end)+v(2:2:end,2:2:end);
v2 = v2/4; %imshow(Y);
%fwrite(fid,yuv420out,'uint8');
fwrite(fid,uint8(y'),'uchar');
fwrite(fid,uint8(u2'),'uchar');
fwrite(fid,uint8(v2'),'uchar');
end
fclose(fid);

2. 字符串操作

  • pos = strfind(str_t, "Hello"):会返回所有搜寻到的Hello的首字母位置。

  • num = str2double(str_t)

3. 画图

  • 依次图例:legend(["a","b","c"])

最新文章

  1. 记一次由于Java泛型类型擦除而导致的问题,及解决办法
  2. 【Python】二分查找算法
  3. Modelsim6.5在Ubuntu12.04的安装过程
  4. php CLI 模式下的传参方法
  5. 半平面交模板(BZOJ1007)
  6. 关于AWR报告命中率指标的解释(转)
  7. 超棒的 15 款 Bootstrap UI 编辑器
  8. Gas Station
  9. C 简单处理excel 转成 json
  10. python 函数默认值的小坑啊
  11. Django自身的CBV列表
  12. C# ToString格式大全
  13. CSS Hack是什么意思
  14. Mysql动态sql语句,用当前时间做表名
  15. Nginx作为HTTP服务器--Nginx配置图片服务器
  16. 设置mysql数据库为只读
  17. Python3学习之路~8.6 开发一个支持多用户在线的FTP程序-代码实现
  18. 原子性 CAS算法
  19. vs感受,由于我的电脑装了俩年了!我直接写感受吧
  20. 个人作业——final

热门文章

  1. Kettle实现从数据库中提取数据到Excel
  2. 关于powermock报错org.powermock.reflect.exceptions.FieldNotFoundException: Field &#39;fTestClass&#39; was not found in class org.junit.internal.runners.MethodValidator.问题解决
  3. spring的事件机制实战
  4. 转载:ubuntu下编译安装nginx及注册服务
  5. 给 K8s API “做减法”:阿里巴巴云原生应用管理的挑战和实践
  6. Web前端——表单提交和Js添加选项
  7. go-GUI-代码
  8. python BeautifulSoup 爬虫运行出现 exited with code -1073741571
  9. MySQL学习——查看数据库信息
  10. SpringBoot(十一):SpringBoot整合Redis