MATLAB随手记
1. 读写文件
简单读写
fp = fopen('record.txt', 'r');
while ~feof(fp)
line = fgetl(fp);
if contains(line, 'Hello')
pass
end
end
将rgb保存为yuv文件
function mov2yuv(File,mov,format)
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
resolution = size(mov(1).cdata);
framenumber = size(mov);
framenumber = framenumber(2);
fclose(fopen(File,'w'));
h = waitbar(0,'Please wait ... ');
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);
function save_yuv(data,video_file,BreiteV,HoeheV,HoehenteilerV,BreitenteilerV)
datasize = size(data);
datasizelength = length(datasize);
fid = fopen(video_file,'a');
if datasizelength == 2 | HoehenteilerV == 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
u2 = u;
v2 = v;
elseif HoehenteilerV == 0.5
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
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
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');
if HoehenteilerV ~= 0
fwrite(fid,uint8(u2'),'uchar');
fwrite(fid,uint8(v2'),'uchar');
end
fclose(fid);
从yuv文件中读取Y通道
function [y] = y_import(filename,dims,range_frames,yuvformat)
fid=fopen(filename,'r');
if (fid < 0)
error('File does not exist!');
end
if (nargin < 4)
yuvformat = 'YUV420_8';
end
inprec = 'ubit8';
sampl = 420;
if (strcmp(yuvformat,'YUV420_16'))
inprec = 'uint16';
elseif (strcmp(yuvformat,'YUV444_8'))
sampl = 444;
end
if (sampl == 420)
dimsUV = dims / 2;
else
dimsUV = dims;
end
Yd = zeros(dims(1),dims(2));
frelem = numel(Yd) + 2 * dimsUV(1) * dimsUV(2);
num_frames = length(range_frames);
y = cell(1,num_frames);
for ite_frame = 1:num_frames
startfrm = range_frames(ite_frame) - 1;
fseek(fid, startfrm * frelem , -1);
Yd = fread(fid,dims,inprec);
y{ite_frame} = Yd';
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);
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";
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);
[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);
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);
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);
[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;
fwrite(fid,uint8(y'),'uchar');
fwrite(fid,uint8(u2'),'uchar');
fwrite(fid,uint8(v2'),'uchar');
end
fclose(fid);
2. 字符串操作
3. 画图
- 依次图例:
legend(["a","b","c"])
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步