附录3:程序源文件1.duqu_image.m文件%数据读取预处理文件%将附件中的图片读取到matlab矩阵中,并保存为image_1,image_2,image_3,image_4,image_5a,image_5b%所有附件均放在文件夹 D:\B 中%%%%%%%%%%%%%%%%%%%%%%%%图片名序列%图像名称序号b = [ones(1,10);0 1 2 3 4 5 6 7 8 9]';image_num= [strcat( num2str(0*b(:,1)),num2str(0*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(2*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(3*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(4*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(5*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(6*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(7*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(8*b(:,1)),num2str(b(:,2)) ),strcat( num2str(0*b(:,1)),num2str(9*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(0*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(2*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(3*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(4*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(5*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(6*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(7*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(8*b(:,1)),num2str(b(:,2)) ),strcat( num2str(b(:,1)),num2str(9*b(:,1)),num2str(b(:,2)) ),strcat( num2str(2*b(:,1)),num2str(0*b(:,1)),num2str(b(:,2)) ),]; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 读取所有附件中的图片%读取附件1中的图像n = 19;image_name = strcat('D:/B/附件1','/',image_num,'.bmp');%拼接图像地址和名字for j = 1:nimage_1(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_1 image_1;%附件1图像%读取附件2中的图像n = 19;image_name = strcat('D:/B/附件2','/',image_num,'.bmp');%拼接图像地址和名字for j = 1:nimage_2(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_2 image_2;%附件2图像%读取附件3中的图像n = 209;image_name = strcat('D:/B/附件3','/',image_num,'.bmp');%拼接图像地址和名字for j = 1:nimage_3(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_3 image_3;%附件3图像%读取附件4中的图像n = 209;image_name = strcat('D:/B/附件4','/',image_num,'.bmp');%拼接图像地址和名字for j = 1:nimage_4(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_4 image_4;%附件4图像%读取附件5 a 中的图像n = 209;image_name = strcat('D:/B/附件5','/',image_num,'a','.bmp');%拼接图像地址和名字for j = 1:nimage_5a(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_5a image_5a;%附件5a图像%读取附件5 b 中的图像n = 209;image_name = strcat('D:/B/附件5','/',image_num,'b','.bmp');%拼接图像地址和名字for j = 1:nimage_5b(:,:,j) = imread(image_name(j,:));%将图像读入矩阵endsave image_5b image_5b;%附件5a图像2.f_hang_left_right.m文件function [left_zi_chang right_zi_chang] = f_hang_left_right(image_hang)% 此函数计算一行字左右两端汉字的长度%image_hang表示附件图片中第几行字%image_hang = image_1(106:146,:,1);%取行[row col] = size(image_hang);image_01 = image_hang>=200; %01化图形,0表示汉字,1表示背景image_hang_he = sum(image_01,1);%对列求和%%%%%%%%%%%%%%%%%%%找汉字hanzi_fa_zhi = row;%表示空格kong_ge_chang = 3;% 空格的长度hang_he_01 = image_hang_he == hanzi_fa_zhi;if( hang_he_01(1) == 0 )%本行左边开头为汉字,统计汉字的长度for i =1:col-kong_ge_changif( sum(hang_he_01(i:i+kong_ge_chang-1)) == kong_ge_chang )break;endendleft_zi_chang = i - 1;elseleft_zi_chang = 0;endif(left_zi_chang > 41)left_zi_chang = 0;endif( hang_he_01(col) == 0 )%本行结束为汉字,统计汉字的长度for i =col:-1:kong_ge_changif( sum(hang_he_01(i-kong_ge_chang +1:i)) == kong_ge_chang ) break;endendright_zi_chang = col - i;elseright_zi_chang = 0;endif(right_zi_chang > 41)right_zi_chang = 0;end3.f_pi_pei_du.m文件function [left_zi_chang right_zi_chang] = f_hang_left_right(image_hang) % 此函数计算一行字左右两端汉字的长度%image_hang表示附件图片中第几行字%image_hang = image_1(106:146,:,1);%取行[row col] = size(image_hang);image_01 = image_hang>=200; %01化图形,0表示汉字,1表示背景image_hang_he = sum(image_01,1);%对列求和%%%%%%%%%%%%%%%%%%%找汉字hanzi_fa_zhi = row;%表示空格kong_ge_chang = 3;% 空格的长度hang_he_01 = image_hang_he == hanzi_fa_zhi;if( hang_he_01(1) == 0 )%本行左边开头为汉字,统计汉字的长度for i =1:col-kong_ge_changif( sum(hang_he_01(i:i+kong_ge_chang-1)) == kong_ge_chang ) break;endendleft_zi_chang = i - 1;elseleft_zi_chang = 0;endif(left_zi_chang > 41)left_zi_chang = 0;endif( hang_he_01(col) == 0 )%本行结束为汉字,统计汉字的长度for i =col:-1:kong_ge_changif( sum(hang_he_01(i-kong_ge_chang +1:i)) == kong_ge_chang ) break;endendright_zi_chang = col - i;elseright_zi_chang = 0;endif(right_zi_chang > 41)right_zi_chang = 0;end4.fujian_1_chengxu.m文件%%%%%%%%% 此文件,将附件1中的图片进行拼接% 1.先统计出附件1 所有行字的左右汉字长度% 2.计算匹配度% 3.根据匹配度进行拼接load image_1;%读取附件1数据文件% 人工干预计算下列值ding_kong = 37;% ding_kong 表示顶端空行zi_gao = 41;% zi_gao 表示字的高度zi_kuan = 41;% zi_kuan 表示字的宽度konghang_gao = 26;% konghang_gao 表示空行的高度hang_shu = 27;%附件1 所给文件的汉字总行数% 1.先统计出附件1 所有行字的左右汉字长度fujian_zitu = zeros(hang_shu,2);%附件1中的每一个子图的左右汉字长度fujian_1 = zeros(27,2,19);%附件1中所有图的左右汉字长度for j = 1:19zi_ding = ding_kong;for i = 1:hang_shu[fujian_zitu(i,1) fujian_zitu(i,2)]= f_hang_left_right( image_1( zi_ding+1 : zi_ding +zi_gao ,:,j) );zi_ding = zi_ding + zi_gao + konghang_gao;endfujian_1(:,:,j) = fujian_zitu;endsave fujian_1_zuoyou_zichang fujian_1;%保存字长结果fujian1_pinjie_jieguo = zeros(1,19);%保存拼接结果的矩阵pipeidu = zeros(1,19);%用来保存中间匹配度的向量fujian1_pinjie_jieguo(1) = 9;%人工干预找到的第一个(最左边)的纸条(即,图008.bmp)fa_zhi = 0.8;% 匹配度计算时的阀值,大于此阀值的任务匹配正确,小于此值的认为匹配不正确% 2.计算匹配度%匹配度fujian_1_pipei_du = zeros(19,19);for i= 1:19for j=1:19fujian_1_pipei_du(i,j) = f_pi_pei_du(fujian_1(:,2,i),fujian_1(:,1,j),fa_zhi);endendsave fujian_1_pipei_du fujian_1_pipei_du;;%保存匹配度结果fujian_1_pipei_du_bian = fujian_1_pipei_du;%用于去除已选的纸条% 3.根据匹配度进行拼接fj1_pinjie_jieguo = zeros(1,19);fj1_yong = zeros(1,19);fj1_pinjie_jieguo(1) = 9;fj1_yong(fj1_pinjie_jieguo(1)) = 1;for i = 1:18for j = 1:19;if( fj1_yong(j)==1 )fujian_1_pipei_du_bian(fj1_pinjie_jieguo(i),j) = 0;endend[max_pipei_du fj1_pinjie_jieguo(i+1)] = max(fujian_1_pipei_du_bian(fj1_pinjie_jieguo(i),:));fj1_yong(fj1_pinjie_jieguo(i+1)) = 1;endsave fj1_pinjie_jieguo fj1_pinjie_jieguo;;%保存拼接结果fj1_pinjie_jieguo%%%拼接结果输出到屏幕上%%%%%%%%%%%%fujian_1_jieguo_tu = zeros(1980,19*72);for i = 1:19fujian_1_jieguo_tu(:,(i-1)*72+1:i*72) = image_1(:,:,fj1_pinjie_jieguo(i));endsave fujian_1_jieguo_tu fujian_1_jieguo_tu;imview(fujian_1_jieguo_tu)5.fujian2_pinjie_jieguo.m文件%%%%%%%%% 此文件,将附件2中的图片进行拼接% 1.先统计出附件2 所有纸条的左右边界像素qian,hou% 2.计算匹配度% 3.根据匹配度进行拼接% 1.先统计出附件2 所有纸条的左右边界像素qian,houload image_2;for m=1:19qian(:,m)=image_2(:,1,m);hou(:,m)=image_2(:,72,m);end% 2.计算匹配度%图像0-1化qian_01 = qian>=255;hou_01 = hou>=255;%匹配度fujian2_pipei_du = zeros(19,19);for j= 1:19for i=1:19fujian2_pipei_du(i,j) = sum( qian_01(:,i) == hou_01(:,j) ) / 1980;endendsave fujian_2_pipei_du fujian2_pipei_du;% 3.根据匹配度进行拼接fj2_pinjie_jieguo = zeros(1,19);fj2_pinjie_jieguo(1) = 4;for i = 1:18[max_pipei_du fj2_pinjie_jieguo(i+1)] = max(fujian2_pipei_du(:,fj2_pinjie_jieguo(i))); endsave fj2_pinjie_jieguo fj2_pinjie_jieguo;fj2_pinjie_jieguofujian_2_jieguo_tu = zeros(1980,19*72);for i = 1:19fujian_2_jieguo_tu(:,(i-1)*72+1:i*72) = image_2(:,:,fj2_pinjie_jieguo(i));endsave fujian_2_jieguo_tu fujian_2_jieguo_tu;imview(fujian_2_jieguo_tu)6.fujian_3_fenzu.m 文件文件%% 1.将11*19的纸条,分成11组,即先把属于同一组的图片归组,依据就是“每个纸条的上边界空行数”或者“汉字数”load image_3;for i = 1:209image_hang_he(:,i) = sum(image_3(:,:,i)',1);end%若为空格则标记为1,并统计空格的个数。