%% test start
close all;
clear all;
% read image
folder = 2;
num_img = 6;
% read image
time_total = 0;
time_correction = 0;
time_blending = 0;
for i=1:num_img
file_name = ['input\',num2str(folder), '\image', num2str(i),'.jpg'];
image{i} = imread(file_name);
end
im1 = double(image{1});
[H, W, c] = size(im1);
for ii = 2: num_img
im2 = double(image{ii});
%Note: every images has 200 pixel shift on x direction relative to previous
%image, no y direction shift
%get transformation of image 2 to fit image 1
%x is horizontal; y is vertical
shift_x = W - 200;
shift_y = 0;
time_start = cputime;
[Bi, Si_cb, Si_cr, w, h] = get_overlap_mean_yuv (im1, im2, shift_x, shift_y);
B(ii-1,:) = Bi;
S_cb(ii-1,:) = Si_cb;
S_cr(ii-1,:) = Si_cr;
overlap_w(ii-1) = w;
overlap_h (ii-1) = h;
time_end = cputime;
time_correction = time_correction + (time_end - time_start);
time_total = time_total + time_correction;
im1 = im2;
end
time_start = cputime;
%get correction parameters
gamma = getCorrParameters (B, num_img);
a_cb = getCorrParameters (S_cb, num_img);
a_cr = getCorrParameters (S_cr, num_img);
time_end = cputime;
time_correction = time_correction + (time_end - time_start);
time_total = time_total + time_correction;
%Do color and luminance
im1 = image{1};
time_start = cputime;
timeCS = cputime;
YCBCR = rgb2yuv(im1);
YCBCR = double (YCBCR);
YCBCR(:,:,1) = YCBCR(:,:,1).^gamma(1);
YCBCR(:,:,2) = YCBCR(:,:,2).*a_cb(1);
YCBCR(:,:,3) = YCBCR(:,:,3).*a_cr(1);
% YCBCR = uint8(YCBCR);
im1 = yuv2rgb(YCBCR);
timeCE = cputime;
time_correction = time_correction + (timeCE - timeCS);
im1 = double(im1);
% gamma(1) = 1;
% a_cb(1) = 1;
% a_cr(1) = 1;
for ii = 2: size(image,2)
im2 = image{ii};
timeCS = cputime;
YCBCR = rgb2yuv(im2);
YCBCR = double (YCBCR);
YCBCR(:,:,1) = YCBCR(:,:,1).^(gamma(ii));
YCBCR(:,:,2) = YCBCR(:,:,2).*(a_cb(ii));
YCBCR(:,:,3) = YCBCR(:,:,3).*(a_cr(ii));
% YCBCR = uint8(YCBCR);
im2 = yuv2rgb(YCBCR);
timeCE = cputime;
time_correction = time_correction + (timeCE - timeCS);
im2 = double(im2);
%get transformation of image 2 to fit image 1
%x is horizontal; y is vertical
shift_x = W - 200;
shift_y = 0;
overlap_w = size(im1,2) - shift_x;
overlap_h = size(im1,1) - shift_y;
if shift_y >= 0
overlap_im1 = im1(1:overlap_h, (shift_x+1):size(im1,2),:);
overlap_im2 = im2((size(im2,1)-overlap_h+1):size(im2,1), 1:overlap_w,:);
else
overlap_im1 = im1(1:overlap_h, (shift_x+1):size(im1,2),:);
overlap_im2 = im2((size(im2,1)-overlap_h+1):size(im2,1), 1:overlap_w,:);
end
squared_diff = zeros(overlap_h, overlap_w);
accumulate_D = zeros(overlap_h, overlap_w);
color_diff = zeros(overlap_h, 3);
%need to add more condition for robustness
for i=1:overlap_h
for j = 1:overlap_w
squared_diff(i,j) = (overlap_im1(i,j,1) - overlap_im2(i,j,1))^2;
if i==1
accumulate_D(i,j) = squared_diff (i,j);
else
if j==1
neighbor = zeros(1,2);
neighbor(1) = accumulate_D (i-1,j);
neighbor(2) = accumulate_D (i-1,j+1);
elseif j== overlap_w
neighbor = zeros(1,2);
neighbor(1) = accumulate_D (i-1,j-1);
neighbor(2) = accumulate_D (i-1,j);
else
neighbor = zeros(1,3);
neighbor(1) = accumulate_D (i-1,j-1);
neighbor(2) = accumulate_D (i-1,j);
neighbor(3) = accumulate_D (i-1,j+1);
end
accumulate_D(i,j) = squared_diff (i,j) + min(neighbor);
end
end
end
%finding seam
seamindex = zeros(1,overlap_h);
[mindiff, seamindex(overlap_h)] = min(accumulate_D(overlap_h,:));
% mindiff
% seamindex(overlap_h)
% squared_diff(overlap_h, seamindex(overlap_h))
for i=(overlap_h-1):-1:1
if seamindex(i+1)==1
if(accumulate_D(i,seamindex(i+1)) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1);
mindiff = accumulate_D(i,seamindex(i));
elseif (accumulate_D(i,seamindex(i+1)+1) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1)+1;
mindiff = accumulate_D(i,seamindex(i));
end
elseif seamindex(i+1) == overlap_w
if (accumulate_D(i,seamindex(i+1)-1) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1)-1;
mindiff = accumulate_D(i,seamindex(i));
elseif(accumulate_D(i,seamindex(i+1)) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1);
mindiff = accumulate_D(i,seamindex(i));
end
else
if (accumulate_D(i,seamindex(i+1)-1) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1)-1;
mindiff = accumulate_D(i,seamindex(i));
elseif(accumulate_D(i,seamindex(i+1)) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1);
mindiff = accumulate_D(i,seamindex(i));
elseif (accumulate_D(i,seamindex(i+1)+1) + squared_diff(i+1,seamindex(i+1)))== mindiff
seamindex(i) = seamindex(i+1)+1;
mindiff = accumulate_D(i,seamindex(i));
end
end
end
for i=1:overlap_h
% overlap_im1 (i, seamindex(i),:) = 0;
temp(i,1:seamindex(i)-1,:) = overlap_im1 (i,1:seamindex(i)-1,:);
temp(i, seamindex(i),:) = 0;
temp(i,(seamindex(i)+1):overlap_w,:) = overlap_im2(i,(seamindex(i)+1):overlap_w,:);
color_diff(i,:) = overlap_im1(i,seamindex(i),:) - overlap_im2(i,seamindex(i),:);
end
% figure; imshow(uint8(temp));
timeS = cputime;
%------Color blending--------------
for i=1:overlap_h
for j= (seamindex(i)+1):size(im2,2)
total = 0;
% weight = zeros(overlap_h,1);
blendvalue = zeros(3,1);
for k=1:overlap_h
%--1. original implementation start----------
% dist(k) = ((i-k)^2 + (j-seamindex(k))^2)^0.5;
%---2. use |x|+|y| start----------
dist(k) = abs(i-k)+ abs(j-seamindex(k));
blendvalue(1) = blendvalue(1) + 1/dist(k)*color_diff(k,1);
blendvalue(2) = blendvalue(2) + 1/dist(k)*color_diff(k,2);
blendvalue(3) = blendvalue(3) + 1/dist(k)*color_diff(k,3);
total = total+1/dist(k);
end
im2(i,j,1) = im2(i,j,1)+ blendvalue(1)/total;
im2(i,j,2) = im2(i,j,2)+ blendvalue(2)/total;
im2(i,j,3) = im2(i,j,3)+ blendvalue(3)/total;
end
end
timeE = cputime;
time_blending = time_blending + (timeE - timeS);
%------Color blending end--------------
%Stitch process
W = size(im1,2)+ size(im2,2) - overlap_w;
H = overlap_h;
stitched_image = zeros(H,W,3);
for i = 1:H
im1end = size(im1, 2) - overlap_w + seamindex(i);
im2start = seamindex(i)+1;
if shift_y >= 0
stitched_image (i, 1:im1end,:) = im1(i, 1:im1end, :);
stitched_image (i, im1end+1:W,:) = im2((size(im2,1)-H+i), im2start:size(im2,2), :);
else
stitched_image (i, 1:im1end,:) = im1((size(im1,1)-H+i), 1:im1end, :);
stitched_image (i, im1end+1:W,:) = im2(i, im2start:size(im2,2), :);
end
%for debug purpose
% stitched_image (i, size(im1,2)-overlap_w, :) = 0;
% stitched_image (i, size(im1,2), :) = 0;
end
im1 = stitched_image;
% figure; imshow(uint8(im1));
end
time_end = cputime;
time_total = time_total + (time_end - time_start)
time_correction
time_blending
figure; imshow(uint8(stitched_image));
out_n
全景图像颜色校正代码
1星 需积分: 50 24 浏览量
2018-01-29
17:46:13
上传
评论 8
收藏 4.81MB ZIP 举报
普通网友
- 粉丝: 1
- 资源: 8