Changing hair color in a photo using matlab

5 views (last 30 days)
rgbColorImage = imread('peppers.png');
% Extract the individual red, green, and blue color channels.
redChannel = rgbColorImage{ii}(:, :, 1);
greenChannel = rgbColorImage{ii}(:, :, 2);
blueChannel = rgbColorImage{ii}(:, :, 3);
% Specify the color we want to make this area.
desiredColor = [146, 40, 146]; % Purple
% Make the red channel that color
redChannel(maskedImage) = desiredColor(1);
greenChannel(maskedImage) = desiredColor(2);
blueChannel(maskedImage) = desiredColor(3);
% Recombine separate color channels into a single, true color RGB image.
rgbColorImage{ii} = cat(3, redChannel, greenChannel, blueChannel);
Undefined function or variable 'ii'.
Error in color_change_red_into_blue (line 92)
redChannel = rgbColorImage{ii}(:, :, 1);
I get this error. What do you recommend ?

Answers (1)

Image Analyst
Image Analyst on 11 Sep 2021
rgbImage is not a cell array so you don't use braces. Get rid of the {ii}. It should be done like
% Extract the individual red, green, and blue color channels.
[redChannel , greenChannel, blueChannel] = imsplit(rgbColorImage);
maskedImage has not been defined at all, so that will cause an error when your program gets to that. You need to define maskedImage as a binary image in order to use it as a logical image like you're trying.
  4 Comments
yasar ismet Yilmaz
yasar ismet Yilmaz on 11 Sep 2021
I created the image where I changed the hair color and saved it to Workspace. How do I print my saved photo to the screen? (sorry i asked a lot of questions)
Image Analyst
Image Analyst on 11 Sep 2021
OK, good. Glad you got it working. To display (not print) the image to the screen you can use imshow():
imshow(rgbImage);
assuming the variable you saved to the workspace is called rgbImage.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!