Skip to content

Commit

Permalink
Added Image Processing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yiqiangjizhang committed May 3, 2021
1 parent b3fd7b9 commit 3c0840b
Show file tree
Hide file tree
Showing 37 changed files with 1,088 additions and 147 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
% EXAMPLE 1 Basic image manipulation with Matlab
% Manel Soria ESEIAAT - 2021

clear
close

img=imread('dark_horse_manel_soria.tiff'); % reads image (most formats)
imshow(img); % displays image
figure
imshow(img(:,:,1)); % displays image, only red channel
title('Only red');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

class(img) % data type

% Matlab can manipulate images based on different data types
% The most usual are:
% uint8: 8 bits (0 to 255)
% uint16: 16 bits (0 to 65535)
% single, double: floating point images (0 to 1)

size(img) % size (TY,TX, number of channels)

small=imresize(img,0.5); % half size image

figure
imshow(small);
title('Original');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

r=small(:,:,1); % extract red channel
g=small(:,:,2); % green
b=small(:,:,3); % blue

r=r*2; % duplicate signal of one channel / image

img2=cat(3,r,g,b); % concatenate three channels to form

figure
imshow(img2);
title('Double red');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
% EXAMPLE 2 Testing jpg image quality with Matlab
% Manel Soria ESEIAAT - 2021

clear
close all

img=uint8(zeros(200,200,3)); % black image, uint8 (0 to 255)
% img=uint8(ones(200,200,3)*255); % white image, uint8 (0 to 255)

% draw red vertical band
img(1:200,30:40,1)=255;
img(1:200,30:40,2:3)=0;

img(1:200,60:70,2)=255; % green
img(1:200,60:70,1)=0;
img(1:200,60:70,3)=0;

img(1:200,90:100,3)=255; % blue
img(1:200,90:100,1:2)=0; % blue


imwrite(img,'test.png');

set(gca,'DefaultTextFontSize',24)
imshow(imresize(img,2));
title('Original');
set(findall(gcf,'-property','FontSize'),'FontSize',18)


q=80;
imwrite(img,'test.jpg','Quality',q);
img2=imread('test.jpg');
set(findall(gcf,'-property','FontSize'),'FontSize',18)

figure
imshow(imresize(img,2));
title(sprintf('JPG quality %d',q));
set(findall(gcf,'-property','FontSize'),'FontSize',18)

figure

plot(img(100,:,1));
hold on
plot(img2(100,:,1));
legend({'Original','Compressed'})
title(sprintf('Horizontal cut red channel, quality=%d',q));

set(findall(gcf,'-property','FontSize'),'FontSize',18)

Binary file not shown.
33 changes: 33 additions & 0 deletions ImageProcessingExamples/B_Histogram_manipulation/B1_histogram.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
clear
close all
img=imread('low_contrast.jpg');

size(img) % we see it is a monochrome image stored as a RGB, to use JPG

img=img(:,:,1); % treat is as a true monochrome
imshow(img);

% find histogram (our own code)
figure

tic % just to measure computing time
n=zeros(256,1);
for c=0:255 % for every possible gray value
n(c+1)=sum(img(:)==c); % count the number of pixels of that value
end
plot(n)
title('Image histogram "manual" ');
set(findall(gcf,'-property','FontSize'),'FontSize',18);
toc


figure

% find histogram (Matlab)
tic
imhist(img);
title('Image histogram with imhist');
set(findall(gcf,'-property','FontSize'),'FontSize',18);
toc


Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
% Manel Soria - Image contrast

clear
close all
img=imread('low_contrast.jpg');

size(img) % we see it is a monochrome image stored as a RGB, to use JPG

img=img(:,:,1); % treat is as a true monochrome
imshow(img);
title('Original image');

figure
myhist(img); % we use our histogram function
title('Original Image histogram');


% how can we improve contrast ?
imgd=double(img); % we will treat the image as a double (from 0 to 1)
m=min(imgd(:)); % min
M=max(imgd(:)); % max
imgd=(imgd-m)/(M-m); % force min to be 0, max 1
img2=uint8(255*imgd); % convert it to 8 bit again

figure
imshow(img2);
title('Forced to 0:255');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

figure
myhist(img2);
title('Forced to 0:255 histogram');
set(findall(gcf,'-property','FontSize'),'FontSize',18);



% if we examine the histogram, we see that at both ends there is very
% little number of pixels, maybe we can force them to be 0 or 1,
% increasing even more the difference between the intermediate values

imgd=double(img);
m=min(imgd(:));
M=max(imgd(:));
% experiment with different values instead of 0.2 and 0.3
m2=m+0.2*(M-m); % force m2 and below to be black
M2=M-0.3*(M-m); % force M2 and above to be white
imgd=(imgd-m2)/(M2-m2);
imgd(imgd<0)=0; % force
imgd(imgd>1)=1; % force
img3=uint8(255*imgd); % this is to convert it back to 8 bits

figure
imshow(img3)
title('Discarded blacks and whites');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

figure
myhist(img3)
title('Discarded blacks and whites histogram');
set(findall(gcf,'-property','FontSize'),'FontSize',18);



Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
% Manel Soria - Image contrast

clear
close all
img=imread('low_contrast.jpg');

size(img) % we see it is a monochrome image stored as a RGB, to use JPG

img=img(:,:,1); % treat is as a true monochrome
imshow(img);
title('Original');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

figure
myhist(img);
title('Original');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

img2=adapthisteq(img); % much better algorithm .. from help:
% adapthisteq Contrast-limited Adaptive Histogram Equalization (CLAHE).
% adapthisteq enhances the contrast of images by transforming the
% values in the intensity image I. Unlike HISTEQ, it operates on small
% data regions (tiles), rather than the entire image. Each tile's
% contrast is enhanced, so that the histogram of the output region
% approximately matches the specified histogram. The neighboring tiles
% are then combined using bilinear interpolation in order to eliminate
% artificially induced boundaries. The contrast, especially
% in homogeneous areas, can be limited in order to avoid amplifying the
% noise which might be present in the image.
figure
imshow(img2);
title('adapthisteq');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

figure

myhist(img2);
title('adapthisteq histogram');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

% now we increase overall constrast
img3=imadjust(img2,[0.2 0.8],[0 1]);
% J = imadjust(I,[LOW_IN; HIGH_IN],[LOW_OUT; HIGH_OUT]) maps the values
% in intensity image I to new values in J such that values between LOW_IN
% and HIGH_IN map to values between LOW_OUT and HIGH_OUT. Values below
% LOW_IN and above HIGH_IN are clipped;
% (Note that this is what we did in the previous example)

figure
imshow(img3);
title('adapthisteq+imadjust');
set(findall(gcf,'-property','FontSize'),'FontSize',18);

figure
myhist(img3);
title('adapthisteq+imadjust histogram');



71 changes: 71 additions & 0 deletions ImageProcessingExamples/B_Histogram_manipulation/B4_expose_darks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
% Manel Soria 2021 - Exposure darks without loosing whites in a Lansat
% image (single band) of the Elephant Island
% UPC - ESEIAAT

clear
close all
img=imread('expose_darks_monochrome.tiff');
imshow(img);
title('Original image');

figure
imhist(img);
title('Original image histogram');

% 1-Convert to double (from 0 to 1)
img2=double(img);
img2=img2/double(max(img(:)));


% 2-What if the image was in color ?
% Convert from RGB to hsv
% hsv=rgb2hsv(imgd);
% and then, adjust contrast of the 3th channel hsv(:,:,3)
% finally, convert the image back to RGB
% BUT: be aware that color images are more difficult to handle

% 3-Adjust local contrast
% Contrast-limited adaptive histogram equalization (CLAHE)
% MORE LOCAL CONTRAST: More risk for posterization
% WARNING: NON-UNIFORM TREATMENT, PHOTOMETRIC INFO IS LOST
%img3=adapthisteq(img2, 'NumTiles',[16 16],'ClipLimit',0.025) ;

% This is more conservative
img3=adapthisteq(img2, 'NumTiles',[16 16],'ClipLimit',0.01) ;

figure
imshow(img3);
title('adapthisteq');

% 4-Increase exposure dark areas
% This is homogeneous (in all image)
% An easy way that rise darks is to use functions y=x.^p; with p<1

imgd4=img3.^0.25;

figure
imshow(imgd4);
title('adapthisteq + ^1/4 ');

% 5-Adjust overall contrast (discarding some blacks and some whites)

% First we check the histogram to select the values
figure
imhist(imgd4);
title('adapthisteq + ^1/4 Histogram');

imgd5=imadjust(imgd4,[0.45 0.99],[0 1]); % this is, below 0.45 will be 0
figure
imshow(imgd5);
title('adapthisteq + ^1/4 + imadjust');

% show the final histogram .. be careful with posterization
figure
imhist(imgd5);
title('Final histogram');

% 6- Convert back to 16 bit and save
img6=uint16((2^16-1)*imgd5);
imwrite(img6,'out.tiff','tif');


Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions ImageProcessingExamples/B_Histogram_manipulation/myhist.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function myhist( img )
% plots histogram of a uint8 monochrome image

for c=0:255
n(c+1)=sum(img(:)==c);
end

plot(n)
end

Binary file not shown.
Loading

0 comments on commit 3c0840b

Please sign in to comment.