Note | MATLAB
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"])
Note | MATLAB的更多相关文章
- Matlab 绘制三维立体图(以地质异常体为例)
前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...
- Matlab slice方法和包络法绘制三维立体图
前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...
- Matlab 高斯_拉普拉斯滤波器处理医学图像
前言:本程序是我去年实现论文算法时所做.主要功能为标记切割肝脏区域.时间有点久,很多细节已经模糊加上代码做了很多注释,因此在博客中不再详述. NOTE: 程序分几大段功能模块,仔细阅读,对解决医学图像 ...
- 基于暗通道优先算法的去雾应用(Matlab/C++)
基于暗通道优先的单幅图像去雾算法(Matlab/C++) 算法原理: 参见论文:Single Image Haze Removal Using Dark Channel Pri ...
- PCA and kmeans MATLAB实现
MATLAB基础知识 l Imread: 读取图片信息: l axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和 ...
- Interpolation in MATLAB
Mathematics One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...
- ADC测试matlab代码
前面有做过ADC性能测试,测试方式是先使用ADC采集一个单频信号,然后利用matlab进行性能分析. 下面把matlab分析的代码记录下来: %The following program code p ...
- ubuntu14.04下安装cudnn5.1.3,opencv3.0,编译caffe及配置matlab和python接口过程记录
已有条件: ubuntu14.04+cuda7.5+anaconda2(即python2.7)+matlabR2014a 上述已经装好了,开始搭建caffe环境. 1. 装cudnn5.1.3,参照: ...
- [原创]Matlab生成随机数
Matlab中有着丰富的随机数生成函数以应用于不同的情景,我一般使用生成随机的1~N的整数,但是之前了解的只有rand函数,其生成主要为0~1之间的随机数,但是和所预想的有差异.在此进行进行了help ...
随机推荐
- 【洛谷5465】[PKUSC2018] 星际穿越(倍增)
点此看题面 大致题意: 给定\(l_{2\sim n}\),其中\(l_i\)表示\([l_i,i-1]\)的所有点与\(i\)之间存在一条长度为\(1\)的双向路径.每次询问给出\(l,r,x\), ...
- Ubuntu Idea 快捷键 Ctrl+Alt+S 无法使用解决
Idea 里习惯了用 Ctrl+Alt+S 打开设置界面,在 Ubuntu 下会因为快捷键冲突无法使用 系统快捷键 打开系统设置中的快捷键设置,按 Backspace 键禁用 Fcitx 如果你的输入 ...
- CSS旋转动画和动画的拼接
旋转动画 第一个样式: @keyframes rotating { from { transform: rotate(0deg); } to { transform: rotate(360deg); ...
- golang数据结构之递归解决迷宫问题
简单来说:递归就是函数/方法自己调用自己,只是每次传入不同的变量. 递归可以解决各种数学问题:n皇后问题.阶乘问题.汉诺塔.迷宫问题.球和篮子问题等等: maze.go package maze im ...
- 02-Git远程仓库Github
1.Git远程仓库 (Gitgub网站作为远程代码仓库时的操作和本地代码仓库一样的,只是仓库位置不同而已) 需要准备的东西: 1.准备Git源代码仓库https://github.com/ 2.准备李 ...
- gcc 编译安装
wget https://kojipkgs.fedoraproject.org//packages/gcc/7.1.1/1.module_1a179a7b/src/gcc-7.1.1-1.module ...
- C#关闭多线程程序
Process[] processes = System.Diagnostics.Process.GetProcesses(); //获得所有进程 foreach (Process p in proc ...
- 高强度学习训练第十一天总结:Class文件结构(二)
常量池 可以理解为Class文件之中的资源仓库,他是Class文件结构中与其他项目关联最多的数据类型,也是占用Class文件空间最大的数据项目之一 访问标志 在常量池结束后,紧接着的俩个字节代表访问标 ...
- 自定义Vue组件打包、发布到npm以及使用
本文将帮助:将自己写的Vue组件打包到npm进行代码托管,以及正常发布之后如何使用自己的组件. 本文讲述的仅仅是最基础的实现,其他复杂的操作需要非常熟悉webpack的相关知识,作者将继续学习. 先附 ...
- 0基础入门学习Python(第4章)
第四章,了不起的分支和循环 4.1 分支和循环 Python主要依靠缩进来区分代码块 4.2 快速上手 成绩按照分数来划分等级,90分以上为A,80~90 为B,60~80 为C,60以下为D p4_ ...