Increase hue of image

18 Ansichten (letzte 30 Tage)
Allen Ray
Allen Ray am 16 Sep. 2016
Bearbeitet: DGM am 30 Jun. 2023
Hi guys, I am new to MATLAB. How to make changes on the hue of image, for example increase the hue of image?

Akzeptierte Antwort

Image Analyst
Image Analyst am 16 Sep. 2016
Take the hue image, and multiply it by something.
hsvImage = rgb2hav(rgbImage);
hsvImage(:,:,1) = hsvImage(:,:,1) * someFactor;
rgbImage = hsv2rgb(hsvImage);
I think you really mean increase the saturation, but whatever... The saturation channel is 2, so switch to that if you really want that.
  2 Kommentare
Allen Ray
Allen Ray am 17 Sep. 2016
Thanks in advance. I pretty sure your answer is correct.
DGM
DGM am 9 Mai 2022
Multiplying hue causes banding discontinuities near red, as your example demonstrates below.
rgbImage = imread('peppers.png');
k = 2.5;
hsvImage = rgb2hsv(rgbImage);
hsvImage(:,:,1) = mod(hsvImage(:,:,1)*k,1);
rgbImage = hsv2rgb(hsvImage);
imshow(rgbImage)
These artifacts occur in smooth image regions containing colors in the vicinity of red when non-integer scaling factors are used.
It also distorts the hue distribution in a way that defies the image structure. Consider doing the exact same hue adjustment to this set of four simple bidirectional linear gradients:
rgbImage = imread('gradset.png');
k = 2.5;
hsvImage = rgb2hsv(rgbImage);
hsvImage(:,:,1) = mod(hsvImage(:,:,1)*k,1);
rgbImage = hsv2rgb(hsvImage);
imshow(rgbImage)
That's almost certainly not the expected result. Again, the discontinuity is noticeable in some of the images.
Simple hue adjustment is done by modular addition.
rgbImage = imread('peppers.png');
k = 0.5;
hsvImage = rgb2hsv(rgbImage);
hsvImage(:,:,1) = mod(hsvImage(:,:,1)+k,1);
rgbImage = hsv2rgb(hsvImage);
imshow(rgbImage)
As to why it's discontinuous:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Image Analyst
Image Analyst am 17 Sep. 2016
Here's the full demo. If it solves your problem, then can you mark the Answer as accepted?
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = fileparts(which('peppers.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the original color image.
subplot(2, 1, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
drawnow;
% Convert to HSV color space
hsvImage = rgb2hsv(rgbImage);
% Extract components:
hueImage = hsvImage(:, :, 1);
saturationImage = hsvImage(:, :, 2);
valueImage = hsvImage(:, :, 3);
% "Increase" hue.
someFactor = 2.5; % Whatever you want.
hueImage = hueImage * someFactor;
% Wrap around if it goes more than 1.
% moreThan1 = hueImage > 1;
% imshow(moreThan1);
hueImage = mod(hueImage, 1);
% Recombine original saturation and value channels
% with new hue channel.
hsvImage = cat(3, hueImage, saturationImage, valueImage);
% Convert back to RGB color space.
rgbImage = hsv2rgb(hsvImage);
% Display the image.
subplot(2, 1, 2);
imshow(rgbImage);
axis on;
title('Hue "Increased" Image', 'FontSize', fontSize, 'Interpreter', 'None');

John D'Errico
John D'Errico am 16 Sep. 2016
Increase the hue? Since hue is generally regarded as an angle in some polar coordinate system, increasing the hue would generally do something silly like move red colors towards orange, oranges towards yellow, yellows towards green, purples towards red, etc. If you increase the overall hue, then you will do all of these things at once.
https://en.wikipedia.org/wiki/Hue
Do you really wish to increase hue? Perhaps you are asking about some other aspect of color, yet you asked about hue?
https://en.wikipedia.org/wiki/HSL_and_HSV
If you truly need to do something like the above, then you would convert your image into some color space like HSV, L*a*b*, etc. Then modify the appropriate color channel, and convert back into an RGB space. Note that the conversion back into RGB may possibly have problems at the gamut peripheries in some color regions, since the gamut of RGB is limited.
  2 Kommentare
Allen Ray
Allen Ray am 16 Sep. 2016
Yes, you are correct, I want to increase the hue of image. It's actually part of basic knowledge questions that lecturer given to us. From your comment, I think the question is asking us to increase the certain colors of the image.
Anyway, thanks a lot for your kindly reply. Much appreciate.
Pengfei Wang
Pengfei Wang am 12 Jul. 2021
Instead of "increase", I think you might meant to "adjust" hue of hte image.
You need to find the hue angle which you want to adjust first, like this:
green_idx = hue(hue>0.2 & hue<0.35)
then,you can adjust hue as you want.

Melden Sie sich an, um zu kommentieren.


DGM
DGM am 28 Okt. 2021
Bearbeitet: DGM am 30 Jun. 2023
You don't increase hue, you rotate hue. You can convert the image to a polar model and then add/subtract and mod() the result to keep it from getting clipped. Multiplying by a factor as in the accepted example is rarely a sensible approach. The goal is typically to shift the hue circularly, not stretch/compress the hue. Stretching the hue of an image often results in banding, as the above demo indicates.
There aren't any convenient tools to do color adjustment in base MATLAB/IPT, but MIMT (on the File Exchange) has imtweak(), which can trivially adjust the components of an image in various color models. Which color model you use depends on what you intend to do. Large hue rotations in the basic cylindrical representations of sRGB (HSV, HSL, HSI) will tend to ruin the brightness information in an image. Using a color model with better separation of brightness and color helps.
A = imread('sources/blacklight2.jpg');
amount = 0.25;
B = imtweak(A,'hsv',[amount 1 1]);
C = imtweak(A,'hsl',[amount 1 1]);
D = imtweak(A,'hsi',[amount 1 1]);
E = imtweak(A,'lchab',[1 1 amount]);
Note the local brightness inversion on the paper when using HSV/HSL, and the loss of contrast on the orange bottle with all HSx methods. Using a polar representation of LAB helps retain brightness information, though other models are available and may work as well.
MIMT also has immodify(), which offers a GUI interface for imtweak() functionality.
As @John D'Errico mentioned, the conversion back to RGB is going to be problematic in all models except for HSV and HSL (and HuSL/HSY). Rotation in LCHab/uv (or even in HSI) will result in color points potentially moving outside the gamut extents. As a convenience tool, imtweak() solves this by truncating the chroma of OOG points prior to converting to RGB. While truncating in the polar model results in less apparent distortion than truncating in RGB would, it can result in chroma compression if an image is repeatedly adjusted. See example 5 here.
It's arguable whether or not any truncation should be performed. I decided to make imtweak() do it this way by default. In the current version of imtweak(), you can change the behavior by specifying the appropriate truncation options.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by