Find and plot the variation of R, G, and B values of the image

4 views (last 30 days)
Looking for help on finding and plotting the values of R, G, and B of the attached image. Also to find pixel locations of the dark lines and the wavelength of the dark lines. I am new to MatLab and could use a little help. Thank you in advance.
  6 Comments
Walter Roberson
Walter Roberson on 2 Oct 2015
Okay, but how would you like to "plot" the values of R, G, and B ?
Garrett Nagy
Garrett Nagy on 4 Oct 2015
Each color on its own graph along the x direction. So the amount of color vs frequency probably

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Sep 2015
RGB = imread('YourImage.tif');
Red = RGB(:,:,1);
histogram(Red(:))
  2 Comments
Garrett Nagy
Garrett Nagy on 4 Oct 2015
i got the red to work but i cannot do the same thing for green or blue, i get an error that says index exceeds matrix dimensions. I tried to do BLUE = RGB(:,:,3) and GREEN = RGB(:,:,2) and came up with that error above.
Walter Roberson
Walter Roberson on 4 Oct 2015
It sounds like your tif might be pseudocolor
[img, map] = imread('YourImage.tif');
and check to see if img comes out 2 dimensional and map comes out non-empty. If so then you can use ind2rgb() to convert it to RGB

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 4 Oct 2015
Like I said before, you can't get wavelength from an RGB color. But if what you really want is a plot of the red signal, green signal, and blue signal as a function of distance along your x axis, then you can do that with this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Open an image.
% Browse for the image file.
[baseFileName, folder] = uigetfile('*.jpg', 'Specify a JPG image file');
fullImageFileName = fullfile(folder, baseFileName);
if folder == 0
return;
end
% Read in image into an array.
[rgbImage, storedColorMap] = imread(fullImageFileName);
[rows, columns, numberOfColorBands] = size(rgbImage)
% If it's monochrome (indexed), convert it to color.
if numberOfColorBands < 3
uiwait(warndlg('It must be a color image'));
end
% Display the original image.
subplot(2, 1, 1);
imshow(rgbImage);
title('RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
subplot(2, 1, 2);
% Plot the red signal
plot(redChannel(1, :), 'r', 'LineWidth', 2);
grid on;
hold on;
% Plot the green signal
plot(greenChannel(1, :), 'g', 'LineWidth', 2);
% Plot the blue signal
plot(blueChannel(1, :), 'b', 'LineWidth', 2);
title('Colors as function of distance along x axis', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Distance in Pixels', 'FontSize', fontSize');
ylabel('Gray Levels', 'FontSize', fontSize');
legend('Red', 'Green', 'Blue');
In addition, see the attached demo below, colormaps_plotted.m, where I do the same thing for any of the standard colormaps that you select.

Tags

Community Treasure Hunt

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

Start Hunting!