【关于实验】批量将jpg转化成txt:MATLAB

因为已写好的程序需要txt的数据文件,打算做100幅以上的数据实验,所以打算将jpg批量转成txt

因为C++好像读取jpg比较难,好像也有库文件,不过还是用matlab了,因为打开jpg非常直接


以下是读取转化一幅图像的脚本:


data_to_be_processed = imread( 'C:\Documents and Settings\Administrator\桌面\图片\1.jpg' );
data = data_to_be_processed(:,:,1);


%打开要写入的txt
fid = fopen('1.txt','wt'); 
fprintf(fid,'%d ',data);
fclose(fid);


*************************************************************************

以下是批量转化的脚本:



%脚本功能:
%处理给定指定路径下的所有某种格式的数据,并形成日志


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%第一步:统计给定路径下,某种格式的数据对象的个数
%数据要求:
%路径需要是字符串类型char
data_storage_PATH = 'C:\Documents and Settings\Administrator\桌面\图片\';


%数据要求:
%路径需要是字符串类型char
data_type = '.jpg';


%搜索目标:C:\Users\Administrator\Desktop\卫星图像\*.jpg
search_for_PATH = strcat(data_storage_PATH, '\*', data_type);


%寻找给定路径下的所有某格式数据
listing = dir(search_for_PATH);


data_NUM = length(listing);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%主循环


for i = 1:data_NUM
    
    %以下是读入数据阶段
    num_str = num2str(i);
    data_NAME = strcat(data_storage_PATH, num_str, data_type);
    %[data_to_be_processed, data_DIMENSION] = data_READING(data_NAME);
    data_to_be_processed = imread( data_NAME );
    data = data_to_be_processed(:,:,1);
    
    %总进度显示:5 of 100(这种样子)
    
    disp(['PRESENT:  ', num2str(i), '  of  ', num2str(data_NUM), '  data objects.']);
    
    %以下是处理数据阶段(并且中间包含中间日志生成过程 & 向最终日志diary中填写本条处理记录的内容)
    %output = data_PROCESSING(data_to_be_processed);
    %打开要写入的txt
    txt_name = strcat(data_storage_PATH, num_str, '.txt');
    fid = fopen(txt_name,'wt');
    fprintf(fid,'%d ',data);
    fclose(fid);
    
end

你可能感兴趣的:(关于实验)