Change Hair Color and apply the one picked by user

조회 수: 1 (최근 30일)
Tehreem Fatima
Tehreem Fatima 2013년 5월 10일
편집: DGM 2023년 3월 12일
I have this image:
and I want to change the hair color to match the hair color in this picture:
How can I do that? I searched for it and found histogram matching, which I don't quite understand. Is this the thing I need and should dig deeper? Or should this be done in any other way? Can anyone help me?

답변 (2개)

Image Analyst
Image Analyst 2013년 5월 10일
Yes, you need histogram matching. The best method I've see is here: http://www.eyemaginary.com/Portfolio/ColorHistogramWarp.html

DGM
DGM 2023년 3월 12일
편집: DGM 2023년 3월 12일
I didn't think about using histogram matching for that. MIMT imrecolor() was never made for this sort of thing, but I've been discovering that it does a fair job in a lot of simple cases.
It's worth noting that the working image has alpha content. The color content in the transparent regions will affect the result, and you'll also need to understand what you're looking at and how to save it. There may be some things that can be done to exclude that non-visible color content from the adjustment, but I'm going to ignore that for now.
% the reference image
brownhair = imread('8725759829_1e0f2638c4_o.jpg');
% the working image has alpha, so read it
[blackhair,~,alpha] = imread('8726880530_477084a8f2_o.png');
% maybe adjust the brightness/gamma a bit
blackhair = imadjust(blackhair,[0 1],[0.05 1],0.85);
% use MIMT imrecolor() with default settings
newhair = imrecolor(brownhair,blackhair);
% the working image has a bunch of junk in transparent regions
% since imshow() can't handle RGBA inputs, just show it as-is
imshow(newhair)
% alternatively, if you're using MIMT, imshow2() does display RGBA
newhair = joinalpha(newhair,alpha); % RGB+A to RGBA
imshow2(newhair)
% either way, saving the image requires the color and alpha to be separate
[newhair alpha] = splitalpha(newhair); % RGBA to RGB+A
imwrite(newhair,'newhair.png','alpha',alpha)
If the instructor suggests histogram matching, perhaps the expected tools would be imhistmatch(), but that probably isn't going to do a great job.
% the reference image
brownhair = imread('8725759829_1e0f2638c4_o.jpg');
% the working image has alpha, so read it
[blackhair,~,alpha] = imread('8726880530_477084a8f2_o.png');
% use imhistmatch() like a maniac
newhair = imhistmatch(blackhair,brownhair);
I mentioned the possibility of excluding non-visible content. That would help significantly if using imhistmatch().
% the reference image
brownhair = imread('8725759829_1e0f2638c4_o.jpg');
% the working image has alpha, so read it
[blackhair,~,alpha] = imread('8726880530_477084a8f2_o.png');
% create a logical mask to select the bulk of the opaque region
mask = repmat(alpha>8,[1 1 3]);
% select only the visible pixels
vispixels = reshape(blackhair(mask),[],1,3); % a stripe
% use imhistmatch() on those pixels
vispixels = imhistmatch(vispixels,brownhair);
% insert modified content back into source image
newhair = blackhair;
newhair(mask) = vispixels;
Note that imhistmatch() simply does a histogram matching of each RGB channel independently. In some cases, this works well, especially if the brightness/contrast details of the working image are less important than those of the reference.
On the other hand, imrecolor() bins both images with respect to their luma, and then does histogram matching of color (hue and saturation/chroma) within those bins in some selected color model. The effect is that imrecolor() strongly attempts to preserve the brightness/contrast of the working image, while ignoring that of the reference image.
These are two different goals. Given two images with different lighting and shadow depths, it's up to you to decide whether it's appropriate to alter the contrast that much. There are plenty of ways to adjust the results or do something in-between.
Otherwise, see this post on colorizing black objects without histogram matching.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by