Skip to content

Commit

Permalink
SNS repository
Browse files Browse the repository at this point in the history
  • Loading branch information
yabinzhangJohn authored and yabinzhangJohn committed Jun 27, 2016
0 parents commit c8ff1be
Show file tree
Hide file tree
Showing 172 changed files with 3,781 additions and 0 deletions.
Binary file added Brasserie_L_Aficion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Brasserie_L_Aficion_0.50_sns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions Demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
% ==== (SNS matlab code)======
% The Code (Version 1) is created by ZHANG Yabin,
% Nanyang Technological University, 2015-12-30
% which is based on the method described in the following paper
% [1] Wang, Yu-Shuen, et al. "Optimized scale-and-stretch for image resizing."
% ACM Transactions on Graphics (TOG) 27.5 (2008): 118.
% The binary code is provided on the project page:
% http://graphics.csie.ncku.edu.tw/Image_Resizing/
% The Matlab codes are for non-comercial use only.
% Note that the importance maps are slightly different from the original
% ones, and the retargeted images are influenced.

clear all; clc

im = imread('tajmahal.png');
im_SNS = imread('tajmahal_0.50_sns.png');

% im = imread('Brasserie_L_Aficion.png');
% im_SNS = imread('Brasserie_L_Aficion_0.50_sns.png');

% parameters
Ratio = 0.5;
mesh_size = 20; % using mesh_size x mesh_size quad
[h, w, ~] = size(im);
quad_num_h = floor(h/mesh_size);
quad_num_w = floor(w/mesh_size);

% the regular mesh on original image
Vertex_set_org = ImgRegualrMeshGrid(im, mesh_size);

% the importance map generation
importance_map = SNS_importanceMap(im, true); % generate the importance map
importance_quad = SNS_importanceMap_quad(importance_map, Vertex_set_org);
importance_quad = importance_quad/sum(importance_quad(:)); % the importance weight for the quad

% the naive initialization of the mesh
% retargeting on the width
Vertex_warped_initial = Vertex_set_org;
Vertex_warped_initial(:,:,2) = Vertex_warped_initial(:,:,2)*Ratio;

% the mesh grid optimization
[Vertex_updated] = ...
SNS_optimization(Vertex_set_org ,Vertex_warped_initial, importance_quad);

% warp the new image
im_warped = MeshBasedImageWarp(im, [1 Ratio], Vertex_set_org, Vertex_updated);
figure; subplot(1,2,1); imshow(im_warped); title(['My warped'], 'FontSize' , 15);
subplot(1,2,2); imshow(im_SNS); title(['Original SNS warped'], 'FontSize' , 15);

% show the mesh grid on the original image and retargeted image
% MeshGridImgPlot(im, Vertex_set_org, [0.5 0.0 0.5]);
% title(['Regular mesh grid on original image'], 'FontSize' , 15);
% MeshGridImgPlot(im_warped, Vertex_updated, [0.5 0.0 0.5]);
% title(['Warped image '], 'FontSize' , 15);




42 changes: 42 additions & 0 deletions ImgRegualrMeshGrid.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function [ Vertex_set ] = ImgRegualrMeshGrid(im, mesh_size)
% Summary of this function goes here
% Detailed explanation goes here
% The Code (Version 1) is created by ZHANG Yabin,
% Nanyang Technological University, 2015-12-30
% which is based on the method described in the following paper
% [1] Wang, Yu-Shuen, et al. "Optimized scale-and-stretch for image resizing."
% ACM Transactions on Graphics (TOG) 27.5 (2008): 118.
% The binary code is provided on the project page:
% http://graphics.csie.ncku.edu.tw/Image_Resizing/
% The Matlab codes are for non-comercial use only.
% Note that the importance maps are slightly different from the original
% ones, and the retargeted images are influenced.

[h, w, ~] = size(im);
quad_num_h = floor(h/mesh_size);
quad_num_w = floor(w/mesh_size);
Remain_h = h - mesh_size*quad_num_h;
Remain_w = w - mesh_size*quad_num_w;

Start_point = 0;

Vertex_set = zeros(quad_num_h+1, quad_num_w+1,2);
for vect_i = 1:quad_num_h+1
for vect_j = 1:quad_num_w+1
if(vect_i ~= 1)
Vertex_set(vect_i, vect_j, 1) = ...
min(Start_point+mesh_size*(vect_i-1)+round((vect_i-1)/quad_num_h*Remain_h), h);
else
Vertex_set(vect_i, vect_j, 1) = Start_point;
end
if(vect_j ~= 1)
Vertex_set(vect_i, vect_j, 2) = ...
min(Start_point+mesh_size*(vect_j-1)+round((vect_j-1)/quad_num_w*Remain_w), w);
else
Vertex_set(vect_i, vect_j, 2) = Start_point;
end
end
end

end

108 changes: 108 additions & 0 deletions MeshBasedImageWarp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
function [im_warped] = MeshBasedImageWarp(im, ratio ,Vertex_set_org, Vertex_set_warped)
% Summary of this function goes here
% Detailed explanation goes here
% The Code (Version 1) is created by ZHANG Yabin,
% Nanyang Technological University, 2015-12-30
% which is based on the method described in the following paper
% [1] Wang, Yu-Shuen, et al. "Optimized scale-and-stretch for image resizing."
% ACM Transactions on Graphics (TOG) 27.5 (2008): 118.
% The binary code is provided on the project page:
% http://graphics.csie.ncku.edu.tw/Image_Resizing/
% The Matlab codes are for non-comercial use only.
% Note that the importance maps are slightly different from the original
% ones, and the retargeted images are influenced.

% ratio is the target ratio [h_ratio, w_ratio]
tic

[h, w, ~] = size(im);
quad_num_h = size(Vertex_set_org,1) -1;
quad_num_w = size(Vertex_set_org,2) -1;
% GridWarped = NaN(round(h*ratio(1)), round(w*ratio(2)), 2);
GridWarped = zeros(round(h*ratio(1)), round(w*ratio(2)), 2);

for Quad_h = 1:quad_num_h
for Quad_w = 1:quad_num_w
V1 = [Vertex_set_org(Quad_h, Quad_w, 2) Vertex_set_org(Quad_h, Quad_w, 1)];
V2 = [Vertex_set_org(Quad_h, Quad_w+1, 2) Vertex_set_org(Quad_h, Quad_w+1, 1)];
V3 = [Vertex_set_org(Quad_h+1, Quad_w, 2) Vertex_set_org(Quad_h+1, Quad_w, 1)];
V4 = [Vertex_set_org(Quad_h+1, Quad_w+1, 2) Vertex_set_org(Quad_h+1, Quad_w+1, 1)];

V1w = [Vertex_set_warped(Quad_h, Quad_w, 2) Vertex_set_warped(Quad_h, Quad_w, 1)];
V2w = [Vertex_set_warped(Quad_h, Quad_w+1, 2) Vertex_set_warped(Quad_h, Quad_w+1, 1)];
V3w = [Vertex_set_warped(Quad_h+1, Quad_w, 2) Vertex_set_warped(Quad_h+1, Quad_w, 1)];
V4w = [Vertex_set_warped(Quad_h+1, Quad_w+1, 2) Vertex_set_warped(Quad_h+1, Quad_w+1, 1)];

Vmatrix = [V1 1; V2 1; V3 1; V4 1]';
Vwmatrix = [V1w 1; V2w 1; V3w 1; V4w 1]';

% compute the projective matrix for the entire quad face
% svd based solution
Amatrix = zeros(8,9);
for i = 1:4
x = Vmatrix(1,i); y = Vmatrix(2,i);
xw = Vwmatrix(1,i); yw = Vwmatrix(2,i);
Amatrix(2*i-1,:) = [-xw -yw -1 0 0 0 x*xw x*yw x];
Amatrix(2*i,:) = [0 0 0 -xw -yw -1 y*xw y*yw y];
end
[~, ~, v] = svd(Amatrix); ColV = v(:,9);
H = [ColV(1:3)'; ColV(4:6)'; ColV(7:9)']/ColV(9);

% % matrix factorization based solution
% Amatrix = zeros(8,9);
% for i = 1:4
% x = Vmatrix(1,i); y = Vmatrix(2,i);
% xw = Vwmatrix(1,i); yw = Vwmatrix(2,i);
% Amatrix(2*i-1,:) = [-xw -yw -1 0 0 0 x*xw x*yw x];
% Amatrix(2*i,:) = [0 0 0 -xw -yw -1 y*xw y*yw y];
% end
% A_m = Amatrix(:,1:8); B_m = -1*Amatrix(:,9);
% [L, U] = lu(A_m);
% H_foo = U\(L\B_m);
% H = [H_foo(1:3)'; H_foo(4:6)'; H_foo(7:8)' 1];


W_min = min([Vwmatrix(1,:)]); W_max = max([Vwmatrix(1,:)]);
H_min = min([Vwmatrix(2,:)]); H_max = max([Vwmatrix(2,:)]);

for grid_h = max(floor(H_min),1): ceil(H_max)
for grid_w = max(floor(W_min),1): ceil(W_max)

if( (grid_w - V1w(1))*(V3w(2) - V1w(2)) - ...
(grid_h - V1w(2))*(V3w(1) - V1w(1)) >= 0 && ...
(grid_w - V2w(1))*(V4w(2) - V2w(2)) - ...
(grid_h - V2w(2))*(V4w(1) - V2w(1)) <= 0 &&...
(grid_w - V1w(1))*(V2w(2) - V1w(2)) - ...
(grid_h - V1w(2))*(V2w(1) - V1w(1)) <= 0 && ...
(grid_w - V3w(1))*(V4w(2) - V3w(2)) - ...
(grid_h - V3w(2))*(V4w(1) - V3w(1)) >= 0);

grid_point = [grid_w; grid_h; 1];
grid_backward = H*grid_point;
grid_backward = grid_backward/grid_backward(3);
GridWarped(grid_h, grid_w, 1) = grid_backward(1);
GridWarped(grid_h, grid_w, 2) = grid_backward(2);
end
end
end

end
end

% deal with the out-of-border situations
GridWarped( GridWarped<1) = 1;
GridWarped_w = GridWarped(:,:,1); GridWarped_w(GridWarped_w > w) = w;
GridWarped(:,:,1) = GridWarped_w;
GridWarped_h = GridWarped(:,:,2); GridWarped_h(GridWarped_h > h) = h;
GridWarped(:,:,2) = GridWarped_h;

[X,Y] = meshgrid(1:w,1:h);
im_warped(:,:,1) = interp2(X,Y,double(im(:,:,1)),GridWarped(:,:,1),GridWarped(:,:,2));
im_warped(:,:,2) = interp2(X,Y,double(im(:,:,2)),GridWarped(:,:,1),GridWarped(:,:,2));
im_warped(:,:,3) = interp2(X,Y,double(im(:,:,3)),GridWarped(:,:,1),GridWarped(:,:,2));
im_warped = uint8(im_warped);
time_consumed = toc;
disp(['Warping time : ' num2str(time_consumed) ' s.']);

end

74 changes: 74 additions & 0 deletions MeshGridImgPlot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function [ ] = MeshGridImgPlot(im, Vertex_set, COLOR_V)
% Summary of this function goes here
% Detailed explanation goes here
% The Code (Version 1) is created by ZHANG Yabin,
% Nanyang Technological University, 2015-12-30
% which is based on the method described in the following paper
% [1] Wang, Yu-Shuen, et al. "Optimized scale-and-stretch for image resizing."
% ACM Transactions on Graphics (TOG) 27.5 (2008): 118.
% The binary code is provided on the project page:
% http://graphics.csie.ncku.edu.tw/Image_Resizing/
% The Matlab codes are for non-comercial use only.
% Note that the importance maps are slightly different from the original
% ones, and the retargeted images are influenced.

if(nargin < 3)
COLOR_V = [0.0 0.5 0.0];
% COLOR_V = [0.5 0.0 0.5];
end

tic
figure('units' ,'normalized' ,'outerposition' ,[0.01 0.01 0.95 0.95]);
imshow(im); hold on;

Vertex_set( Vertex_set == 0) = 1;

[ver_h, ver_w, ~] = size(Vertex_set);
quad_num_h = ver_h - 2;
quad_num_w = ver_w - 2;

for vect_i = 1:quad_num_h+1
for vect_j = 1:quad_num_w+1
pos_ini_x = Vertex_set(vect_i, vect_j, 2);
pos_ini_y = Vertex_set(vect_i, vect_j, 1);
pos_end_x = Vertex_set(vect_i, vect_j+1, 2);
pos_end_y = Vertex_set(vect_i, vect_j+1, 1);
line([pos_ini_x pos_end_x],...
[pos_ini_y pos_end_y],...
'Color', COLOR_V, 'LineWidth',2);
pos_end_x = Vertex_set(vect_i+1, vect_j, 2);
pos_end_y = Vertex_set(vect_i+1, vect_j, 1);
line([pos_ini_x pos_end_x],...
[pos_ini_y pos_end_y],...
'Color', COLOR_V, 'LineWidth',2);

end
end
for vect_i = quad_num_h+2
for vect_j = 1:quad_num_w+1
pos_ini_x = Vertex_set(vect_i, vect_j, 2);
pos_ini_y = Vertex_set(vect_i, vect_j, 1);
pos_end_x = Vertex_set(vect_i, vect_j+1, 2);
pos_end_y = Vertex_set(vect_i, vect_j+1, 1);
line([pos_ini_x pos_end_x],...
[pos_ini_y pos_end_y],...
'Color', COLOR_V, 'LineWidth',2);
end
end
for vect_i = 1:quad_num_h+1
for vect_j = quad_num_w+2
pos_ini_x = Vertex_set(vect_i, vect_j, 2);
pos_ini_y = Vertex_set(vect_i, vect_j, 1);
pos_end_x = Vertex_set(vect_i+1, vect_j, 2);
pos_end_y = Vertex_set(vect_i+1, vect_j, 1);
line([pos_ini_x pos_end_x],...
[pos_ini_y pos_end_y],...
'Color', COLOR_V, 'LineWidth',2);
end
end

mesh_grid_time = toc;
disp(['Mesh grid plot time consuming: ' num2str(mesh_grid_time) '.']);

end

18 changes: 18 additions & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

% The Matlab Code (Version 1) is created by ZHANG Yabin,
% Nanyang Technological University, 2015-12-30
% which is based on the method described in the following paper
% [1] Wang, Yu-Shuen, et al. "Optimized scale-and-stretch for image resizing."
% ACM Transactions on Graphics (TOG) 27.5 (2008): 118.
% The binary code is provided on the project page:
% http://graphics.csie.ncku.edu.tw/Image_Resizing/
% The Matlab codes are for non-comercial use only.
% Note that the importance maps are slightly different from the original
% ones, and the retargeted images are influenced.


The code has been tested on the Windows system.
Available at: http://yabinzhangjohn.github.io/
To run the code:
1.Install the gbvs: run the gbvs/gbvs_install.m
2.Directly run the Demo.m
Loading

0 comments on commit c8ff1be

Please sign in to comment.