2013B题碎纸片拼接

Photo1_1:

clear;clc;
path='E:\B\附件1\';
files=dir('E:\B\附件1\*.bmp');
% objdir='E:\B\附件1\';
% bgfile=[objdir,['00' int2str(i)],'.bmp'];
% lengthf=length(files);
% for i=1:lengthf
%     img=imread(strcat('E:\B\附件1\',files(i).name));
%     imshow(img);
% end
% bg=imread([path,files(1).name]);
% imshow(bg);
% imageinfo([path,files(1).name])
xsmat=ones(19,19);
for i=1:19
    imi=imread([path,files(i).name]);
    [mrow,mcol]=size(imi);
    for j=1:19
        imj=imread([path,files(j).name]);
        timi=im2double(imi);
        timj=im2double(imj);
        xssum=sum(abs(timi(:,mcol)-timj(:,1)));%计算图像i和图像j的相似度。
        xsmat(i,j)=xssum;
    end
end
first=0;last=0;
for i=1:19
    img=imread([path,files(i).name]);
   [ro,co]=size(img);
   tot=0;
   for j=1:ro
       if(img(j,1)==255)
           tot=tot+1;
       end
   end
   if tot==ro
       first=i;
       break;
   end
end
que=[];
que=[que first];
flag=1;
pos=1;
tf=first;
while(flag==1)
    t=255*255*254;
    tp=0;
    for i=1:19
        if i~=tf
            if t>xsmat(tf,i)
                t=xsmat(tf,i);
                tp=i;
            end
        end
    end
    tf=tp;
    pos=pos+1;
    que=[que tp];
    if pos==19
        flag=0;
    end
    
end

tim=[];
for i=1:19
    im=imread([path files(que(i)).name]);
    tim=[tim im];
end
imshow(tim);

  Photo1_2:

clear;clc;
path='E:\B\附件2\';
files=dir('E:\B\附件2\*.bmp');
xsmat=ones(19,19);
for i=1:19
    imi=imread([path,files(i).name]);
    [mrow,mcol]=size(imi);
    for j=1:19
        imj=imread([path,files(j).name]);
        timi=im2double(imi);
        timj=im2double(imj);
        xssum=sum((timi(:,mcol)-timj(:,1)).^2);%计算图像i和图像j的相似度。
        xsmat(i,j)=xssum;
    end
end
first=0;last=0;
for i=1:19
    img=imread([path,files(i).name]);
   [ro,co]=size(img);
   tot=0;
   for j=1:ro
       if(img(j,1)==255)
           tot=tot+1;
       end
   end
   if tot==ro
       first=i;
       break;
   end
end
que=[]
que=[que first];
flag=1;
pos=1;
tf=first;
while(flag==1)
    t=255*255*254;
    tp=0;
    for i=1:19
        if i~=tf
            if t>xsmat(tf,i)
                t=xsmat(tf,i);
                tp=i;
            end
        end
    end
    tf=tp;
    pos=pos+1;
    que=[que tp];
    if pos==19
        flag=0;
    end
    
end

tim=[];
for i=1:19
    im=imread([path files(que(i)).name]);
    tim=[tim im];
end
imshow(tim);

  

photo2_1_a:

clear;clc;
path='E:\B\附件3\';
files=dir('E:\B\附件3\*.bmp');
fl=length(files);

eig=[];%特征向量矩阵
for i=1:fl
    imi=imread([path,files(i).name]);
    [ro,co]=size(imi);
    thresh=graythresh(imi);%图像二值化;
    imj=im2bw(imi,thresh);
    t=[];
    for j=1:ro
        if min(imj(j,:))==0
            t=[  t 0];
        else
            t=[t 1];
        end
    end
    eig=[eig t'];
end


P=eig;
net=newc(minmax(P),11,0.1);
net.trainParam.epochs=200;
net=init(net);
% net=newsom(P,[11]);
net=train(net,P);
Y=sim(net,P);
y=vec2ind(Y);
% y=kmeans(P',11);


% 测试分类
te=[];
for i=1:11
    tot=0;
    for j=1:209
        if y(j)==i
            tot=tot+1;
        end
    end
    te=[te tot];
end


c1=[];c2=[];c3=[];c4=[];c5=[];c6=[];c7=[];c8=[];c9=[];c10=[];c11=[];
for i=1:fl
    if y(i)==1 c1=[c1 i];
    elseif y(i)==2 c2=[c2 i];
    elseif y(i)==3 c3=[c3 i];
    elseif y(i)==4 c4=[c4 i];
    elseif y(i)==5 c5=[c5 i];
    elseif y(i)==6 c6=[c6 i];
    elseif y(i)==7 c7=[c7 i];
    elseif y(i)==8 c8=[c8 i];
    elseif y(i)==9 c9=[c9 i];
    elseif y(i)==10 c10=[c10 i];
    else c11=[c11 i];
    end
end
c1=sort(c1);c2=sort(c2);c3=sort(c3);c4=sort(c4);c5=sort(c5);c6=sort(c6);c7=sort(c7);c8=sort(c8);c9=sort(c9);c10=sort(c10);c11=sort(c11);
%人工干预

  

photo2_1_b:

% 求最左边的图片
leftque=[];
cntnum=zeros(fl,1);
maxnum=0;
tmax=0;tpos=0;
for i=1:fl
    imi=imread([path,files(i).name]);
    [ro,co]=size(imi);
    thresh=graythresh(imi);%图像二值化;
    imj=im2bw(imi,thresh);
    tot=0;
    for j=1:co
        if min(imj(:,j))==1
            tot=tot+1;
        else
            break;
        end
    end
    if i==8
        maxnum=tot;
    end
    if tmax<tot
        tmax=tot;tpos=i;
    end
    cntnum(i)=tot;
end

for i=1:fl
    if cntnum(i)>=maxnum
        leftque=[leftque i];
    end
end
leftque;

%构建209*209相似度矩阵
x2smat=ones(209,209);
for i=1:fl
    imi=imread([path,files(i).name]);
%     thresh=graythresh(imi);
%     tmi=im2bw(imi,thresh);
    for j=1:fl
        imj=imread([path,files(j).name]);
%         thresh=graythresh(imj);
%         tmj=im2bw(imj,thresh);
        timi=im2double(imi);
        timj=im2double(imj);
        x2ssum=sum(abs(timi(:,co)-timj(:,1)));
        x2smat(i,j)=x2ssum;
    end
end 

  

photo2_1_c:

%%组合每行碎片
c=[ 8    1  33    46    54    57    69    71    94   127 138   139   154   159   167   175   176   197   209;
    169    2    19    24    27    31    42    51    63    77  87    88   101   121   143   148  180   192  196;
    50    3    12    23    29    55    58    66    92  96   119   130   142   144   179   187   189   191    193;
    15    4    13    32    40    52    74    83   108  116   129   135   136   160   161   170   177   200  204;
    90    5    41   102   103   109   114   115   118  120   124      141   147   152   155   156   186  195   208;
    126    14    17    22    67   107   110   111  140   146   151     158   174   182   183   185   188   198   205 ;
    30     6    11    38    45    49    56    60    65    76   93    99   105   112   172   173   181   202   207;
    62    7    20    21    37    53    64    68    70    73  79    80    97   100   117   132   163   164   178;
    39    9    10    25    26    36   47    75    82   89   104   106   123   131   149   162   168   190 194;
    72    16    18    28    34    61   81    84    86   133   134   153   157   166   171   199   201   203   206;
    95    35    43    44    48    59    78    85    91     98   113   122   125   128   137   145   150   165   184];
p1=c(1,:);p2=c(2,:);p3=c(3,:);p4=c(4,:);p5=c(5,:);p6=c(6,:);p7=c(7,:);p8=c(8,:);
p9=c(9,:);p10=c(10,:);p11=c(11,:);

q1=cocom(p1);%根据相似度求每列的顺序
q2=cocom(p2);q3=cocom(p3);q4=cocom(p4);q5=cocom(p5);q6=cocom(p6);q7=cocom(p7);
q8=cocom(p8);q9=cocom(p9);q10=cocom(p10);q11=cocom(p11);
f1=[8 209 139 159 127  69 176  46 175 1 138 54  57  94 154 71 167  33 197 ];
f2=[169  101  77  63 143  31  42  24 148 192 51   180   121    87 196  27 2    88   19];
f3=[50 55  66 144 187  3  58 193 179 119   191    96    12    23   130    29    92   189   142];
f4=[15   129     4   160    83   200   136    13    74 161   204   170   135    40    32    52   108   116 177];
f5=[90   147   103   155   115    41   152   208   156 141   186   109   118     5   102   114   195   120 124];
f6=[126    14   183   110   198    17   185   111   188  67   107   151    22   174   158   182   205   140  146];
f7=p7(q7);
f8=[62    20    79    68    70   100   163    97   132 80    64   117   164    73     7   178    21    53 37];
f9=[39   149    47   162    25    36    82   190   123 104   131   194    89   168    26     9    10   106  75];
f10=[72   157    84   133   201    18    81    34   203   199    16   134 171   206    86   153   166    28    61];
f11=[95    35    85   184    91    48   122    43   125   145    78   113   150    98   137   165   128    59    44];
tm1=[];tm2=[];tm3=[];tm4=[];tm5=[];tm6=[];tm7=[];tm8=[];tm9=[];tm10=[];tm11=[];
for i=1:19
    tpim=imread([path,files(f1(i)).name]);
    tm1=[tm1 tpim];
    tpim1=imread([path,files(f2(i)).name]);
    tm2=[tm2 tpim1];
    tpim2=imread([path,files(f3(i)).name]);
    tm3=[tm3 tpim2];
    tpim3=imread([path,files(f4(i)).name]);
    tm4=[tm4 tpim3];
    tpim4=imread([path,files(f5(i)).name]);
    tm5=[tm5 tpim4];
    tpim5=imread([path,files(f6(i)).name]);
    tm6=[tm6 tpim5];
    tpim6=imread([path,files(f7(i)).name]);
    tm7=[tm7 tpim6];
    tpim7=imread([path,files(f8(i)).name]);
    tm8=[tm8 tpim7];
    tpim8=imread([path,files(f9(i)).name]);
    tm9=[tm9 tpim8];
    tpim9=imread([path,files(f10(i)).name]);
    tm10=[tm10 tpim9];
    tpim10=imread([path,files(f11(i)).name]);
    tm11=[tm11 tpim10];
end

%%组合11张横向碎片

%first 50 tm3;
[a,b]=size(tm3);
eu=[];ed=[];%tm矩阵的第一行和最后一行;
eu=[eu tm1(1,:);tm2(1,:);tm3(1,:);tm4(1,:);tm5(1,:);tm6(1,:);tm7(1,:);tm8(1,:);tm9(1,:);tm10(1,:);tm11(1,:)];
ed=[ed tm1(a,:);tm2(a,:);tm3(a,:);tm4(a,:);tm5(a,:);tm6(a,:);tm7(a,:);tm8(a,:);tm9(a,:);tm10(a,:);tm11(a,:)];
eu=im2double(eu);
ed=im2double(ed);
ei=zeros(11,11);
for i=1:11
    for j=1:11
        eimat(i,j)=sum(abs(ed(i,:)-eu(j,:)));
    end
end

first=3;
q2ue=[];ff=ones(11,1);
q2ue=[q2ue first];ff(first)=-1;
flag=1;
pos=1;
tf=first;
while(flag==1)
    t=255*255*254;
    tp=0;
    for i=1:11
        if i~=tf && ff(i)~=-1;
            if t>eimat(tf,i)
                t=eimat(tf,i);
                tp=i;
            end
        end
    end
    tf=tp;ff(tf)=-1;
    pos=pos+1;
    q2ue=[q2ue tp];
    if pos==11
        flag=0;
    end 
end
% imshow(tm4)
fim=[tm3;tm8;tm2;tm9;tm10;tm4;tm11;tm6;tm7;tm1;tm5;];
imshow(fim);

  

cocom函数:

function fc=cocom(p)%根据相似度求行的排列顺序
load x2smat.mat
p1=p;tp1=p1;
fc=[1];
lo=1;tp1(1)=-1;
flag=1;tot=0;
while(flag==1)    
    tot=tot+1;
    tpo=0;
    min=255*255*255;
    for i=1:19
        if i~=lo && tp1(i)~=-1
            if min>x2smat(p1(lo),p1(i))
                min=x2smat(p1(lo),p1(i));
                tpo=i;
            end
        end
    end
    fc=[fc tpo];
    lo=tpo;
    tp1(lo)=-1;
    if tot==18
        flag=0;
    end
end
% la=0;
% for i=1:19
%     if tp1(i)~=-1
%         la=i;
%     end
% end
% fc=[fc la];

  Photo2_2_a:

clear;clc;
path='E:\B\附件4\';
files=dir('E:\B\附件4\*.bmp');
fl=length(files);

eig=[];%特征向量矩阵
for i=1:fl
    imi=imread([path,files(i).name]);
    [ro,co]=size(imi);
    thresh=graythresh(imi);%图像二值化;
    imj=im2bw(imi,thresh);
    t=[];
    for j=1:ro
        if sum(imj(j,:))<56
            t=[t 1];
        else
            t=[t 0];
        end
    end
    eig=[eig t'];
end


P=eig;
net=newc(minmax(P),11,0.1);
net.trainParam.epochs=250;
net=init(net);
% net=newsom(P,[11]);
net=train(net,P);
Y=sim(net,P);
y=vec2ind(Y);
% y=kmeans(P',11);


% 测试分类
te=[];
for i=1:11
    tot=0;
    for j=1:209
        if y(j)==i
            tot=tot+1;
        end
    end
    te=[te tot];
end


c1=[];c2=[];c3=[];c4=[];c5=[];c6=[];c7=[];c8=[];c9=[];c10=[];c11=[];
for i=1:fl
    if y(i)==1 c1=[c1 i];
    elseif y(i)==2 c2=[c2 i];
    elseif y(i)==3 c3=[c3 i];
    elseif y(i)==4 c4=[c4 i];
    elseif y(i)==5 c5=[c5 i];
    elseif y(i)==6 c6=[c6 i];
    elseif y(i)==7 c7=[c7 i];
    elseif y(i)==8 c8=[c8 i];
    elseif y(i)==9 c9=[c9 i];
    elseif y(i)==10 c10=[c10 i];
    else c11=[c11 i];
    end
end
c1=sort(c1);c2=sort(c2);c3=sort(c3);c4=sort(c4);c5=sort(c5);c6=sort(c6);c7=sort(c7);c8=sort(c8);c9=sort(c9);c10=sort(c10);c11=sort(c11);
%人工干预

  Photo2_2_b:

% 求最左边的图片
leftque=[];
cntnum=zeros(fl,1);
maxnum=0;
tmax=0;tpos=0;
for i=1:fl
    imi=imread([path,files(i).name]);
    [ro,co]=size(imi);
    thresh=graythresh(imi);%图像二值化;
    imj=im2bw(imi,thresh);
    tot=0;
    for j=1:co
        if min(imj(:,j))==1
            tot=tot+1;
        else
            break;
        end
    end
    if i==172
        maxnum=tot;
    end
    if tmax<tot
        tmax=tot;tpos=i;
    end
    cntnum(i)=tot;
end

for i=1:fl
    if cntnum(i)>0
        leftque=[leftque i];
    end
end
leftque=[19    20    70    81  86   132    159   171   191   201   208];leftque=leftque+1;

%构建209*209相似度矩阵
x2smat=ones(209,209);
for i=1:fl
    imi=imread([path,files(i).name]);
%     thresh=graythresh(imi);
%     tmi=im2bw(imi,thresh);
    for j=1:fl
        imj=imread([path,files(j).name]);
%         thresh=graythresh(imj);
%         tmj=im2bw(imj,thresh);
        timi=im2double(imi);
        timj=im2double(imj);
        x2ssum=sum(abs(timi(:,co)-timj(:,1)));
        x2smat(i,j)=x2ssum;
    end
end 

  Photo2_2_c:

%%组合每行碎片
c=[ 
10	11	17	19	36	43	45	56	57	67	75	84	135	146	153	158	172	184	206;
6	25	30	31	38	41	47	52	59	60	87	93	99	108	118	128	151	159	187;
16	21	37	42	44	46	74	77	80	109	117	136	137	144	162	174	180	200	208;
2	32	39	51	54	64	86	98	121	124	130	139	140	154	160	161	176	188	204;
8	22	34	50	55	62	63	113	119	120	134	143	163	169	170	190	193	198	209;
7	18	27	29	79	81	92	95	101	102	104	114	147	149	165	171	197	199	202;
20	23	58	72	83	89	94	106	115	122	127	142	152	156	166	177	183	195	203;
4	14	26	28	35	70	96	111	112	131	133	145	164	167	168	179	182	189	207;
9	15	24	48	61	69	71	85	91	97	100	110	123	138	157	173	175	186	196;
1	13	49	53	73	78	82	88	90	103	116	125	126	129	132	141	178	194	201;
3	5	12	33	40	65	66	68	76	105	107	148	150	155	181	185	191	192	205];
p1=c(1,:);p2=c(2,:);p3=c(3,:);p4=c(4,:);p5=c(5,:);p6=c(6,:);p7=c(7,:);p8=c(8,:);
p9=c(9,:);p10=c(10,:);p11=c(11,:);

q1=cocom(p1);%根据相似度求每列的顺序
q2=cocom(p2);q3=cocom(p3);q4=cocom(p4);q5=cocom(p5);q6=cocom(p6);q7=cocom(p7);
q8=cocom(p8);q9=cocom(p9);q10=cocom(p10);q11=cocom(p11);
f1=[172	43	67	206	11	158	75	146	84	135	56	19	57	36	17	10	184	153	45];
f2=[87	52	108	30	41	159	187	99	25	118	151	6	60	59	93	31	38	47	128];
f3=[21	42	109	117	137	74	37	208	136	16	77	44	200	46	174	80	162	180	144];
f4=[160	140	2	130	64	139	154	54	39	124	121	176	86	51	161	188	98	204	32];
f5=[209	22	8	50	62	120	34	143	169	63	170	55	193	134	119	190	163	198	113];
f6=[202	149	171	197	199	95	114	165	79	104	92	81	102	27	101	7	18	29	147];
f7=[20	195	94	142	89	122	127	106	156	115	177	183	152	23	58	203	72	166	83];
f8=[133	182	96	70	168	164	167	189	112	145	207	4	131	35	14	111	26	28	179];
f9=[71	85	61	15	69	175	138	196	9	48	173	157	97	24	100	123	91	186	110];
f10=[82	78	129	201	132	53	126	141	194	88	90	49	73	13	178	125	1	103	116];
f11=[192	76	12	155	191	185	3	105	181	65	107	5	150	33	205	66	40	68	148];
tm1=[];tm2=[];tm3=[];tm4=[];tm5=[];tm6=[];tm7=[];tm8=[];tm9=[];tm10=[];tm11=[];
for i=1:19
    tpim=imread([path,files(f1(i)).name]);
    tm1=[tm1 tpim];
    tpim1=imread([path,files(f2(i)).name]);
    tm2=[tm2 tpim1];
    tpim2=imread([path,files(f3(i)).name]);
    tm3=[tm3 tpim2];
    tpim3=imread([path,files(f4(i)).name]);
    tm4=[tm4 tpim3];
    tpim4=imread([path,files(f5(i)).name]);
    tm5=[tm5 tpim4];
    tpim5=imread([path,files(f6(i)).name]);
    tm6=[tm6 tpim5];
    tpim6=imread([path,files(f7(i)).name]);
    tm7=[tm7 tpim6];
    tpim7=imread([path,files(f8(i)).name]);
    tm8=[tm8 tpim7];
    tpim8=imread([path,files(f9(i)).name]);
    tm9=[tm9 tpim8];
    tpim9=imread([path,files(f10(i)).name]);
    tm10=[tm10 tpim9];
    tpim10=imread([path,files(f11(i)).name]);
    tm11=[tm11 tpim10];
end

%组合11张横向碎片

%first 50 tm3;
[a,b]=size(tm3);
eu=[];ed=[];%tm矩阵的第一行和最后一行;
eu=[eu tm1(1,:);tm2(1,:);tm3(1,:);tm4(1,:);tm5(1,:);tm6(1,:);tm7(1,:);tm8(1,:);tm9(1,:);tm10(1,:);tm11(1,:)];
ed=[ed tm1(a,:);tm2(a,:);tm3(a,:);tm4(a,:);tm5(a,:);tm6(a,:);tm7(a,:);tm8(a,:);tm9(a,:);tm10(a,:);tm11(a,:)];
eu=im2double(eu);
ed=im2double(ed);
ei=zeros(11,11);
for i=1:11
    for j=1:11
        eimat(i,j)=sum(abs(ed(i,:)-eu(j,:)));
    end
end

first=3;
q2ue=[];ff=ones(11,1);
q2ue=[q2ue first];ff(first)=-1;
flag=1;
pos=1;
tf=first;
while(flag==1)
    t=255*255*254;
    tp=0;
    for i=1:11
        if i~=tf && ff(i)~=-1;
            if t>eimat(tf,i)
                t=eimat(tf,i);
                tp=i;
            end
        end
    end
    tf=tp;ff(tf)=-1;
    pos=pos+1;
    q2ue=[q2ue tp];
    if pos==11
        flag=0;
    end 
end
% imshow(tm7)
fim=[tm11;tm6;tm2;tm7;tm4;tm3;tm5;tm9;tm8;tm1;tm10];
imshow(fim);

  

posted @ 2018-08-27 18:58  ╰追憶似水年華ぃ╮  阅读(781)  评论(0编辑  收藏  举报