数学建模B题国家一等奖Matlab程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

附录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:n
image_1(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save image_1 image_1;%附件1图像
%读取附件2中的图像
n = 19;
image_name = strcat('D:/B/附件2','/',image_num,'.bmp');%拼接图像地址和名字
for j = 1:n
image_2(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save image_2 image_2;%附件2图像
%读取附件3中的图像
n = 209;
image_name = strcat('D:/B/附件3','/',image_num,'.bmp');%拼接图像地址和名字
for j = 1:n
image_3(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save image_3 image_3;%附件3图像
%读取附件4中的图像
n = 209;
image_name = strcat('D:/B/附件4','/',image_num,'.bmp');%拼接图像地址和名字
for j = 1:n
image_4(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save image_4 image_4;%附件4图像
%读取附件5 a 中的图像
n = 209;
image_name = strcat('D:/B/附件5','/',image_num,'a','.bmp');%拼接图像地址和名字for j = 1:n
image_5a(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save image_5a image_5a;%附件5a图像
%读取附件5 b 中的图像
n = 209;
image_name = strcat('D:/B/附件5','/',image_num,'b','.bmp');%拼接图像地址和名字for j = 1:n
image_5b(:,:,j) = imread(image_name(j,:));%将图像读入矩阵
end
save 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_chang
if( sum(hang_he_01(i:i+kong_ge_chang-1)) == kong_ge_chang )
break;
end
end
left_zi_chang = i - 1;
else
left_zi_chang = 0;
end
if(left_zi_chang > 41)
left_zi_chang = 0;
end
if( hang_he_01(col) == 0 )%本行结束为汉字,统计汉字的长度
for i =col:-1:kong_ge_chang
if( sum(hang_he_01(i-kong_ge_chang +1:i)) == kong_ge_chang ) break;
end
end
right_zi_chang = col - i;
else
right_zi_chang = 0;
end
if(right_zi_chang > 41)
right_zi_chang = 0;
end
3.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_chang
if( sum(hang_he_01(i:i+kong_ge_chang-1)) == kong_ge_chang ) break;
end
end
left_zi_chang = i - 1;
else
left_zi_chang = 0;
end
if(left_zi_chang > 41)
left_zi_chang = 0;
end
if( hang_he_01(col) == 0 )%本行结束为汉字,统计汉字的长度
for i =col:-1:kong_ge_chang
if( sum(hang_he_01(i-kong_ge_chang +1:i)) == kong_ge_chang ) break;
end
end
right_zi_chang = col - i;
else
right_zi_chang = 0;
end
if(right_zi_chang > 41)
right_zi_chang = 0;
end
4.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:19
zi_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;
end
fujian_1(:,:,j) = fujian_zitu;
end
save 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:19
for j=1:19
fujian_1_pipei_du(i,j) = f_pi_pei_du(fujian_1(:,2,i),fujian_1(:,1,j),fa_zhi);
end
end
save 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:18
for j = 1:19;
if( fj1_yong(j)==1 )
fujian_1_pipei_du_bian(fj1_pinjie_jieguo(i),j) = 0;
end
end
[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;
end
save fj1_pinjie_jieguo fj1_pinjie_jieguo;;%保存拼接结果
fj1_pinjie_jieguo%%%拼接结果输出到屏幕上
%%%%%%%%%%%%
fujian_1_jieguo_tu = zeros(1980,19*72);
for i = 1:19
fujian_1_jieguo_tu(:,(i-1)*72+1:i*72) = image_1(:,:,fj1_pinjie_jieguo(i));
end
save 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,hou
load image_2;
for m=1:19
qian(:,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:19
for i=1:19
fujian2_pipei_du(i,j) = sum( qian_01(:,i) == hou_01(:,j) ) / 1980;
end
end
save 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))); end
save fj2_pinjie_jieguo fj2_pinjie_jieguo;
fj2_pinjie_jieguo
fujian_2_jieguo_tu = zeros(1980,19*72);
for i = 1:19
fujian_2_jieguo_tu(:,(i-1)*72+1:i*72) = image_2(:,:,fj2_pinjie_jieguo(i));
end
save 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:209
image_hang_he(:,i) = sum(image_3(:,:,i)',1);
end
%若为空格则标记为1,并统计空格的个数。

若为汉字则标记为0,并统计汉字的个数。

将结果放入矩阵image_hang_he_18360中
for i = 1:209
he_18360 = 0; %若一行都是空格,则他的像素和为72*255=18360
for j=1:180
if(image_hang_he(1,i) == 18360)
index = 1;
if(image_hang_he(j,i) == 18360)
he_18360 = he_18360 + 1;
else
break;
end
else
index = 0;
if(image_hang_he(j,i) ~= 18360)
he_18360 = he_18360 + 1;
else
break;
end
end
end
image_hang_he_18360(1,i) = he_18360;
image_hang_he_18360(2,i) = index;
end
7. fj_3_hang_jieguo.m function fj_3_hang_jieguo = f_fj3_hang_pinjie(image_3,A1)
%A1 为附件3的一组
%例如 A1=[50 3 23 58 92 119 130 144 191 66 142 187 193 12 29 55 179 189 96]; %A1行所选择的图片 %输入参数
%将A1行的这些图片的最左边(第一列)的像素放入fujian3_1qian。

将这些图片做右边(最后一列)的像素放入fujian3_1hou。

%load image_3;
fujian3_1qian = zeros(180,19);
for i=1:19
fujian3_1qian(:,i)=image_3(:,1,A1(i));
end
fujian3_1hou = zeros(180,19);
for i=1:19
fujian3_1hou(:,i)=image_3(:,72,A1(i));
end
%图像0-1化
qian_01 = fujian3_1qian>=255;
hou_01 = fujian3_1hou>=255;
%匹配度
fujian_3_pipei_du = zeros(19,19);
liaxu_fazhi = 5;
for i= 1:19
for j=1:19
if(j~=i)
%qian_hou_he = hou_01(:,i) + qian_01(:,j);
% fujian_3_pipei_du(i,j) = sum( (qian_hou_he == 0) + (qian_hou_he == 2)*2 - (qian_hou_he == 1)*3 ) / 180;
fujian_3_pipei_du(i,j) = sum( hou_01(:,i) == qian_01(:,j) ) / 180;
%fujian_3_pipei_du(i,j) = f_lianxu_pipei_du(hou_01(:,i),qian_01(:,j),liaxu_fazhi);
else
fujian_3_pipei_du(i,j) = 0;
end
end
end
%save fujian_3_pipei_du fujian_3_pipei_du;
fj_3_pinjie_jieguo = zeros(1,19);
fj_3_pinjie_yong = zeros(1,19)
fj_3_pinjie_jieguo(1) = 1;%输入参数
fj_3_pinjie_yong(3) = 1;
for i = 1:18
for j = 1:19;
if( fj_3_pinjie_yong(j)==1 )
fujian_3_pipei_du(fj_3_pinjie_jieguo(i),j) = 0;
end
end
[max_pipei_du max_pipei_index] = max(fujian_3_pipei_du(fj_3_pinjie_jieguo(i),:));
fj_3_pinjie_jieguo(i+1) = max_pipei_index;
fj_3_pinjie_yong(max_pipei_index) = 1;
end
fj_3_hang_jieguo = zeros(1,19);
for i = 1:19
fj_3_hang_jieguo(i) = A1(fj_3_pinjie_jieguo(i));
end
%A1
fj_3_pinjie_jieguo
fj_3_hang_jieguo
figure;
imshow([image_3(:,:,fj_3_hang_jieguo(1)),image_3(:,:,fj_3_hang_jieguo(2)),image_3(:,:,fj_3_ hang_jieguo(3)),image_3(:,:,fj_3_hang_jieguo(4))...
,image_3(:,:,fj_3_hang_jieguo(5)),image_3(:,:,fj_3_hang_jieguo(6)),image_3(:,:,fj_3_han g_jieguo(7)),image_3(:,:,fj_3_hang_jieguo(8))...
,image_3(:,:,fj_3_hang_jieguo(9)),image_3(:,:,fj_3_hang_jieguo(10)),image_3(:,:,fj_3_ha ng_jieguo(11)),image_3(:,:,fj_3_hang_jieguo(12))...
,image_3(:,:,fj_3_hang_jieguo(13)),image_3(:,:,fj_3_hang_jieguo(14)),image_3(:,:,fj_3_h ang_jieguo(15)),image_3(:,:,fj_3_hang_jieguo(16))...
,image_3(:,:,fj_3_hang_jieguo(17)),image_3(:,:,fj_3_hang_jieguo(18)),image_3(:,:,fj_3_h ang_jieguo(19))]);
8.fujian_3_pinjie_hang.m文件
%%%%%%%%
% 此文件,将附件 3 中的图片分好的组,进行组内拼接,产生源文件的行
%行分组情况
fujian_3_fenzu_juzh = [
30 6 49 60 76 173 207 11 38 45 56 65 93 99 105 112 172 181 202
39 106 9 26 36 47 75 82 89 104 123 131 149 162 168 194 10 25 190
95 35 59 44 48 78 85 91 98 113 122 125 128 137 145 150 165 184 43
8 1 46 33 69 71 127 159 167 175 176 54 57 94 138 139 154 197 209
62 97 117 132 7 20 21 37 53 64 68 70 73 79 80 100 163 164 178
72 84 86 171 203 34 81 133 134 157 166 199 201 206 28 16 153 18 61
126 17 22 111 140 146 151 158 182 183 185 188 205 107 174 198 14 67 110
90 5 102 103 109 114 124 152 155 156 195 41 120 141 147 208 115 186 118
169 51 63 121 180 192 196 19 24 27 42 77 88 101 143 148 87 2 31
15 74 108 4 13 32 40 83 116 129 135 160 161 170 177 200 52 136 204
50 3 23 58 92 119 130 144 191 66 142 187 193 12 29 55 179 189 96
];
load image_3;
fj_3_pinjie_hang = zeros(11,19);
for i = 1:11
%调用函数f_fj3_hang_pinjie ,完成行分组后的最内拼接,并图形显示结果 fj_3_pinjie_hang(i,:) = f_fj3_hang_pinjie(image_3,fujian_3_fenzu_juzh(i,:));
end
save fj_3_pinjie_hang fj_3_pinjie_hang;
9.fujian_3_pinjie_jieguo.m 文件
m 文件
%% 1.将11*19的纸条,分成11组,即先把属于同一组的图片归组,依据就是“每个纸条的上边界空行数”或者“汉字数”
load image_3;
for i = 1:209
image_hang_he(:,i) = sum(image_3(:,:,i)',1);
end
%若为空格则标记为1,并统计空格的个数。

若为汉字则标记为0,并统计汉字的个数。

将结果放入矩阵image_hang_he_18360中
for i = 1:209
he_18360 = 0; %若一行都是空格,则他的像素和为72*255=18360
for j=1:180
if(image_hang_he(1,i) == 18360)
index = 1;
if(image_hang_he(j,i) == 18360)
he_18360 = he_18360 + 1;
else
break;
end
else
index = 0;
if(image_hang_he(j,i) ~= 18360)
he_18360 = he_18360 + 1;
else
break;
end
end
end
image_hang_he_18360(1,i) = he_18360;
image_hang_he_18360(2,i) = index;
end
10.fujian_4_tezheng_chazhao.% 从投影图上观察图像特征
load image_4
image_4_01 = image_4<250;
%fujian_hang = [21 42 109 117 137 74 37 208 136 16 77 44 200 46 174 80 162 180 144];
fujian_hang = [ 21 42 109 117 137 74 37 208 136 16 77];
for i = 1:11
fujian_4_zitu = image_4_01(:,:,fujian_hang(i));
fujian_4_zitu_hang_touying = sum(fujian_4_zitu,2);
fujian_4_zitu_hang_touying_fazhi = 40*(fujian_4_zitu_hang_touying == 0);
figure;
subplot(2,2,1);
plot(1:180,fujian_4_zitu_hang_touying);
subplot(2,2,2);
plot(fujian_4_zitu_hang_touying_fazhi,'*');
subplot(2,2,3);
imshow(image_4(:,:,fujian_hang(i)));
end
%figure
%imshow([image_4(:,:,fujian_hang(1)),image_4(:,:,fujian_hang(2)),image_4(:, %:,fujian_hang(3)),image_4(:,:,fujian_hang(4)),image_4(:,:,fujian_hang(5))]);
11.f_fujian_4_weizhi
function zhongxin_weizhi = f_fujian_4_weizhi(tou_ying)
% 计算附件4中每个子图第一行的中心位置
%tou_ying = fujian_4_hang_touying(:,1);
biaozhi = 0;% 1表示找见第一行,
zi = 0;
qishi_weizhi = 1;
while(biaozhi == 0)
chang = 0;
if( tou_ying(qishi_weizhi) ~=0 )%字符
for i = qishi_weizhi:180
if(tou_ying(i) ~=0)
chang = chang + 1;
else
break
end
end
if(chang>20)
jiesu_weizhi = qishi_weizhi+chang;
biaozhi = 1;
break
else
qishi_weizhi = i;
end
else%空行
for i = qishi_weizhi:180
if(tou_ying(i) ==0)
chang = chang + 1;
else
break
end
end
qishi_weizhi = i;
end
end
%找极值
qishi_hang = tou_ying(1:jiesu_weizhi);
qishi_hang(1:qishi_weizhi) = zeros(1,qishi_weizhi);
zhengfu = zeros(1,jiesu_weizhi);
for i = 1:jiesu_weizhi-1
if(qishi_hang(i+1)>qishi_hang(i))
zhengfu(i) = 1;
else
zhengfu(i) = -1;
end
end
cuowei_jia = zhengfu(1:jiesu_weizhi-2) + zhengfu(2:jiesu_weizhi-1);
jizhi_weizhi = find(cuowei_jia==0);
jizhi_shu = length(jizhi_weizhi);
jizhi = [];
for i = 1:jizhi_shu
jizhi = [jizhi;[tou_ying(jizhi_weizhi(i)) jizhi_weizhi(i)]];
end
jizhi_sort = sortrows(jizhi);
zhongxin_weizhi = floor((jizhi_sort(jizhi_shu,1) + jizhi_sort(jizhi_shu-1,2))/2); if(zhongxin_weizhi>70)
zhongxin_weizhi = zhongxin_weizhi-70;
end。

相关文档
最新文档