Prev: Dividing image to sub-blocks by dct2 and for each keeping only fixed set of coefficients.
Next: Need helps for some work in matrix
From: Daphne on 4 Jun 2010 06:43 I would like to generate a histogram with the colors present in an RGB image. Specifically, that image is and addition of two other images: one that I define as the red path and the other is the green. So I end up with, red, green, and shades of yellow from overlaps in the images. What I would like to eventually know is how much red, how much green, and mostly how much yellow I have, and perhaps also be able to filter the merged image by color. Does any one have an idea how to do this? I've tried imhist, but couldn't quite figure out what to do with it. Thanks in advance, Daphne
From: ImageAnalyst on 4 Jun 2010 07:07 Daphne You have to extract each color band individually and then histogram them, like in this demo: 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; % Read in standard MATLAB color demo image. rgbImage = imread('peppers.png'); [rows columns numberOfColorBands] = size(rgbImage); subplot(2, 2, 1); imshow(rgbImage, []); title('Original Color Image', 'Fontsize', fontSize); set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. % Extract the individual color planes. redPlane = rgbImage(:, :, 1); greenPlane = rgbImage(:, :, 2); bluePlane = rgbImage(:, :, 3); % Let's get its histograms. [pixelCountR grayLevelsR] = imhist(redPlane); subplot(2, 2, 2); bar(pixelCountR, 'r'); title('Histogram of red plane', 'Fontsize', fontSize); xlim([0 grayLevelsR(end)]); % Scale x axis manually. [pixelCountG grayLevelsG] = imhist(greenPlane); subplot(2, 2, 3); bar(pixelCountG, 'g'); title('Histogram of green plane', 'Fontsize', fontSize); xlim([0 grayLevelsG(end)]); % Scale x axis manually. [pixelCountB grayLevelsB] = imhist(bluePlane); subplot(2, 2, 4); bar(pixelCountB, 'b'); title('Histogram of blue plane', 'Fontsize', fontSize); xlim([0 grayLevelsB(end)]); % Scale x axis manually.
From: Daphne on 4 Jun 2010 10:07 Thanks so much IA! That works great for the three colors RGB. What do I do if I need the yellow? the overlap regions of the red and the green... For me the blue is empty, so perhpas there's a way to use that channel for the overlap or for detecting yellow directly? Daphne ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <1dd992dd-9e52-4473-8a2d-df612a9c31c6(a)r5g2000yqr.googlegroups.com>... > Daphne > You have to extract each color band individually and then histogram > them, like in this demo: > > 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; > > % Read in standard MATLAB color demo image. > rgbImage = imread('peppers.png'); > [rows columns numberOfColorBands] = size(rgbImage); > subplot(2, 2, 1); > imshow(rgbImage, []); > title('Original Color Image', 'Fontsize', fontSize); > set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. > > % Extract the individual color planes. > redPlane = rgbImage(:, :, 1); > greenPlane = rgbImage(:, :, 2); > bluePlane = rgbImage(:, :, 3); > > % Let's get its histograms. > [pixelCountR grayLevelsR] = imhist(redPlane); > subplot(2, 2, 2); > bar(pixelCountR, 'r'); > title('Histogram of red plane', 'Fontsize', fontSize); > xlim([0 grayLevelsR(end)]); % Scale x axis manually. > > [pixelCountG grayLevelsG] = imhist(greenPlane); > subplot(2, 2, 3); > bar(pixelCountG, 'g'); > title('Histogram of green plane', 'Fontsize', fontSize); > xlim([0 grayLevelsG(end)]); % Scale x axis manually. > > [pixelCountB grayLevelsB] = imhist(bluePlane); > subplot(2, 2, 4); > bar(pixelCountB, 'b'); > title('Histogram of blue plane', 'Fontsize', fontSize); > xlim([0 grayLevelsB(end)]); % Scale x axis manually.
From: Daphne on 4 Jun 2010 10:23 Actually, maybe I'm adding the images wrong? If I see yellow, there should be a blue component... I upload an RGB image and define its (:,:,2:3) = 0 (that's my red channel) then upload another RGB image and define (:,:,[1,3]) = 0 (green channel) then I use imadd to combine them (just a vectorial addition, I know), and so I don't get anything in the blue channel, although when I imshow the merge there is yellow. Ideas? Daphne
From: ImageAnalyst on 4 Jun 2010 10:47
Daphne: See my color detection demo: http://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection You can adapt it to find yellow instead of red. Now that I'm on my other computer, I found my better demo of color histogramming: %------------------------------------------------------------------------------------------------------------- % Calculates and displays histograms of a color image. % Each color channel (red, green, blue) is extracted and its histogram calculated and displayed. % Written by Image Analyst, Jan. 2010. clc; clear; close all; workspace; % Change the current folder to the folder of this m-file. % (The line of code below is from Brett Shoelson of The Mathworks.) if(~isdeployed) cd(fileparts(which(mfilename))); end % Set up sizes for the captions and text we will show on the figure. captionFontSize = 20; % Font size for headers/captions of images. axisFontSize = 14; % Font size for axes of the histogram plots. % Set the initial default directory to the one containing the % standard demo images for the MATLAB Image Processing Toolbox. folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos'; if ~exist(folder, 'dir') folder = pwd; end filePattern = fullfile(folder, '*.*'); % Ask user to browse for the image file. [baseFileName, folder] = uigetfile(filePattern, 'Specify an image file'); % Construct the full file name. fullImageFileName = fullfile(folder, baseFileName); %------------------------------------ % Read the image into an array. rgbImage = imread(fullImageFileName); %------------------------------------ % Check to see if it is color or monochrome. [rows columns numberOfColorChannels] = size(rgbImage); if strcmpi(class(rgbImage), 'uint8') % Flag for 256 gray levels. eightBit = true; else eightBit = false; end % If it's monochrome, convert it to color. if numberOfColorChannels == 1 rgbImage = cat(3, rgbImage, rgbImage, rgbImage); end % Display the original RGB image. subplot(3,4,1); imshow(rgbImage); % Maximize figure. set(gcf, 'Position', get(0, 'ScreenSize')); % drawnow; % Make it display immediately. if numberOfColorChannels > 1 title('Original Color Image', 'FontSize', captionFontSize); else title('Original Monochrome Image (converted to color)', 'FontSize', captionFontSize); end % Extract out the individual color channels redChannel = rgbImage(:, :, 1); greenChannel = rgbImage(:, :, 2); blueChannel = rgbImage(:, :, 3); % Display the red channel image. subplot(3,4,2); imshow(redChannel); title('Red Channel Image', 'FontSize', captionFontSize); % Display the green channel image. subplot(3,4,3); imshow(greenChannel); title('Green Channel Image', 'FontSize', captionFontSize); % Display the blue channel image. subplot(3,4,4); imshow(blueChannel); title('Blue Channel Image', 'FontSize', captionFontSize); % Calculate the histogram of the red channel. hR = subplot(3, 4, 6); [countsR, grayLevelsR] = imhist(redChannel); maxGLValueR = find(countsR > 0, 1, 'last'); maxCountR = max(countsR); % Plot the histogram of the red channel. bar(countsR, 'r'); grid on; xlabel('Red Gray Levels', 'FontSize', axisFontSize); ylabel('Red Pixel Count', 'FontSize', axisFontSize); title('Histogram of Red Channel', 'FontSize', captionFontSize); % Calculate the histogram of the green channel. hG = subplot(3, 4, 7); [countsG, grayLevelsG] = imhist(greenChannel); maxGLValueG = find(countsG > 0, 1, 'last'); maxCountG = max(countsG); % Plot the histogram of the green channel. bar(countsG, 'g'); grid on; xlabel('Green Gray Levels', 'FontSize', axisFontSize); ylabel('Green Pixel Count', 'FontSize', axisFontSize); title('Histogram of Green Channel', 'FontSize', captionFontSize); % Calculate the histogram of the blue channel. hB = subplot(3, 4, 8); [countsB, grayLevelsB] = imhist(blueChannel); maxGLValueB = find(countsB > 0, 1, 'last'); maxCountB = max(countsB); % Plot the histogram of the blue channel. bar(countsB, 'b'); grid on; xlabel('Blue Gray Levels', 'FontSize', axisFontSize); ylabel('Blue Pixel Count', 'FontSize', axisFontSize); title('Histogram of Blue Channel', 'FontSize', captionFontSize); % Set all axes to be the same height. maxCount = max([maxCountR, maxCountG, maxCountB]); % Set all axes to be the same width. maxGL = max([maxGLValueR, maxGLValueG, maxGLValueB]); if eightBit maxGL = 255; end % If there's a big spike at the last bin, it can be hard to see because % of the box around the axes, so make the box a few bins wider. % This will make it easier to see spikes in the last bin. maxGL = maxGL + 8; axis([hR hG hB], [0 maxGL 0 maxCount]); % Plot all subplot(3, 1, 3); plot(grayLevelsR, countsR, 'r', 'LineWidth', 2); grid on; xlabel('Gray Level', 'FontSize', axisFontSize); ylabel('Pixel Count', 'FontSize', axisFontSize); hold on; plot(grayLevelsG, countsG, 'g', 'LineWidth', 2); plot(grayLevelsB, countsB, 'b', 'LineWidth', 2); title('Histograms of All Channels', 'FontSize', captionFontSize); maxGrayLevel = max([maxGLValueR, maxGLValueG, maxGLValueB]); % Trim x-axis to just the max gray level on the bright end. if eightBit xlim([0 255]); else xlim([0 maxGrayLevel]); end |