%
% Author: Raul Correal
% Date: Feb 2012
% (raulcorreal@hotmail.com)
%
% Input:
% - image1: original image1
% - image2: original image2
% - type: (1) Gray matching, (2) RGB matching, (3) HSV matching
%
% Output
% - image2T: image2 transformed
%
function [image2T] = histogramMatching(image1, image2, type)
switch type
case 1
%Gray: convert to Gray and match channels
if (ndims(image1) > 2)
image2T = histeq(rgb2gray(image2), imhist(rgb2gray(image1)));
else
image2T = histeq(image2, imhist(image1));
end
case 2
%RGB: match each channel separatedly
image2T(:,:,1) = histeq(image2(:,:,1), imhist(image1(:,:,1)));
image2T(:,:,2) = histeq(image2(:,:,2), imhist(image1(:,:,2)));
image2T(:,:,3) = histeq(image2(:,:,3), imhist(image1(:,:,3)));
case 3
%HSV: convert to HSV and match V channel
HSV1 = rgb2hsv(image1);
HSV2 = rgb2hsv(image2);
% HSV2(:,:,2) = mat2gray(histeq(uint8(HSV2(:,:,2).*255), imhist(uint8(HSV1(:,:,2).*255))));
HSV2(:,:,3) = mat2gray(histeq(uint8(HSV2(:,:,3).*255), imhist(uint8(HSV1(:,:,3).*255))));
image2T = uint8(hsv2rgb(HSV2).*255);
end
end