Skip to content

Commit

Permalink
Code uploaded
Browse files Browse the repository at this point in the history
Version 1.0
  • Loading branch information
mahmoudnafifi committed May 31, 2019
1 parent 41cef69 commit aa1a7e4
Show file tree
Hide file tree
Showing 224 changed files with 4,942 additions and 0 deletions.
Binary file added color150/airplane.jpg
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 color150/animal.jpg
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 color150/apparel.jpg
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 color150/arcade machine.jpg
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 color150/armchair.jpg
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 color150/ashcan.jpg
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 color150/awning.jpg
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 color150/bag.jpg
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 color150/ball.jpg
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 color150/bannister.jpg
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 color150/bar.jpg
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 color150/barrel.jpg
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 color150/base.jpg
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 color150/basket.jpg
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 color150/bathtub.jpg
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 color150/bed.jpg
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 color150/bench.jpg
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 color150/bicycle.jpg
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 color150/blanket.jpg
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 color150/blind.jpg
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 color150/boat.jpg
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 color150/book.jpg
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 color150/bookcase.jpg
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 color150/booth.jpg
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 color150/bottle.jpg
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 color150/box.jpg
Binary file added color150/bridge.jpg
Binary file added color150/buffet.jpg
Binary file added color150/building.jpg
Binary file added color150/bulletin board.jpg
Binary file added color150/bus.jpg
Binary file added color150/cabinet.jpg
Binary file added color150/canopy.jpg
Binary file added color150/car.jpg
Binary file added color150/case.jpg
Binary file added color150/ceiling.jpg
Binary file added color150/chair.jpg
Binary file added color150/chandelier.jpg
Binary file added color150/chest of drawers.jpg
Binary file added color150/clock.jpg
Binary file added color150/coffee table.jpg
Binary file added color150/column.jpg
Binary file added color150/computer.jpg
Binary file added color150/conveyer belt.jpg
Binary file added color150/counter.jpg
Binary file added color150/countertop.jpg
Binary file added color150/cradle.jpg
Binary file added color150/crt screen.jpg
Binary file added color150/curtain.jpg
Binary file added color150/cushion.jpg
Binary file added color150/desk.jpg
Binary file added color150/dirt track.jpg
Binary file added color150/dishwasher.jpg
Binary file added color150/door.jpg
Binary file added color150/earth.jpg
Binary file added color150/escalator.jpg
Binary file added color150/fan.jpg
Binary file added color150/fence.jpg
Binary file added color150/field.jpg
Binary file added color150/fireplace.jpg
Binary file added color150/flag.jpg
Binary file added color150/floor.jpg
Binary file added color150/flower.jpg
Binary file added color150/food.jpg
Binary file added color150/fountain.jpg
Binary file added color150/glass.jpg
Binary file added color150/grandstand.jpg
Binary file added color150/grass.jpg
Binary file added color150/hill.jpg
Binary file added color150/hood.jpg
Binary file added color150/house.jpg
Binary file added color150/hovel.jpg
Binary file added color150/kitchen island.jpg
Binary file added color150/lake.jpg
Binary file added color150/lamp.jpg
Binary file added color150/land.jpg
Binary file added color150/light.jpg
Binary file added color150/microwave.jpg
Binary file added color150/minibike.jpg
Binary file added color150/mirror.jpg
Binary file added color150/monitor.jpg
Binary file added color150/mountain.jpg
Binary file added color150/others.jpg
Binary file added color150/ottoman.jpg
Binary file added color150/oven.jpg
Binary file added color150/painting.jpg
Binary file added color150/palm.jpg
Binary file added color150/path.jpg
Binary file added color150/person.jpg
Binary file added color150/pier.jpg
Binary file added color150/pillow.jpg
Binary file added color150/plant.jpg
Binary file added color150/plate.jpg
Binary file added color150/plaything.jpg
Binary file added color150/pole.jpg
Binary file added color150/pool table.jpg
Binary file added color150/poster.jpg
Binary file added color150/pot.jpg
Binary file added color150/radiator.jpg
Binary file added color150/railing.jpg
Binary file added color150/refrigerator.jpg
Binary file added color150/river.jpg
Binary file added color150/road.jpg
Binary file added color150/rock.jpg
Binary file added color150/rug.jpg
Binary file added color150/runway.jpg
Binary file added color150/sand.jpg
Binary file added color150/sconce.jpg
Binary file added color150/screen door.jpg
Binary file added color150/screen.jpg
Binary file added color150/sculpture.jpg
Binary file added color150/sea.jpg
Binary file added color150/seat.jpg
Binary file added color150/shelf.jpg
Binary file added color150/ship.jpg
Binary file added color150/shower.jpg
Binary file added color150/sidewalk.jpg
Binary file added color150/signboard.jpg
Binary file added color150/sink.jpg
Binary file added color150/sky.jpg
Binary file added color150/skyscraper.jpg
Binary file added color150/sofa.jpg
Binary file added color150/stage.jpg
Binary file added color150/stairs.jpg
Binary file added color150/stairway.jpg
Binary file added color150/step.jpg
Binary file added color150/stool.jpg
Binary file added color150/stove.jpg
Binary file added color150/streetlight.jpg
Binary file added color150/swimming pool.jpg
Binary file added color150/swivel chair.jpg
Binary file added color150/table.jpg
Binary file added color150/tank.jpg
Binary file added color150/television receiver.jpg
Binary file added color150/television.jpg
Binary file added color150/tent.jpg
Binary file added color150/toilet.jpg
Binary file added color150/towel.jpg
Binary file added color150/tower.jpg
Binary file added color150/trade name.jpg
Binary file added color150/traffic light.jpg
Binary file added color150/tray.jpg
Binary file added color150/tree.jpg
Binary file added color150/truck.jpg
Binary file added color150/van.jpg
Binary file added color150/vase.jpg
Binary file added color150/wall.jpg
Binary file added color150/wardrobe.jpg
Binary file added color150/washer.jpg
Binary file added color150/water.jpg
Binary file added color150/waterfall.jpg
Binary file added color150/windowpane.jpg
340 changes: 340 additions & 0 deletions colour-transfer-master/LICENSE

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions colour-transfer-master/colour_transfer_MKL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%
% colour transfer algorithm based on linear Monge-Kantorovitch solution
%
% IR = colour_transfer_MKL(I_original, I_target, nbiterations);
%
% (c) F. Pitie 2007
%
% see reference:
%
%
function IR = colour_transfer_MKL(I0, I1)

if (ndims(I0)~=3)
error('pictures must have 3 dimensions');
end

X0 = reshape(I0, [], size(I0,3));
X1 = reshape(I1, [], size(I1,3));

A = cov(X0);
B = cov(X1);

T = MKL(A, B);

mX0 = repmat(mean(X0), [size(X0,1) 1]);
mX1 = repmat(mean(X1), [size(X0,1) 1]);

XR = (X0-mX0)*T + mX1;

IR = reshape(XR, size(I0));

function [T] = MKL(A, B)
N = size(A,1);
[Ua,Da2] = eig(A);
Da2 = diag(Da2);
Da2(Da2<0) = 0;
Da = diag(sqrt(Da2 + eps));
C = Da*Ua'*B*Ua*Da;
[Uc,Dc2] = eig(C);
Dc2 = diag(Dc2);
Dc2(Dc2<0) = 0;
Dc = diag(sqrt(Dc2 + eps));
Da_inv = diag(1./(diag(Da)));
T = Ua*Da_inv*Uc*Dc*Uc'*Da_inv*Ua';




82 changes: 82 additions & 0 deletions cp/extract_theme.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function [C,labels] = extract_theme(I, k, sigma)
%I: Image
%K: number of palettes
%Sigma: is sigma!
% increase one more to remove dark palette color
k = k + 1;
bin = 16;
Ibin = floor(255*I/bin);

[weights, X,l1] = im3Dhist(I,Ibin, bin);
% Initialize the cluster centers
cinits = zeros(k, 3);
cw = weights;
N = size(X, 1);
sigma2 = sigma^2;
for i = 1:k
[~,id] = max(cw);
cinits(i,:) = X(id,:);
d2 = repmat(cinits(i,:), N, 1) - X;
d2 = sum(d2 .* d2,2);
cw = cw .* (1 - exp(-d2/sigma2));
end


opt.weight = weights;
[l2, C, ~] = fkmeans(X, cinits, opt);

% C = cinits;
% sort by brightness
[~,id] = sort(C(:,1), 'descend');
C = C(id,:);
C = C(1:k-1,:); %ignore black or darkest color

labels=l1;
for kk=1:length(l2)
if l2(kk)~=id(end)
v=find(id==l2(kk));
labels(l1==kk)=v;
else
labels(l1==kk)=0;
end
end

% covert to rgb
colorTransform = makecform('lab2srgb');
C = applycform(C, colorTransform);

function [W, C,labels] = im3Dhist(I,Ibin, bin)
[h, w, ~] = size(I);

% Compute 3D histogram
Ibin = reshape(Ibin, [], 3);
n = bin;
J = Ibin(:,1)*n*n + Ibin(:,2)*n + Ibin(:,3) + 1;
N = h*w;
M = n * n * n;


% Compute the mean of each bin in Lab
colorTransform = makecform('srgb2lab');
lab = applycform(I, colorTransform);
lab = reshape(lab, [], 3);
weights = zeros(M, 2);
csums = zeros(M, 3);
for i = 1:N
weights(J(i),1) = weights(J(i),1) + 1;
weights(J(i),2)=J(i); %index of J
csums(J(i),:) = csums(J(i),:) + lab(i,:);
end

% Remove all empty bins
ids = find(weights(:,1) ~= 0);
W = weights(ids,:);
C = csums(ids,:);
C = C ./ repmat(W(:,1),1,3);

labels=zeros(size(J));
for i=1:size(W,1)
labels(J==W(i,2))=i;
end
labels=reshape(labels,[h, w]);
W=W(:,1);
77 changes: 77 additions & 0 deletions demo/demo_recoloring.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2019 Mahmoud Afifi
% York University, Canada
% Email: [email protected] - [email protected]
% Permission is hereby granted, free of charge, to any person obtaining
% a copy of this software and associated documentation files (the
% "Software"), to deal in the Software with restriction for its use for
% research purpose only, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.
%
% Please cite the following work if this program is used:
% Mahmoud Afifi, Brian Price, Scott Cohen, and Michael S. Brown, Image Recoloring Based on Object Color Distributions, Eurographics 2019 - Short Papers, 2019
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%


%% Recoloring demo
close all
clear;


resizing = 1; %to resize any large image to have maximum dimension = 800

input_images_dir = 'input_images'; %input images directory
output_images_dir = 'recolored_images'; %output images directory
output_masks_dir = 'output_masks'; %output mask directory

if exist(output_images_dir,'dir') == 0
mkdir(output_images_dir);
end

if exist(output_masks_dir,'dir') == 0
mkdir(output_masks_dir);
end

load('objectName150.mat');
load('color150.mat');

bestCandidates = 15; %should be <20

%Recoloring demo
files = dir(fullfile(input_images_dir,'*.jpg'));
for f = 1 : length(files)
imageName = files(f).name;
disp('Loading the image...');
I = imread(fullfile(input_images_dir,imageName));
%resizing if required
if resizing == 1
if size(I,1)*size(I,2) > 800 * 800
I=imresize(I,800/max(size(I,1),size(I,2)));
fprintf('Warning: the image size is too large, so we resized it to %d x %d... \n', size(I,1),size(I,2));
end
end

disp('Getting the semantic mask...');
[M, ~, ~]=segm(I);

disp('Recoloring image...');
out_images = image_recoloring( I, M, bestCandidates);
disp('Done!');

N = size(out_images,4);
for i=1:N
imwrite(out_images(:,:,:,i),fullfile(output_images_dir,strrep(imageName,'.jpg',sprintf('_%02d.jpg',i))));
end
imwrite(M,fullfile(output_masks_dir,strrep(imageName,'.jpg','.png')));
rgbMask = colorEncode_(M, colors);
imwrite(rgbMask,fullfile(output_masks_dir,strrep(imageName,'.jpg','_rgb.png')));
colormap = colorMap_(M, objectNames);
imwrite(colormap,fullfile(output_masks_dir,strrep(imageName,'.jpg','_colorMap.png')));
end

Binary file added demo/input_images/image_00001.jpg
Binary file added demo/input_images/image_00002.jpg
Binary file added demo/input_images/image_00003.jpg
Binary file added demo/input_images/image_00004.jpg
Binary file added demo/input_images/image_00005.jpg
Binary file added demo/input_images/image_00006.jpg
Binary file added demo/input_images/image_00007.jpg
Binary file added demo/input_images/image_00008.jpg
Binary file added demo/input_images/image_00009.jpg
Binary file added demo/input_images/image_00010.jpg
Binary file added demo/input_images/image_00011.jpg
Binary file added demo/input_images/image_00012.jpg
Binary file added demo/input_images/image_00013.jpg
Binary file added demo/input_images/image_00014.jpg
Binary file added demo/input_images/image_00015.jpg
Binary file added demo/input_images/image_00016.jpg
Binary file added demo/input_images/image_00017.jpg
Binary file added demo/input_images/image_00018.jpg
Binary file added demo/input_images/image_00019.jpg
Binary file added demo/input_images/image_00020.jpg
Binary file added demo/input_images/image_00021.jpg
Binary file added demo/input_images/image_00022.jpg
Binary file added demo/input_images/image_00023.jpg
Binary file added demo/input_images/image_00024.jpg
Binary file added demo/input_images/image_00025.jpg
Binary file added demo/input_images/image_00026.jpg
Binary file added demo/input_images/image_00027.jpg
79 changes: 79 additions & 0 deletions emd/EMD1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdlib.h>
#include "mex.h"
#include "emd__.cpp"


//in Matlab:
//call [distance,flow]=EMD_(features1,feature2,weights1,weights2);
// distance(1) = EMD, distance(2) = flowSize
// flow (flowSize*3) matrix. Each row contains [from, to, amount of work]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

feature_t *f1, *f2;
float *w1, *w2;
signature_t s1, s2;
float e;
flow_t *flow;
int flowSize;

double *f1_mat, *f2_mat;
double *w1_mat, *w2_mat;
mwSize palette1_size, palette2_size;
double *out_matrix1, *out_matrix2;

int i, j;

palette1_size = mxGetM(prhs[0]);
palette2_size = mxGetM(prhs[1]);
f1_mat = mxGetPr(prhs[0]); // each row is a Lab color
f2_mat = mxGetPr(prhs[1]); // each row is a Lab color
w1_mat = mxGetPr(prhs[2]);
w2_mat = mxGetPr(prhs[3]);
flow = new flow_t[palette1_size + palette2_size];

f1 = new feature_t[palette1_size];
w1 = new float[palette1_size];
for (i=0; i<palette1_size; i++)
{
f1[i].X = f1_mat[ i];
f1[i].Y = f1_mat[palette1_size + i];
f1[i].Z = f1_mat[palette1_size * 2 + i];
w1[i] = (float)w1_mat[i];

}

f2 = new feature_t[palette2_size];
w2 = new float[palette2_size];
for (i=0; i<palette2_size; i++)
{
f2[i].X = f2_mat[ i];
f2[i].Y = f2_mat[palette2_size + i];
f2[i].Z = f2_mat[palette2_size * 2 + i];
w2[i] = (float)w2_mat[i];
}

s1 = { palette1_size, f1, w1 };
s2 = { palette2_size, f2, w2 };

e = emd1(&s1, &s2, flow, &flowSize);

plhs[0] = mxCreateDoubleMatrix(2, 1, mxREAL);
out_matrix1 = mxGetPr(plhs[0]);
out_matrix1[0] = (double)e;
out_matrix1[1] = (double)flowSize;

plhs[1] = mxCreateDoubleMatrix(flowSize, 3, mxREAL);
out_matrix2 = mxGetPr(plhs[1]);
for (i = 0; i < flowSize; i++)
//if (flow[i].amount > 0)
{
out_matrix2[ i] = flow[i].from;
out_matrix2[flowSize + i] = flow[i].to;
out_matrix2[flowSize * 2 + i] = flow[i].amount;
}
delete(f1); delete(f2);
delete(w1); delete(w2);

} // end (void) mexFunction

Binary file added emd/EMD1.mexw64
Binary file not shown.
95 changes: 95 additions & 0 deletions emd/EMD2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <stdlib.h>
#include "mex.h"
#include "emd__.cpp"


//in Matlab:
//call [distances]=EMD2(features1,feature2,weights1,weights2,size);
// feature2 and weights2 are array of features and weights
// size is the size of the array
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

feature_t *f1, **f2;
float *w1, **w2;
signature_t s1;
float* e;
flow_t* flow;
int flowSize;
signature_t** s2;
double *f1_mat, *f2_mat;
double *w1_mat, *w2_mat;
mwSize palette1_size, palette2_size;
double *out_matrix1, *out_matrix2;
double* size;
int i, j;

palette1_size = mxGetM(prhs[0]);
palette2_size = mxGetM(prhs[1]); //we assume the second palette has the same size of the first one
f1_mat = mxGetPr(prhs[0]); // each row is a color
f2_mat = mxGetPr(prhs[1]); // each row is a color
w1_mat = mxGetPr(prhs[2]);
w2_mat = mxGetPr(prhs[3]);
size = mxGetPr(prhs[4]); //size of the array of data points

s2=new signature_t*[(int)(*size)];
f2=new feature_t*[(int)(*size)];
w2=new float*[(int)(*size)];
/*allocate features and weights to S*/

flow = new flow_t[palette1_size + palette1_size];
e=new float[(int)(*size)];

f1 = new feature_t[palette1_size];
//initialize the first signature
w1 = new float[palette1_size];
for (i=0; i<palette1_size; i++)
{
f1[i].X = f1_mat[ i];
f1[i].Y = f1_mat[palette1_size + i];
f1[i].Z = f1_mat[palette1_size * 2 + i];
w1[i] = (float)w1_mat[i];

}
s1 = { palette1_size, f1, w1 }; //first signature
int count=0;
for (int s_=0;s_<int(*size); s_++)
{
s2[s_]=new signature_t;
f2[s_]=new feature_t[palette1_size];
w2[s_]=new float[palette1_size];
for (i=0; i<palette1_size; i++)
{
f2[s_][i].X = f2_mat[s_*palette1_size+ i];
f2[s_][i].Y = f2_mat[s_*palette1_size+ palette2_size + i];
f2[s_][i].Z = f2_mat[s_*palette1_size+ 2*palette2_size+ i];
w2[s_][i] = (float)w2_mat[count++];
}
s2[s_]->n=palette1_size;
s2[s_]->Features=f2[s_];
s2[s_]->Weights=w2[s_];

}



e = emd2(&s1, s2, int(*size), flow, &flowSize);
/*float* emd2(signature_t *Signature1, signature_t *Signature2[], int size,
flow_t *Flow, int *FlowSize)*/
/*float* emd3(signature_t *Signature[], int size,
flow_t *Flow, int *FlowSize)*/

plhs[0] = mxCreateDoubleMatrix(int(*size), 1, mxREAL);

out_matrix1 = mxGetPr(plhs[0]);
for (int out=0;out<=int(*size);out++)
out_matrix1[out] = (double)e[out];
free(f1); free(w1);
for (int s_=0;s_<int(*size); s_++)
{
delete(f2[s_]);
delete(w2[s_]);
}

} // end (void) mexFunction

Binary file added emd/EMD2.mexw64
Binary file not shown.
Loading

0 comments on commit aa1a7e4

Please sign in to comment.