Tuesday, November 4, 2014

Image Processing: Matlab code - Local Histogram equalization 3x3 window

Letus begin by considering following 64x64 image.

Letus apply local window processing by taking 3x3 window and move certral pixel of local window x(2,2) to output image J. Store above image to your harddisk folder and provide name in path variable below (line#2).


clear all
path = 'E:\xxx\woman_1.gif'
I = imread(path)
[row, col]=size(I);
eI = histeq(I);
h = imhist(I);
for i=2:row-1
for j=2:col-1
a = i
b = j
win = I(i-1:i+1, j-1:j+1);
ewin = histeq(win)
J(i,j) = ewin(2,2)
end
end
imshow(I); 
imhist(I);
imshow(J);
imshow(J);

Result of the local window processing is as follows:





And
to



Note: it may take certain time :)

Image Processing: Matlab Introduction with Histogram Equalization on image


>>>>>>>>>>>> Some basic command and mathematics>>>>>>>>>>>>>

Desc.
Command
Results
Clear all variables
>> clear
>> clear all

Clear screen
>> clc

Array single dimension
x=1:5
X = 1     2     3     4     5
Array with specific interval
t=0:0.1:1
T = 0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000
2D array, matrix with random values
x= rand(3:3)
0.6258    0.8439    0.1939
0.2507    0.3974    0.3631
0.2630    0.1046    0.8745
Another 2D array, matrix
y= rand(3:3)
0.6258    0.8439    0.1939
0.2507    0.3974    0.3631
0.2630    0.1046    0.8745
Show graph
plot(x,y)



List all files in current directory
dirlist = dir('.');
for i = 1:length(dirlist)
dirlist(i)
end

How to find help
>> help imhist

Generate single dimensional and array with zeros
>> h=zeros(1,300);

Variable names

Avoide giving reserved names like pi, i= =sqrt(-1)
Matrix
>> v=[1, 2, 3]
1 2 3

>> v=[1 2 3]
1 2 3

>> v=[1; 2; 3]
1
2
3

>> w=[1+ 5 3 4]
6 3 4

>> v=[1 +2 3]
1 2 3
Multiplication of matrices
>> v=[4 5 6]
>> u=[1; 2; 3]
>> v*u
4    5     6
8    10    12
12   15    18
3x3 matrix

>> u*v
32
1x1 matrix





Plot Graphs
>> a=0
>> b=1
>> n=10
>> x=linspace(a,b,n+1)  à h = b-a/(n+1)-1 = 1/10=0.1
>> y = sin(x)
>> z = length(x)
Answer:
x = 0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000
y = 0    0.0998    0.1987    0.2955    0.3894    0.4794    0.5646    0.6442    0.7174    0.7833    0.8415
z = 11

>> plot(x,y)
>> plot(x,y, 'r') à r=red, g=green, b=blue, y=yellow, c=cyan, m=magenta, k=black
>> plot(x,y, 'red')
>> plot(x,y, 'red--') à -- dashed, *, x, <, >, . , .- , -.
>> plot(x,y, 'red*')
>> plot(x,y, 'red.')
>> plot(x,y, 'red*', 'linewidth', 3,  'markersize', 20)
plot(x,y, 'red*', 'linewidth', 3,  'markersize', 10)

Quadretic equation
Imagine an equateion Ax^2 + Bx + C = 0
x = -B +/- Sqrt(B^2 – 4AC)/2A
Program:
>> A=1, B=2, C=3
>> x(1)=(-B+sqrt(B^2-4*A*C))/(2*A)              à ans: x = -1.0000 + 1.4142i
>> x(2)=(-B-sqrt(B^2-4*A*C))/(2*A)  à ans: x = -1 + 1.4142i  -1 - 1.4142i
X now contains two values in an array.

>>>>>>>>>>>> Image Processing >>>>>>>>>>>>>
Read an image
>> path = E:\MS_CS\Semester_1\3. Digtal.Img.Processing\Matlab\images\a.jpg'
>> A = imread(path)
Read built-in  image
>> I=imread('cameraman.tif');
>> imshow(I)
display an image
>> imshow(A)

Negative of an image
>> B = imcomplement(A)
>> imshow(B)

Blue Filtering of an image
>> B = A
>> B = double(A);
B(:,:,3) = 3*B(:,:,3);
B = uint8(B);
>> imshow(B)

r = A(:,:,1);
g = A(:,:,2);
b = A(:,:,3);

Color and sizing of an image
>> [m,n,k] = size(A)
m = h, n = w, k = colors/graylevels

Display histogram of an image
Method 1:
>> [count,bin] = hist(A(:), 0:255);
>> figure,plot(bin, count);
image >> 


Method 2:
>> imhist(A);
Error using imhist
Expected input number 1, I or X, to be two-dimensional.
Reason: input image is RGB – change to gray scale.
>> imhist(rgb2gray(A));


Display histogram equalization of an image
>> B = rgb2gray(A)
>> J = histeq(B);
>> figure, imshow(B), figure, imshow(J)


And histogram is now stretched:
>> imhist(J)

Number of Pixels in an image
>> numel(A)




MATLAB Program to apply Histogram Equalization on image
Just copy paste in matlab, it should work 
It is an alternative program not using built-in histeq function.
clear all
clc
I=imread('cameraman.tif');
I=double(I);
maximum_value=max((max(I)));
[row col]=size(I);
c=row*col;
h=zeros(1,300);
z=zeros(1,300);
for n=1:row
for m=1:col
if I(n,m) == 0
I(n,m)=1;
end
end
end

for n=1:row
for m=1:col
t = I(n,m);
h(t) = h(t) + 1;
end
end
pdf = h/c;
cdf(1) = pdf(1);
for x=2:maximum_value
cdf(x) = pdf(x) + cdf(x-1);
end

new = round(cdf * maximum_value);
new= new + 1;
for p=1:row
for q=1:col
temp=I(p,q);
b(p,q)=new(temp);
t=b(p,q);
z(t)=z(t)+1;
end
end
b=b-1;

subplot(2,2,1), imshow(uint8(I)) , title(' Image1');

subplot(2,2,2), bar(h) , title('Histogram of d Orig. Image');

subplot(2,2,3), imshow(uint8(b)) , title('Image2');

subplot(2,2,4),bar(z) , title('Histogram Equalisation of image2');