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的更多相关文章

  1. Matlab 绘制三维立体图(以地质异常体为例)

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  2. Matlab slice方法和包络法绘制三维立体图

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  3. Matlab 高斯_拉普拉斯滤波器处理医学图像

    前言:本程序是我去年实现论文算法时所做.主要功能为标记切割肝脏区域.时间有点久,很多细节已经模糊加上代码做了很多注释,因此在博客中不再详述. NOTE: 程序分几大段功能模块,仔细阅读,对解决医学图像 ...

  4. 基于暗通道优先算法的去雾应用(Matlab/C++)

    基于暗通道优先的单幅图像去雾算法(Matlab/C++) 算法原理:             参见论文:Single Image Haze Removal Using Dark Channel Pri ...

  5. PCA and kmeans MATLAB实现

    MATLAB基础知识 l  Imread:  读取图片信息: l  axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和  ...

  6. Interpolation in MATLAB

    Mathematics     One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...

  7. ADC测试matlab代码

    前面有做过ADC性能测试,测试方式是先使用ADC采集一个单频信号,然后利用matlab进行性能分析. 下面把matlab分析的代码记录下来: %The following program code p ...

  8. ubuntu14.04下安装cudnn5.1.3,opencv3.0,编译caffe及配置matlab和python接口过程记录

    已有条件: ubuntu14.04+cuda7.5+anaconda2(即python2.7)+matlabR2014a 上述已经装好了,开始搭建caffe环境. 1. 装cudnn5.1.3,参照: ...

  9. [原创]Matlab生成随机数

    Matlab中有着丰富的随机数生成函数以应用于不同的情景,我一般使用生成随机的1~N的整数,但是之前了解的只有rand函数,其生成主要为0~1之间的随机数,但是和所预想的有差异.在此进行进行了help ...

随机推荐

  1. 构建LVS负载均衡集群——NAT模式(最简单方式)

    一.装备一台lvs调度器主机要求两个网卡一个为内部局域网ip,一个为公网ip #IP地址设置过程不再重复 [root@localhost ~]# ip a | grep eth0 #内网ip : et ...

  2. 【PAT甲级】Public Bike Management 题解

    题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourist ...

  3. Python与用户交互

    目录 一.为什么交互? 二.如何交互? 三.Python2的交互 一.为什么交互?   让我们来回顾计算机的发明有何意义,计算机的发明是为了奴役计算机,解放劳动力.假设我们现在写了一个ATM系统取代了 ...

  4. 抓包工具之fiddler实战2-设置断点

    Fiddler作为抓工具包,功能强大,作为代理服务器,可以对抓获到的请求或响应进行修改,然后模拟客户端发送新的请求或模拟服务器返回修改后的响应结果. Fiddler中设置断点修改Request Fid ...

  5. 读懂在单台机器上创建RabbitMQ集群

    在优锐课java中了解有关在单台计算机上安装集群以及如何向集群添加更多节点的更多信息,码了很多专业的相关知识, 分享给大家参考学习. 如果你在单台计算机上设置群集时遇到问题,那么以下文章可能会帮助回答 ...

  6. 09-Django静态文件

    1.静态文件 项目中的图片.CSS.js都是静态文件,一般会将静态文件放到一个单独的目录下,也方便管理.一般会将静态文件放到一个单独的目录下,也可以放在应用的目录下,由于静态文件是全部应用都在使用的, ...

  7. 【CF528E】Triangles 3000(计算几何)

    [CF528E]Triangles 3000(计算几何) 题面 CF 平面上有若干条直线,保证不平行,不会三线共点. 求任选三条直线出来围出的三角形的面积的期望. 题解 如果一定考虑直接计算这个三角形 ...

  8. [算法]LeetCode 152:乘积最大子序列

    题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示 ...

  9. 屏幕输入转换为int//方法大注释

    可以使用两种方法: using System; namespace 方法测试 { class Program { static void Main(string[] args) { Console.W ...

  10. 深入理解JVM,7种垃圾收集器

    本人免费整理了Java高级资料,一共30G,需要自己领取.传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q 如果说收集算法是内存回收的方法论, ...