1. 对序列进行洗牌 randperm()
randperm()产生随机的序列 %if filepaths 是一个5*1的结构体,then cshuffle = randperm(length(filepaths)) %对filepaths进行随机的洗牌,得到了 cshuffle => [2,5,4,1,3] 洗牌后的序列
2. 图像灰度化 rgb2gray()
MyYuanLaiPic = imread('e:/image/matlab/Cluo.jpg');%读取RGB格式的图像 MyFirstGrayPic = rgb2gray(MyYuanLaiPic);%用已有的函数进行RGB到灰度图像的转换 [rows , cols , colors] = size(MyYuanLaiPic);%得到原来图像的矩阵的参数 MidGrayPic = zeros(rows , cols);%用得到的参数创建一个全零的矩阵,这个矩阵用来存储用下面的方法产生的灰度图像 MidGrayPic = uint8(MidGrayPic);%将创建的全零矩阵转化为uint8格式,因为用上面的语句创建之后图像是double型的 for i = 1:rows for j = 1:cols sum = 0; for k = 1:colors sum = sum + MyYuanLaiPic(i , j , k) / 3;%进行转化的关键公式,sum每次都因为后面的数字而不能超过255 end MidGrayPic(i , j) = sum; end end imwrite(MidGrayPic , 'E:/image/matlab/Cluo.png' , 'png'); %显示原来的RGB图像 figure(1); imshow(MyYuanLaiPic); %显示经过系统函数运算过的灰度图像 figure(2); imshow(MyFirstGrayPic); %显示转化之后的灰度图像 figure(3); imshow(MidGrayPic);
3. 对图像进行旋转和翻转
function I = data_augmentation(I, K) if K == 1 return; elseif K == 2 % flipped I = flipud(I); return; elseif K == 3 % rotation 90 I = rot90(I,1); return; elseif K == 4 % rotation 90 & flipped I = rot90(I,1); I = flipud(I); return; elseif K == 5 % rotation 180 I = rot90(I,2); return; elseif K == 6 % rotation 180 & flipped I = rot90(I,2); I = flipud(I); return; elseif K == 7 % rotation 270 I = rot90(I,3); return; elseif K == 8 % rotation 270 & flipped I = rot90(I,3); I = flipud(I); return; end
4. 对array进行连接 cat(dim, A, B)
、
5.对图像进缩放,imresize()
HR_current = imresize(HR,nscales(j,i),'bicubic'); %第二个参数为缩放因子,e.g. 0.5、0.8
%第三个参数为缩放method
6.读取图像默认的数据类型uint8,最大值为255;但是在进行图形矩阵的操作和变化过程中非常容易溢出,所以需要转化为double(64为,0~1)或者single
im2double()将值0~255映射到0~1之间
7.两个循环搞定patch的获取
for x = 1+step1 : stride : (hei-patchsize+1) for y = 1+step2 : stride : (wid-patchsize+1) count = count + 1; subim_label = HR_current(x : x+patchsize-1, y : y+patchsize-1,1:nch); imdb.HRlabels(:, :, :, count) = subim_label; if count<=diffPatches %不够一个patch进行填充 imdb.HRlabels(:, :, :, end-count+1) = HR_current(x : x+patchsize-1, y : y+patchsize-1,1:nch); end end end
8.产生图像的patch
function [imdb] = generatepatches %% Note, set your training image set first, large dataset is prefered! folders = {'path_of_your_training_dataset'}; % set this first! stride = 60; % control the number of image patches patchsize = 70; batchSize = 256; % important for BNorm count = 0; nch = 1; % 1 for grayscale image, 3 for color image step1 = 0; step2 = 0; ext = {'*.jpg','*.png','*.bmp'}; filepaths = []; for j = 1:length(folders) for i = 1 : length(ext) filepaths = cat(1,filepaths, dir(fullfile(folders{j}, ext{i}))); %获取folder获取所有figure end end cshuffle = randperm(length(filepaths)); % randperm获取filepaths的随机排列 nimages = round(length(filepaths)); % control the number of image patches 去整数 ns = 1; nscales = min(1,0.45 + 0.05*randi(21,[ns,nimages])); %产生随机矩阵 1*5 all values are less than 1 naugment = randi(8,[1,nimages]); %产生一个随机矩阵1*5 all values are form 1 - 8 for i = 1 : nimages % HR = imread(fullfile(filepaths(cshuffle(i)).folder,filepaths(cshuffle(i)).name)); HR = imread(fullfile(folders{1},filepaths(cshuffle(i)).name)); %从文件夹里随机读取一张图片 HR = HR(:,:,1); %如果是rgb图就获取一个通道的,如果是gray图则自然为该图像 HR = data_augmentation(HR, naugment(i)); % 数据增大data_augmentation data_augmentation(HR, 7); 转到data_augmentation.m里面就将图片旋转90度 disp([i,nimages,round(count/batchSize)]) for j = 1: size(nscales,1) % size(nscales,1) 为1 HR_current = imresize(HR,nscales(j,i),'bicubic'); %对图像进行尺度的缩放 [hei,wid,~] = size(HR_current); % 得到此时的高和宽 for x = 1+step1 : stride : (hei-patchsize+1) % 产生的patch (hei-patchsize+1)这个value是一个临界值,最后放不了一个patch for y = 1+step2 : stride : (wid-patchsize+1) count=count+1; end end end end numPatches = ceil(count/batchSize)*batchSize; %ceil(1.2)=>2 ceil(0.2)=>1 总共有多少个patch diffPatches = numPatches - count; % 真实的差了多少个patch disp([numPatches,numPatches/batchSize,diffPatches]); disp('-----------------------------'); %------------------------------------------------------------------ %------------------------------------------------------------------ count = 0; imdb.HRlabels = zeros(patchsize, patchsize, nch, numPatches,'single'); %imdb是一个结构体 70*70*1*256 for i = 1 : nimages % HR = imread(fullfile(filepaths(cshuffle(i)).folder,filepaths(cshuffle(i)).name)); HR = imread(fullfile(folders{1},filepaths(cshuffle(i)).name)); %从文件夹里随机读取一张图片 if nch ==1 && size(HR,3) == 3 %rgb2gray HR = rgb2gray(HR); end HR = data_augmentation(HR, naugment(i)); % 图像旋转操作 disp([i,nimages,round(count/256)]) for j = 1: size(nscales,1) HR_current = imresize(HR,nscales(j,i),'bicubic'); %图像进行随机的缩放 [hei,wid,~] = size(HR_current); HR_current = im2single(HR_current); for x = 1+step1 : stride : (hei-patchsize+1) for y = 1+step2 : stride : (wid-patchsize+1) count = count + 1; subim_label = HR_current(x : x+patchsize-1, y : y+patchsize-1,1:nch); imdb.HRlabels(:, :, :, count) = subim_label; if count<=diffPatches %不够一个patch进行填充 imdb.HRlabels(:, :, :, end-count+1) = HR_current(x : x+patchsize-1, y : y+patchsize-1,1:nch); end end end end end imdb.set = uint8(ones(1,size(imdb.HRlabels,4)));
9.求某一个矩阵的指定维度的大小
size(A,dims) = value # 如xx是一个16*401的矩阵,则求到第一维度的大小16 size(xx,1) =>16 # 如xx是一个16*401的矩阵,则求到第二维度的大小401 size(xx,2) =>401
请发表评论