Skip to content

Instantly share code, notes, and snippets.

@seyyah
Last active November 22, 2019 22:43
Show Gist options
  • Save seyyah/ca31710b8541a0599036 to your computer and use it in GitHub Desktop.
Save seyyah/ca31710b8541a0599036 to your computer and use it in GitHub Desktop.
% http://stackoverflow.com/questions/27693593/color-correcting-images-in-matlab
% https://ia802707.us.archive.org/23/items/Lectures_on_Image_Processing/EECE253_05_ColorCorrection.pdf
close all;
clear all;
im1 = imread('http://i.stack.imgur.com/GtgHU.jpg');
im2 = imread('http://i.stack.imgur.com/wHW50.jpg');
rng(123); %// Set seed for reproducibility
num_colours = 2000;
ind = randperm(numel(im1) / size(im1,3), num_colours);
%// Grab colours from original image
red_out = im1(:,:,1);
green_out = im1(:,:,2);
blue_out = im1(:,:,3);
%// Grab colours from corrupted image
red_in = im2(:,:,1);
green_in = im2(:,:,2);
blue_in = im2(:,:,3);
%// Create 3 x N matrices
X = double([red_in(ind); green_in(ind); blue_in(ind)]);
Y = double([red_out(ind); green_out(ind); blue_out(ind)]);
%// Find A
A = Y*(X.')/(X*X.');
%// Cast im2 to double for precision
im2_double = double(im2);
%// Apply matrix multiplication
out = cast(reshape((A*reshape(permute(im2_double, [3 1 2]), 3, [])).', ...
[size(im2_double,1) size(im2_double,2), 3]), class(im2));
figure(1);
subplot(221); imshow(im1)
subplot(222); imshow(im2)
subplot(223); imshow(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment