Clear Filters
Clear Filters

color enhancement using histogram

1 view (last 30 days)
Maryam Al-Muhaini
Maryam Al-Muhaini on 18 Dec 2019
Answered: Image Analyst on 20 Dec 2019
I keep getting black and white pic while my purpose is to enhance the color using the average of histograms
this is my coe
X = im2double(imread('Capture.jpg'));
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);
z = zeros(size(R));
Rimg = cat(3, R, z, z);
Gimg = cat(3, z, G, z);
Bimg = cat(3, z, z, B);
L256 = linspace(0,1,256).';
z256 = zeros(256,1);
mapR = [L256, z256, z256];
mapG = [z256, L256, z256];
mapB = [z256, z256, L256];
figure,
subplot(2,2,1),image(Rimg); colormap(mapR);
subplot(2,2,2),image(Gimg); colormap(mapG);
subplot(2,2,3),image(Bimg); colormap(mapB);
counts1=histeq(R);
counts2=histeq(G);
counts3=histeq(B);
figure,
subplot(2,2,1),imshow(counts1);
subplot(2,2,2),imshow(counts2);
subplot(2,2,3),imshow(counts3);
mean_counts = mean([counts1(:), counts2(:), counts3(:)],2);
figure, plot(mean_counts)%average
J= histeq(X, mean_counts);%itf
r= histeq(R,mean_counts);
g= histeq(G,mean_counts);
b= histeq(B,mean_counts);
figure,
subplot(2,2,1),imshow(X);
subplot(2,2,2),imshow(r);
subplot(2,2,3),imshow(g);
subplot(2,2,4),imshow(b);

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 18 Dec 2019
Edited: KALYAN ACHARJYA on 18 Dec 2019
It's not black and white image, the resultant also RGB image, see the data types. When I have tried with different image. The color of an image is just a perception, how it looks, mathematically it may not be exactly like that.
  5 Comments
Maryam Al-Muhaini
Maryam Al-Muhaini on 18 Dec 2019
I don't get it. I'm a beginner at using matlab

Sign in to comment.


Image Analyst
Image Analyst on 20 Dec 2019
Try this:
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;
%=======================================================================================
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
% startingFolder = pwd; % or 'C:\wherever';
% if ~exist(startingFolder, 'dir')
% % If that folder doesn't exist, just start in the current folder.
% startingFolder = pwd;
% end
% % Get the name of the file that the user wants to use.
% defaultFileName = fullfile(startingFolder, 'n*.*');
% [baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
% if baseFileName == 0
% % User clicked the Cancel button.
% return;
% end
folder = 'C:\Program Files\MATLAB\R2019b\toolbox\images\imdata';
baseFileName = 'pears.png';
baseFileName = 'toysflash.png';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows1, columns1, numberOfColorChannels1] = size(rgbImage)
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\n"%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.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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 Image Analyst', 'NumberTitle', 'Off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get histograms
histR = imhist(redChannel);
histG = imhist(greenChannel);
histB = imhist(blueChannel);
% Get the mean
histMean = (histR + histG + histB) / 3;
theCDF = cumsum(histMean);
% Normalize 0 - 1
theCDF = rescale(theCDF, 0, 255);
subplot(2, 2, 2);
plot(theCDF, 'b-', 'LineWidth', 2);
grid on;
xlabel('Input Gray Level', 'FontSize', fontSize);
ylabel('Output Gray Level', 'FontSize', fontSize);
title('The CDF', 'FontSize', fontSize, 'Interpreter', 'None');
% theCDF is the intensity mapping
lut = uint8(theCDF);
% Expand the channels
newR = intlut(redChannel, lut);
newG = intlut(greenChannel, lut);
newB = intlut(blueChannel, lut);
% Combine them all into a new RGB image.
rgbImage2 = cat(3, newR, newG, newB);
% Display the image.
subplot(2, 2, 3);
imshow(rgbImage2, []);
axis('on', 'image');
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
0000 Screenshot.png

Community Treasure Hunt

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

Start Hunting!