前段時間,無所事事,就幫人寫了一個將多張圖片轉化爲AVI的程序,因爲圖方便就採用了Matlab語言!
其實整個程序和流程都很簡單,就是將一個文件夾中的圖片依次讀入,並將其數據存儲到AVI容器中!
整個程序包括兩個子程序:getallfile.m-----查找文件夾下包含的所有圖片
pic2avi.m -----圖片的顯示並儲存爲AVI文件
下面直接上代碼:
%%
% 功能:程序實現了將多幅圖片轉換爲AVI格式
% 說明:1.可以選擇是否對圖片進行預處理,如果圖片都是尺寸相同,圖片格式相同,
% 那麼可以直接跳過這一步,否則對圖片進行預處理操作
% 2.步驟:
% step1: 參數設置
% step2: 遍歷目標文件夾下的所有圖片,將圖片的路徑信息保存爲結構體
% step3: 按照設定的參數讀取圖片,並進行預處理操作
% step4: 得到圖片信息,轉化爲AVI,並進行保存
%% step1:參數的設置
clear
clc
disp('第一步,基本參數的設置...................')
%待處理圖片的存放文件夾,可以存在子文件夾的情況
dirName = 'D:\MATLAB\work\Study\DATA';
%處理後的文件夾的存放位置,可以不存在,程序會自動生成
desName = 'D:\MATLAB\work\Study\PDATA';
%最終圖片的歸一化尺寸
thesizex = 500;thesizey=500; thesizez=3; ischangeszie = 0;
%採用RGB還是直接使用Gray的圖片
imagetype = 'Gray';
%圖片的通用格式設置
fmt = '.bmp';
%% step2:遍歷目標文件夾下的所有圖片,將圖片的路徑信息保存爲結構體
disp('第二步,獲取文件夾下所有的圖像文件...................')
%注意:不要在此文件夾下放其他類型的文件,否則會報錯
%獲取文件夾下所有的文件
fileList = getAllFiles(dirName);
%% step3: 開始對圖片進行預處理操作
disp('第三步,開始對對每一張圖片進行處理...................')
%圖片的數目
img_num = length(fileList);
if img_num ==0;
error('設定的文件夾內沒有任何的圖片,請重新檢查...')
end
for ii = 1:img_num %開始遍歷
imgname = fileList{ii};
I_img = imread(imgname);
%轉化爲灰度圖
[sizex,sizey,sizez] = size(I_img);
if sizez ==3&&strcmp(imagetype,'Gray')
I_img = rgb2gray(I_img);
thesizez = 1;
end
if ii==1
disp(strcat('當前尺寸範圍爲: ',num2str(sizex),' *',num2str(sizey),';\n'));
elseif (ii==1)&&(ischangeszie==1)
disp(strcat('現將所有圖片轉化爲:',num2str(thesizex),'*',num2str(thesizey)));
ischange = input('是否對變換尺寸進行更改:是(1) 否(0)');
if ischange ==1
thesizex = input('請輸入歸一化圖像的高度:');
thesizey = input('請輸入歸一化圖像的寬度:');
end
elseif (ii==1)&&(ischangeszie==0)
disp('不進行圖像的尺寸變換');
end
%進行尺寸的歸一化操作
if ischangeszie==1
I_img = imresize(I_img,[thesizex,thesizey],'bilinear');
end
%對處理的圖片進行統一命名的保存
if ii==1
if not(exist(desName,'dir'))
mkdir(desName);
end
end
imwrite(I_img,strcat(desName,'\',num2str(ii),fmt),fmt(2:end));
end
clear ii I_img sizex sizey sizez fileList
%% step4: 得到圖片信息,轉化爲AVI,並進行保存
%獲取文件夾下所有的文件
fileList = getAllFiles(desName);
%圖片的數目
img_num_out = length(fileList);
if img_num_out ==0|| img_num_out~=img_num
error('設定的文件夾內的圖片數量出現了錯誤,請重新檢查...')
end
%創建一個AVI格式的容器,用於保存圖像數據信息
aviobj = avifile('mymovie.avi','fps',1);
%Mdata = zeros(thesizex,thesizey,thesizez,img_num_out,'uint8');
for i=1:img_num_out
%Mdata(:,:,:,i)=imread(fileList{i});
Mdata = imread(fileList{i});
imshow(Mdata)
frame = getframe(gca);
aviobj = addframe(aviobj,frame);
end
aviobj = close(aviobj);
function fileList = getAllFiles(dirName)
% 功能:指定文件路徑,獲取該路徑所包含的所有文件
% 由於採用了嵌套操作,因此可以獲取子文件夾的文件
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end
相應的測試程序和測試圖片包括使用說明在我的資源裏下載!