Last active
January 1, 2019 14:44
-
-
Save fireattack/5c8c8016d2900a03e81547d31c698bf3 to your computer and use it in GitHub Desktop.
Match image color based on a reference, and save the transformation for more images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
color_mode = 'ycbcr'; %rgb, ycbcr, hsv | |
work_patch = 'E:\!Scan\image match\'; | |
ref = imread([work_patch,'ref.bmp']); % Your reference image | |
A = imread([work_patch,'img.bmp']); % Your scan that needs to be calibrated | |
numColorChan = size(ref,3); | |
isColor = numColorChan > 1; | |
if isColor | |
switch color_mode | |
case 'ycbcr' | |
ref=rgb2ycbcr(ref); | |
A=rgb2ycbcr(A); | |
case 'hsv' | |
ref=im2double(ref); | |
ref=rgb2hsv(ref); | |
A=im2double(A); | |
A=rgb2hsv(A); | |
end | |
end | |
% Anything beyond 256 will essentially give the same result. If you want | |
% the color to be closer to your ref, you can choose lower value but | |
% obviously that will result in banding (because it also decides how many | |
% discrete colors your result will have. | |
N = 1000; | |
% Compute histogram of the reference image | |
hgram = zeros(numColorChan,N); | |
for i = 1:numColorChan | |
hgram(i,:) = imhist(ref(:,:,i),N); | |
end | |
% Adjust A using reference histogram | |
hgramToUse = 1; | |
for k = 1:size(A,3) % Process one color channel at a time | |
if isColor | |
hgramToUse = k; % Use the k-th color channel's histogram | |
end | |
for p = 1:size(A,4) | |
% Use A to store output, to save memory; T is the transformation | |
% matrix | |
[A(:,:,k,p), T(k,:)] = histeq(A(:,:,k,p), hgram(hgramToUse,:)); | |
end | |
end | |
%% | |
% This is your image after matching. | |
if isColor | |
switch color_mode | |
case 'ycbcr' | |
A=ycbcr2rgb(A); | |
imwrite(A,'match_ycbcr.bmp'); | |
case 'hsv' | |
A=hsv2rgb(A); | |
imwrite(A,'match_hsv.bmp'); | |
otherwise | |
imwrite(A,'match_rgb.bmp') | |
end | |
else | |
imwrite(A,'match_grey.bmp'); | |
end | |
%% Batch process other images | |
%myfolder='E:\!Scan\_temp\backup'; % load other images E:\!Scan\_temp\done | |
myfolder=[work_patch,'todo']; | |
newfolder =[myfolder,'_changedcolor']; % result folder | |
mkdir(newfolder); | |
files = [dir(fullfile(myfolder,'*.jpg')); dir(fullfile(myfolder,'*.bmp')); dir(fullfile(myfolder,'*.png'))]; | |
for file = files' | |
fullfilename = fullfile(file.folder, file.name); | |
new=imread(fullfilename); % Now load another image. | |
switch color_mode | |
case 'ycbcr' | |
new=rgb2ycbcr(new); | |
case 'hsv' | |
new=im2double(new); | |
new=rgb2hsv(new); | |
end | |
for k = 1:size(A,3) | |
% Use the same transformation. make sure you have | |
% grayxformmex.mexw64 in your working path. | |
new(:,:,k)=grayxformmex(new(:,:,k),T(k,:)); | |
end | |
switch color_mode | |
case 'ycbcr' | |
new=ycbcr2rgb(new); | |
case 'hsv' | |
new=hsv2rgb(new); | |
end | |
imwrite(new,fullfile(newfolder,[file.name,'.bmp'])); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment