How to compare two colors?
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to tell whethere one color is lighter or darker to another color.
So I would like to compare an image of one color to 4 colors (red,blue, green, and yellow), have it select the color closest to it then anaylyze whether it is darker or lighter than the closest color.
0 Commenti
Risposte (2)
Image Analyst
il 21 Gen 2021
JD, I haven't heard back from you so I assume you're having a lot of trouble. Maybe start with this simple script where I compare the L value (brightness) of a demo image to the L value of the four pure colors you listed. Please report back if you understand this simple program.
clear all;
close all;
clc;
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
rgbImage = imread('peppers.png');
subplot(3, 4, 1);
imshow(rgbImage);
title('RGB image', 'FontSize', fontSize);
% Get the L (brightness) values for this RGB image.
labImage = rgb2lab(rgbImage);
lImage = labImage(:, :, 1);
subplot(3, 4, 2);
imshow(lImage, []);
title('L image', 'FontSize', fontSize);
% Get the L (brightness) values for these four primary colors.
labRed = rgb2lab([255, 0, 0])
labGreen = rgb2lab([0, 255, 0])
labBlue = rgb2lab([0, 0, 255])
labYellow = rgb2lab([255, 255, 0])
% Find where the image is brighter than pure red. You need to look at the L channel.
subplot(3, 4, 5);
imshow(lImage > labRed(1));
caption = sprintf('Pixels where image is\nbrighter than pure red.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure green. You need to look at the L channel.
subplot(3, 4, 6);
imshow(lImage > labGreen(1));
caption = sprintf('Pixels where image is\nbrighter than pure green.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure blue. You need to look at the L channel.
subplot(3, 4, 7);
imshow(lImage > labBlue(1));
caption = sprintf('Pixels where image is\nbrighter than pure blue.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure yellow. You need to look at the L channel.
subplot(3, 4, 8);
imshow(lImage > labYellow(1));
caption = sprintf('Pixels where image is\nbrighter than pure yellow.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure red. You need to look at the L channel.
subplot(3, 4, 9);
imshow(lImage <= labRed(1));
caption = sprintf('Pixels where image is\ndarker than pure red.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure green. You need to look at the L channel.
subplot(3, 4, 10);
imshow(lImage <= labGreen(1));
caption = sprintf('Pixels where image is\ndarker than pure green.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure blue. You need to look at the L channel.
subplot(3, 4, 11);
imshow(lImage <= labBlue(1));
caption = sprintf('Pixels where image is\ndarker than pure blue.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure yellow. You need to look at the L channel.
subplot(3, 4, 12);
imshow(lImage <= labYellow(1));
caption = sprintf('Pixels where image is\ndarker than pure yellow.');
title(caption, 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);
You get:
labRed =
53.2390736041736 80.0977176619316 67.2172100598051
labGreen =
87.7340833084369 -86.1522965123016 83.1853561183118
labBlue =
32.2959590303621 79.1924278969797 -107.862641835025
labYellow =
97.138085602063 -21.5334876785774 94.4869625694341
Where the pixels are pure white is shows those pixels where the condition is true (for example pixels that are brighter than the brightness of pure red color).
0 Commenti
Vedere anche
Categorie
Scopri di più su Image Processing Toolbox in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!