convert white color in image to red color

6 次查看(过去 30 天)
Could you please support me by Matlab code to convert white color in any image to red color ?
  1 个评论
DGM
DGM 2022-11-5
编辑:DGM 2022-11-5
This comment demonstrates the challenges of getting decent colorization of white objects.
This answer demonstrates the colorization of black objects.

请先登录,再进行评论。

采纳的回答

Sibi
Sibi 2020-11-22
编辑:Sibi 2020-11-22
k is the image(RGB) array
j=k(:,:,1);
l=k(:,:,2);
m=k(:,:,3);
l((j>=40))=0;
m(j>=40)=0;
k(:,:,1)=j;k(:,:,2)=l;k(:,:,3)=m;
imshow(k)
  1 个评论
Badar Salim
Badar Salim 2020-11-22
Thank you dear, It's works but if possible to convert only white color to red that will be a very good.
Thank you again, and if you have another code to change only white color to red please provide.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2020-11-22
编辑:Image Analyst 2020-11-22
Since you didn't say what "red" means to you, I've done it three different ways:
% Demo to turn white petals of a daisy red. By Image Analyst, Nov. 22, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'white-flower.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(3, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Find a mask of just the white pixels.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display the mask image.
subplot(3, 2, 2);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Display the initial mask image.
subplot(3, 2, 3);
imshow(maskedRGBImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Masked RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%-----------------------------------------------------------------------------------
% Option 1 : make the white pure red or (255, 0, 0)
% Get the individual RGB channels
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Make the image pure red in the mask regions
redChannel(mask) = 255;
greenChannel(mask) = 0;
blueChannel(mask) = 0;
% Recombine separate color channels into a single, true color RGB image.
tintedRgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the initial mask image.
subplot(3, 2, 4);
imshow(tintedRgbImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Tinted RGB Image via Setting to a Constant', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%-----------------------------------------------------------------------------------
% Option 2 : make the white redder than it is by multiplying it by some brightness reduction factor.
% Get the individual RGB channels
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Make the image redder in the mask regions by lowering the green and blue channels.
brightnessReductionFactor = 0.35;
greenChannel(mask) = uint8(brightnessReductionFactor * double(greenChannel(mask)));
blueChannel(mask) = uint8(brightnessReductionFactor * double(blueChannel(mask)));
% Recombine separate color channels into a single, true color RGB image.
tintedRgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the initial mask image.
subplot(3, 2, 5);
imshow(tintedRgbImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Tinted RGB Image via Multiplication', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%-----------------------------------------------------------------------------------
% Option 3 : make the white redder than it is by subtracting some brightness factor.
% Get the individual RGB channels
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Make the image redder in the mask regions by lowering the green and blue channels.
subtractionAmount = uint8(150);
greenChannel(mask) = greenChannel(mask) - subtractionAmount;
blueChannel(mask) = blueChannel(mask) - subtractionAmount;
% Recombine separate color channels into a single, true color RGB image.
tintedRgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the initial mask image.
subplot(3, 2, 6);
imshow(tintedRgbImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Tinted RGB Image via Subtraction', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
fprintf('Done running %s.m ...\n', mfilename);
msgbox('Done!');
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 22-Nov-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.374;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.467;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  1 个评论
Badar Salim
Badar Salim 2020-11-22
Thank you for your support.
Could you please make it more semple ?
I try but is doesn't works with me.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by