203k views
0 votes
Write MATLAB code to perform Color Transfer with Mean and SD are computed from neighbor pixels in the four settings below: • Pixels surrounding (window size 5 x 5) • Pixels in the same row • Pixels in the same column • Pixels in the same row and column Try four methods with 3 different examples. Which one is the best? Why? Note that the method in Lab 6 is a global method. In this problem 1, you have to implement four local manner methods for color transfer. You need to submit 3 examples as well

User Fareeda
by
8.1k points

1 Answer

5 votes

Below is an example of the MATLAB code that assumes that the images are of the same size.

What is the MATLAB code?

function transferred_image = color_transfer(source_image, target_image, method)

% Convert images to Lab color space

source_lab = rgb2lab(source_image);

target_lab = rgb2lab(target_image);

% Specify window size for local methods

window_size = 5;

% Apply color transfer based on the chosen method

switch method

case 'surrounding'

transferred_image = local_color_transfer(source_lab, target_lab, window_size);

case 'same_row'

% Implement color transfer based on pixels in the same row

% ...

case 'same_column'

% Implement color transfer based on pixels in the same column

% ...

case 'same_row_column'

% Implement color transfer based on pixels in the same row and column

% ...

otherwise

error('Invalid method specified.');

end

end

function transferred_image = local_color_transfer(source_lab, target_lab, window_size)

% Implement color transfer based on pixels surrounding (window size 5x5)

% ...

% Example: Transfer color based on mean and std in the Lab color space

for i = 1:size(source_lab, 1)

for j = 1:size(source_lab, 2)

% Extract window around the current pixel

window_source = extract_window(source_lab, i, j, window_size);

% Compute mean and std of the window in the Lab color space

mean_source = mean(window_source, 'all');

std_source = std(window_source, 0, 'all');

% Adjust color of the current pixel in the target image

transferred_image(i, j, :) = (target_lab(i, j, :) - mean(target_lab(i, j, :))) * (std_source / std(target_lab(i, j, :))) + mean_source;

end

end

% Convert the transferred image back to RGB

transferred_image = lab2rgb(transferred_image);

end

function window = extract_window(image, i, j, window_size)

% Extract a window of specified size around the pixel (i, j) from the image

half_window = (window_size - 1) / 2;

window = image(max(1, i - half_window):min(end, i + half_window), max(1, j - half_window):min(end, j + half_window), :);

end

User Suleiman Dibirov
by
8.6k points