Commit 89f285c4 authored by Rohit Prasad's avatar Rohit Prasad

assignment 1

parents
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>myMainScript</title><meta name="generator" content="MATLAB 9.4"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2018-08-06"><meta name="DC.source" content="myMainScript.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }
</style></head><body><div class="content"><h1></h1><!--introduction--><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">MyMainScript</a></li><li><a href="#2">Q1 (a)</a></li><li><a href="#3">Q1 (b)</a></li><li><a href="#4">Q1 (c)</a></li></ul></div><h2 id="1">MyMainScript</h2><h2 id="2">Q1 (a)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/circles_concentric.png'</span>);
figure;
subplot(2,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(2,2,3);
displayImage(myShrinkImageByFactorD(im, 2), <span class="string">'Shrinked by factor 2'</span>);
subplot(2,2,4);
displayImage(myShrinkImageByFactorD(im, 3), <span class="string">'Shrinked by factor 3'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.362417 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_01.png" alt=""> <h2 id="3">Q1 (b)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/barbaraSmall.png'</span>);
figure;
subplot(1,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(1,2,2);
displayImage(myBilinearInterpolation(im), <span class="string">'Resized with Bilinear Interpolation'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.279954 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_02.png" alt=""> <h2 id="4">Q1 (c)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/barbaraSmall.png'</span>);
figure;
subplot(1,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(1,2,2);
displayImage(myNearestNeighborInterpolation(im), <span class="string">'Resized with Nearest Neighbor Interploation'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.278882 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_03.png" alt=""> <p class="footer"><br><a href="https://www.mathworks.com/products/matlab/">Published with MATLAB&reg; R2018a</a><br></p></div><!--
##### SOURCE BEGIN #####
%% MyMainScript
%% Q1 (a)
tic;
im = imread('../data/circles_concentric.png');
figure;
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,3);
displayImage(myShrinkImageByFactorD(im, 2), 'Shrinked by factor 2');
subplot(2,2,4);
displayImage(myShrinkImageByFactorD(im, 3), 'Shrinked by factor 3');
toc;
%% Q1 (b)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myBilinearInterpolation(im), 'Resized with Bilinear Interpolation');
toc;
%% Q1 (c)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myNearestNeighborInterpolation(im), 'Resized with Nearest Neighbor Interploation');
toc;
##### SOURCE END #####
--></body></html>
\ No newline at end of file
function [] = displayColorbar(is_grayscale)
%DISPLAYCOLORBAR Summary of this function goes here
% Detailed explanation goes here
if is_grayscale
colormap(gray(300));
else
colormap(hsv(300));
end
colorbar;
end
function [] = displayImage(im, im_title)
%DISPLAYIMAGE Summary of this function goes here
% Detailed explanation goes here
is_grayscale = size(im, 3) == 1;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
imshow(im);
axis on;
xlabel('Pixels');
ylabel('Pixels');
displayColorbar(is_grayscale);
title(im_title);
end
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>myMainScript</title><meta name="generator" content="MATLAB 9.4"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2018-08-06"><meta name="DC.source" content="myMainScript.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }
</style></head><body><div class="content"><h1></h1><!--introduction--><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">MyMainScript</a></li><li><a href="#2">Q1 (a)</a></li><li><a href="#3">Q1 (b)</a></li><li><a href="#4">Q1 (c)</a></li></ul></div><h2 id="1">MyMainScript</h2><h2 id="2">Q1 (a)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/circles_concentric.png'</span>);
figure;
subplot(2,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(2,2,3);
displayImage(myShrinkImageByFactorD(im, 2), <span class="string">'Shrinked by factor 2'</span>);
subplot(2,2,4);
displayImage(myShrinkImageByFactorD(im, 3), <span class="string">'Shrinked by factor 3'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.362417 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_01.png" alt=""> <h2 id="3">Q1 (b)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/barbaraSmall.png'</span>);
figure;
subplot(1,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(1,2,2);
displayImage(myBilinearInterpolation(im), <span class="string">'Resized with Bilinear Interpolation'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.279954 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_02.png" alt=""> <h2 id="4">Q1 (c)</h2><pre class="codeinput">tic;
im = imread(<span class="string">'../data/barbaraSmall.png'</span>);
figure;
subplot(1,2,1);
displayImage(im, <span class="string">'Original Image'</span>);
subplot(1,2,2);
displayImage(myNearestNeighborInterpolation(im), <span class="string">'Resized with Nearest Neighbor Interploation'</span>);
toc;
</pre><pre class="codeoutput">Elapsed time is 0.278882 seconds.
</pre><img vspace="5" hspace="5" src="myMainScript_03.png" alt=""> <p class="footer"><br><a href="https://www.mathworks.com/products/matlab/">Published with MATLAB&reg; R2018a</a><br></p></div><!--
##### SOURCE BEGIN #####
%% MyMainScript
%% Q1 (a)
tic;
im = imread('../data/circles_concentric.png');
figure;
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,3);
displayImage(myShrinkImageByFactorD(im, 2), 'Shrinked by factor 2');
subplot(2,2,4);
displayImage(myShrinkImageByFactorD(im, 3), 'Shrinked by factor 3');
toc;
%% Q1 (b)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myBilinearInterpolation(im), 'Resized with Bilinear Interpolation');
toc;
%% Q1 (c)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myNearestNeighborInterpolation(im), 'Resized with Nearest Neighbor Interploation');
toc;
##### SOURCE END #####
--></body></html>
\ No newline at end of file
function [output_img] = myBilinearInterpolation(im)
%MYBILINEARINTERPOLATION Summary of this function goes here
% Detailed explanation goes here
img = im2double(im);
% img = im2double(uint8([255, 50; 150, 130]));
[m,n] = size(img);
o_m = 3*m-2; % output image rows
o_n = 2*n-1; % output image cols
s_r = m / o_m;
s_c = n / o_n;
output_img = zeros(o_m, o_n);
for i = 1:o_m
for j = 1:o_n
r_f = i * s_r;
c_f = j * s_c;
if (r_f < 1)
r_f = 1;
end
if (r_f >= m)
r_f = m-1;
end
if (c_f < 1)
c_f = 1;
end
if (c_f >= n)
c_f = n-1;
end
[r,c] = deal(floor(r_f), floor(c_f));
delta_r = r_f - r;
delta_c = c_f - c;
output_img(i,j) = img(r,c) * (1-delta_r) * (1-delta_c) ...
+ img(r+1,c) * delta_r * (1-delta_c) ...
+ img(r,c+1) * (1-delta_r) * delta_c ...
+ img(r+1,c+1) * delta_r * delta_c;
end
end
end
%% MyMainScript
%% Q1 (a)
tic;
im = imread('../data/circles_concentric.png');
figure;
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,3);
displayImage(myShrinkImageByFactorD(im, 2), 'Shrinked by factor 2');
subplot(2,2,4);
displayImage(myShrinkImageByFactorD(im, 3), 'Shrinked by factor 3');
toc;
%% Q1 (b)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myBilinearInterpolation(im), 'Resized with Bilinear Interpolation');
toc;
%% Q1 (c)
tic;
im = imread('../data/barbaraSmall.png');
figure;
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
displayImage(myNearestNeighborInterpolation(im), 'Resized with Nearest Neighbor Interploation');
toc;
function [output_img] = myNearestNeighborInterpolation(im)
%MYNEARESTNEIGHBORINTERPOLATION Summary of this function goes here
% Detailed explanation goes here
img = im2double(im);
[m,n] = size(img);
o_m = 3*m-2; % output image rows
o_n = 2*n-1; % output image cols
s_r = m / o_m;
s_c = n / o_n;
output_img = zeros(o_m, o_n);
for i = 1:o_m
for j = 1:o_n
r_f = i * s_r;
c_f = j * s_c;
if (r_f < 1)
r_f = 1;
end
if (r_f > m-1)
r_f = m-1;
end
if (c_f < 1)
c_f = 1;
end
if (c_f > n-1)
c_f = n-1;
end
[r,c] = deal(round(r_f), round(c_f));
output_img(i,j) = img(r,c);
end
end
end
function [sampledImg] = myShrinkImageByFactorD(im, d)
%myShrinkImageByFactorD
% Shrink the image size by a factor of d along each dimension
% using image subsampling by sampling / selecting every d-th
% pixel along the rows and columns.
img = im2double(im);
sampledImg = im2uint8(img(1:d:end,1:d:end));
end
function [] = displayColorbar(is_grayscale)
%DISPLAYCOLORBAR Summary of this function goes here
% Detailed explanation goes here
if is_grayscale
colormap(gray(300));
else
colormap(hsv(300));
end
colorbar;
end
This diff is collapsed.
function [] = displayColorbar(is_grayscale)
%DISPLAYCOLORBAR Summary of this function goes here
% Detailed explanation goes here
if is_grayscale
colormap(gray(300));
else
colormap(hsv(300));
end
colorbar;
end
function [] = displayHist(im, im_title)
%DISPLAYHIST Summary of this function goes here
% Detailed explanation goes here
is_grayscale = size(im, 3) == 1;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
imhist(im2uint8(im));
title(im_title);
end
function [] = displayImage(im, im_title)
%DISPLAYIMAGE Summary of this function goes here
% Detailed explanation goes here
is_grayscale = size(im, 3) == 1;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
imshow(im);
axis on;
xlabel('Pixels');
ylabel('Pixels');
displayColorbar(is_grayscale);
title(im_title);
end
function [] = displayColorbar(is_grayscale)
%DISPLAYCOLORBAR Summary of this function goes here
% Detailed explanation goes here
if is_grayscale
colormap(gray(300));
else
colormap(hsv(300));
end
colorbar;
end
This diff is collapsed.
function [output_im] = myAHE(im, m)
%MYAHE Summary of this function goes here
% Detailed explanation goes here
im = uint16(im);
im = im + 1;
output_im = zeros(size(im));
for channel=1:size(im,3)
output_im(:,:,channel) = nlfilter(im(:,:,channel), [m,m], @ahe);
end
output_im = uint8(output_im);
% figure;
% imshow(uint8(im-1));
%
% figure;
% imshow(uint8(output_im));
end
function [intensity] = ahe(block)
m = size(block, 1);
x = block(ceil(m/2), ceil(m/2)) - 1;
block = block(block > 0);
block_vector = block(:) - 1;
freq = imhist(uint8(block_vector), 256);
pn = freq / size(block_vector, 1);
cdf = cumsum(pn);
intensity = floor(255 * cdf(x+1));
end
function [output_im] = myCLAHE(im, m, clip)
%MYCLAHE Summary of this function goes here
% Detailed explanation goes here
global clip;
im = uint16(im);
im = im + 1;
output_im = zeros(size(im));
for channel=1:size(im,3)
output_im(:,:,channel) = nlfilter(im(:,:,channel), [m,m], @clahe);
end
output_im = uint8(output_im);
end
function [intensity] = clahe(block)
global clip;
m = size(block, 1);
x = block(ceil(m/2), ceil(m/2)) - 1;
block = block(block > 0);
block_vector = block(:) - 1;
freq = imhist(uint8(block_vector), 256);
pn = freq / size(block_vector, 1);
pn(pn >= clip) = clip;
pn = pn + (1 - sum(pn))/size(pn,1);
cdf = cumsum(pn);
intensity = floor(255 * cdf(x+1));
end
function [output_img] = myHE(im)
%MYHE Summary of this function goes here
% Detailed explanation goes here
img = im;
output_img = zeros(size(img));
for channel=1:size(img,3)
img_vector = img(:,:,channel);
img_vector = img_vector(:);
freq = imhist(img_vector, 256);
pn = freq / size(img_vector,1);
cumulative_pn = cumsum(pn);
equalize = @(x) floor(255 * cumulative_pn(x+1));
output_img(:,:,channel) = arrayfun(equalize, img(:,:,channel));
end
output_img = uint8(output_img);
% figure;
% set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% subplot(2,2,1);
% imshow(img);
% displayColorbar(is_grayscale);
% subplot(2,2,2);
% imshow(uint8(output_img));
% displayColorbar(is_grayscale);
% subplot(2,2,3);
% imhist(img);
% subplot(2,2,4);
% imhist(uint8(output_img));
end
function [output_im] = myHM()
%MYHM Summary of this function goes here
% Detailed explanation goes here
global cum_im_hist cum_im_ref_hist lookup_table;
im = imread('../data/retina.png');
im_mask = imread('../data/retinaMask.png');
im_ref = imread('../data/retinaRef.png');
im_ref_mask = imread('../data/retinaRefMask.png');
output_im = zeros(size(im)); % output image
for channel=1:size(im, 3)
lookup_table = zeros(1, 256);
lookup_table(:) = -1;
im_channel = im(:,:,channel);
im_hist = imhist(im_channel(im_mask), 256);
cum_im_hist = cumsum(im_hist);
im_ref_channel = im_ref(:,:,channel);
im_ref_hist = imhist(im_ref_channel(im_ref_mask), 256);
cum_im_ref_hist = cumsum(im_ref_hist);
for i=1:size(output_im, 1)
for j=1:size(output_im, 2)
output_im(i,j,channel) = hist_match(im_channel(i,j) + 1);
end
end
end
output_im = uint8(output_im);
% figure;
% imshow(im);
%
% figure;
% imshow(uint8(output_im));
end
function [idx] = hist_match(x)
global cum_im_hist cum_im_ref_hist lookup_table;
if (lookup_table(x) ~= -1)
idx = lookup_table(x);
else
[v,idx] = min(abs(cum_im_hist(x)-cum_im_ref_hist));
lookup_table(x) = idx;
end
end
function [output_img] = myLinearContrastStretching(im)
%MYLINEARCONTRASTSTRETCHING Summary of this function goes here
% Detailed explanation goes here
img = im2double(im);
output_img = zeros(size(img));
for channel=1:size(img,3)
low_value = min(min(img(:,:,channel)));
high_value = max(max(img(:,:,channel)));
output_img(:,:,channel) = ...
(img(:,:,channel) - low_value) ...
/ (high_value - low_value);
end
end
%% MyMainScript
%% Q2(a)
tic;
figure;
im = imread('../data/barbara.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myLinearContrastStretching(im);
displayImage(output_im_he, 'Linear Contrast Enhanced Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Linear Contrast Enhanced Image Histogram');
figure;
im = imread('../data/TEM.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myLinearContrastStretching(im);
displayImage(output_im_he, 'Linear Contrast Enhanced Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Linear Contrast Enhanced Image Histogram');
figure;
im = imread('../data/retina.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myLinearContrastStretching(im);
displayImage(output_im_he, 'Linear Contrast Enhanced Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Linear Contrast Enhanced Image Histogram');
figure;
im = imread('../data/canyon.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myLinearContrastStretching(im);
displayImage(output_im_he, 'Linear Contrast Enhanced Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Linear Contrast Enhanced Image Histogram');
figure;
im = imread('../data/church.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myLinearContrastStretching(im);
displayImage(output_im_he, 'Linear Contrast Enhanced Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Linear Contrast Enhanced Image Histogram');
% Liner contrast stretching isn't effective here because
% the histogram of the original image is already distributed on the whole range
% of intensities. So the linear contrast enhancement does not have much effect
% on it when we apply max-min normalization;
% Formula
% new_pixel = ((old_pixel - low) / (high - low) ) * 255
toc;
%% Q2(b)
tic;
figure;
im = imread('../data/barbara.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Histogram Equalized Histogram');
figure;
im = imread('../data/TEM.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Histogram Equalized Histogram');
figure;
im = imread('../data/retina.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Histogram Equalized Histogram');
figure;
im = imread('../data/canyon.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Histogram Equalized Histogram');
figure;
im = imread('../data/church.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,2,3);
displayHist(im, 'Original Image - Histogram');
subplot(2,2,4);
displayHist(output_im_he, 'Histogram Equalized Histogram');
% Histogram Equalization will be preferred for Image 5. It works better
% becuause of non linear mapping. It stretches the area with high
% intensities and compresses the area with low intensities.
toc;
%% Q2 (c)
tic;
figure;
im = imread('../data/retina.png');
subplot(2,3,1);
displayImage(im, 'Original Image');
subplot(2,3,2);
output_im_he = myHE(im);
displayImage(output_im_he, 'Histogram Equalized Image');
subplot(2,3,3);
output_im_hm = myHM();
displayImage(output_im_hm, 'Histogram Matched Image');
subplot(2,3,4);
displayHist(im, 'Original Image Histogram');
subplot(2,3,5);
displayHist(output_im_he, 'Histogram Equalized Image Histogram');
subplot(2,3,6);
displayHist(output_im_hm, 'Histogram Matched Image Histogram');
toc;
%% Q2 (d)
tic;
figure;
im = imread('../data/barbara.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_window_51 = myAHE(im, 51);
displayImage(output_im_window_51, 'AHE with window size 51');
subplot(2,2,3);
output_im_window_5 = myAHE(im, 5);
displayImage(output_im_window_5, 'AHE with window size 5');
subplot(2,2,4);
output_im_window_151 = myAHE(im, 151);
displayImage(output_im_window_151, 'AHE with window size 151');
figure;
im = imread('../data/canyon.png');
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
output_im_window_51 = myAHE(im, 51);
displayImage(output_im_window_51, 'AHE with window size 51');
% subplot(2,2,3);
% output_im_window_5 = myAHE(im, 5);
% displayImage(output_im_window_5, 'AHE with window size 5');
% subplot(2,2,4);
% output_im_window_151 = myAHE(im, 151);
% displayImage(output_im_window_151, 'AHE with window size 151');
figure;
im = imread('../data/TEM.png');
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
output_im_window_51 = myAHE(im, 51);
displayImage(output_im_window_51, 'AHE with window size 51');
% subplot(2,2,3);
% output_im_window_5 = myAHE(im, 5);
% displayImage(output_im_window_5, 'AHE with window size 5');
% subplot(2,2,4);
% output_im_window_151 = myAHE(im, 151);
% displayImage(output_im_window_151, 'AHE with window size 151');
toc;
%% Q2 (e)
tic;
figure;
im = imread('../data/barbara.png');
subplot(2,2,1);
displayImage(im, 'Original Image');
subplot(2,2,2);
output_im_window_75_clip_0_5 = myCLAHE(im, 75, 0.5);
displayImage(output_im_window_75_clip_0_5, 'CLAHE with window size 75 and clip 0.5');
subplot(2,2,3);
output_im_window_75_clip_0_25 = myCLAHE(im, 75, 0.25);
displayImage(output_im_window_75_clip_0_25, 'CLAHE with window size 75 and clip 0.25');
figure;
im = imread('../data/canyon.png');
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
output_im_window_75_clip_0_5_canyon = myCLAHE(im, 75, 0.5);
displayImage(output_im_window_75_clip_0_5_canyon, 'CLAHE with window size 75 and clip 0.5');
figure;
im = imread('../data/TEM.png');
subplot(1,2,1);
displayImage(im, 'Original Image');
subplot(1,2,2);
output_im_window_75_clip_0_5_tem = myCLAHE(im, 75, 0.5);
displayImage(output_im_window_75_clip_0_5_tem, 'CLAHE with window size 75 and clip 0.5');
toc;
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment