Using cumulative density functions to adjust the histograms of an image.

11 views (last 30 days)
Using the code below i am trying to match the Cumulative density functions of the grayscale gradient and the output image. i am having trouble with doing so.
All of the image arrays are in 16bit as i am hoping to use this code along side a series of 14bit grayscale images from a IR camera to adjust contrast and thersholds by adjusting the gradient array.
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;
inputImage=uint16(imread('cameraman.tif'));
inputImage=(5000+inputImage*3);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(inputImage);
% Display the original gray scale image.
subplot(3, 3, 1);
imshow(inputImage, []);
title('Input Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[ipixelCount inputLevels] =imhist(inputImage , 65536);
subplot(3, 3, 2);
bar(ipixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 inputLevels(end)]);
% Compute the CDF
icdf = uint16(65536*((cumsum(ipixelCount) )/(sum(ipixelCount))));
subplot(3, 3, 3);
plot(icdf);
grid on;
title('CDF of input', 'FontSize', fontSize);
Greyscalegradi = [rows , columns];
Greyscalegradi = uint16(Greyscalegradi);
X1=rows;
Y1=columns;
F=32768/X1;
G=32768/Y1;
for Y= 1:Y1
for X =1:X1
Greyscalegradi (X,Y) = ((F * (X-1))+(G*(Y-1)));
X=X+1;
end
Y=Y+1;
end
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(Greyscalegradi);
% Display the original gray scale image.
subplot(3, 3, 4);
imshow(Greyscalegradi, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] =imhist(Greyscalegradi , 65536);
subplot(3, 3, 5);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
% Compute the CDF
cdf = uint16(65536*((cumsum(pixelCount) )/(sum(pixelCount))));
subplot(3, 3, 6);
plot(cdf);
grid on;
title('CDF of grayscale', 'FontSize', fontSize);
%tansfer function
Tcdf=(cdf);
% subplot(3, 3, 6);
% plot(Tcdf);
% grid on;
% title('CDF of transfer grayscale', 'FontSize', fontSize);
% Equalize the image.
heImage = intlut(inputImage, Tcdf);
% Display the new gray scale image.
subplot(3, 3, 7);
imshow(heImage, []);
title('Equalized Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCountHE grayLevelsHE] = imhist(heImage, 65536);
subplot(3, 3, 8);
bar(pixelCountHE);
grid on;
title('Histogram of Equalized Image', 'FontSize', fontSize);
% xlim([0 grayLevelsHE(end)]); % Scale x axis manually.
% Compute the CDF
cdfHE = ((65536 * (cumsum(pixelCountHE) )/ (sum(pixelCountHE))));
subplot(3, 3, 9);
plot(cdfHE);
grid on;
title('CDF of output', 'FontSize', fontSize);

Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!