clear ;close all;clc
imrgb1 = imread('09.jpg');
imrgb2 = imread('10.jpg');
imgray1 = rgb2gray(imrgb1);
imgray2 = rgb2gray(imrgb2);
figure
imshow(imgray1);
figure
imshow(imgray2);
[im_h ,im_w ] = size(imgray1);
W = im_w ;
H = im_h ;
n= 12 ;
f = W/2/tan(3.14/n) ;
a = 2*atan(W/2/f) ;
new_w = a*f ;
imtest1 = zeros(im_h, new_w);
imtest2 = zeros(im_h, new_w);
for i=1:im_h
for j=1:new_w
x1=j;
y1=i;
x = f*tan( ( x1-f*a/2 )/f ) + W/2 ;
k = sqrt(f*f + (W/2-x)*(W/2-x) ) ;
y = k*( y1-H/2 )/f + H/2 ;
if x<1||y<1||x>im_w-1||y>im_h-1
continue;
end
if x>1&&y>1&&x<im_w&&y<im_h
ii= floor(y) ;
jj= floor(x) ;
u = x - jj ;
v = y - ii ;
% imtest1(y1,x1) =(1-u)*(1-v)*imgray1(ii,jj) + u*(1-v)*imgray1(ii,jj+1) + ...
% (1-u)*v*imgray1(ii+1,jj) + u*v*imgray1(ii+1,jj+1) ;
% imtest2(y1,x1) =(1-u)*(1-v)*imgray2(ii,jj) + u*(1-v)*imgray2(ii,jj+1) + ...
% (1-u)*v*imgray2(ii+1,jj) + u*v*imgray2(ii+1,jj+1) ;
imtest1(y1,x1) = imgray1(ii,jj) ;
imtest2(y1,x1) = imgray2(ii,jj) ;
end
end
end
figure
imshow(uint8(imtest1));
figure
imshow(uint8(imtest2));
imgray1 = imtest1;
imgray2 = imtest2;
[im_h ,im_w ] = size(imgray1);
oriX = im_w-70 ;
oriY = im_h/2 ;
imcrop1 = imcrop( imgray1 ,[oriX oriY 49 49 ] );
figure
imshow(uint8(imcrop1));
imcrop1 =double (imcrop1) ;
[c_h , c_w ] = size(imcrop1);
imcrop12 = imcrop1.*imcrop1 ;
c1s2 = sum(sum(imcrop12));
c1s = sum(sum(imcrop1));
c1avg = mean(mean(imcrop1));
bb = sqrt(c1s2 - 50*50*c1avg*c1avg ) ;
fmatch = zeros(im_h,im_w);
minval=0;
ssd = 0;
% figure
for i=oriY-30:oriY+30
for j=1:im_w-c_w
imcrop2 = imcrop( imgray2 ,[j i 49 49 ] );
% imshow(imcrop2);
imcrop2 = double (imcrop2) ;
c2avg = mean(mean(imcrop2));
imcrop22 = imcrop2.*imcrop2;
c2s2 = sum(sum(imcrop22));
imcropmu = imcrop2.*imcrop1 ;
f1 = sum(sum(imcropmu )) - c1s* c2avg ;
f2 = sqrt( c2s2 - 50*50*c2avg*c2avg )*bb ;
fmatch(i,j) = f1/f2 ;
if(fmatch(i,j)>minval)
X_match = j ;
Y_match = i ;
ssd = fmatch(i,j);
minval = fmatch(i,j);
end
end
end
figure
mesh(fmatch);
deltaY = Y_match - oriY ;
final_h = abs(deltaY) + im_h;
final_w = oriX + (im_w - X_match );
final_im = zeros(final_h,final_w);
final_im = uint8(final_im);
if deltaY<=0
final_im(1:im_h , 1:oriX-1 ) = imgray1(1:im_h , 1:oriX-1) ;
final_im(1+abs(deltaY) :im_h+abs(deltaY) , oriX:final_w ) = imgray2(1:im_h ,X_match:im_w ) ;
else
final_im(1+abs(deltaY) :im_h+abs(deltaY) , 1:oriX-1 ) = imgray1(1:im_h , 1:oriX-1) ;
final_im(1 :im_h , oriX:final_w ) = imgray2(1:im_h ,X_match:im_w ) ;
end
figure
imshow(final_im);
imem1 = zeros(final_h, 50) ;
imem2 = zeros(final_h, 50) ;
if deltaY<=0
imem1( 1:im_h , :) = imgray1(1:im_h , oriX :oriX+49 );
imem2( 1+abs(deltaY) :im_h+abs(deltaY) , :) = imgray2(1:im_h , X_match:X_match+49 );
else
imem1( 1+abs(deltaY):im_h+abs(deltaY) , :) = imgray1(1:im_h , oriX :oriX+49 );
imem2( 1 :im_h , :) = imgray2(1:im_h , X_match :X_match+49 );
end
figure
imshow(uint8(imem1));
figure
imshow(uint8(imem2));
imem3 = zeros(final_h, 50) ;
%改为最佳缝合线
for j =1:50
a = (50-j)/50 ;
b = j/50 ;
for i =1:final_h
if abs(imem1(i,j) - imem2(i,j))<50
imem3(i,j) = a*imem1(i,j) + b*imem2(i,j);
else
imem3(i,j) = a*imem1(i,j) + b*imem2(i,j);
end
end
end
figure
imshow(uint8(imem3))
final_im(1 :final_h , oriX:oriX+49 ) = imem3 ;
figure
imshow(uint8(final_im))
% %% 最佳缝合线
% pp= 25 ;
%
%
% for i =1:final_h
% for j =1:50
%
%
% sub1 = abs( imem1(i,pp-1) - imem2(i,pp-1) ) ;
% sub2 = abs( imem1(i,pp) - imem2(i,pp) ) ;
% sub3 = abs( imem1(i,pp+1) - imem2(i,pp+1) ) ;
%
% if sub1<=sub2&&sub1<=sub3
% pp=pp-1 ;
% end
% if sub2<=sub1&&sub2<=sub3
% pp=pp ;
% end
% if sub3<=sub1&&sub3<=sub2
% pp=pp+1 ;
% end
%
% if pp>49
% pp=49;
% else
% if pp<2
% pp=2;
% end
% end
%
% a = (50-j)/50 ;
% b = j/50 ;
%
% imem3(i,j) = a*imem1(i,j) + b*imem2(i,j);
%
%
% end
% end
% figure
% imshow(uint8(imem3))
%
% final_im(1 :final_h , oriX:oriX+49 ) = imem3 ;
%
% figure
% imshow(uint8(final_im))
KKK=1;
% for i=1:im_h
% for j=1:im_w
%
%
%
% end
% end
评论0