Tutorial to discuss steps on how to install Anaconda 5.3聽 and configure OpenCv3.2聽 in Anaconda 5.3 for python 3.6.

After completing these steps I am sure you can use anaconda/Python3聽 and OpenCv 3.2.

Introduction

In office I was assigned a simple work in image processing which required extra modules of OpenCv in Python. It was simple task but I struggled a lot to configure python with opencv modules 馃槮 . After wasting my weekends聽 I completed the task on time and came to know how easy it was 馃檪 .

For Ubuntu 16.04 LTS (almost similar steps for聽 Window 7).

1]. Download the Anaconda according to your system configuration from below link.

If we calculate the eigen value and eigen vector of 聽data; eigen vector 聽represent which basis direction data is spread and eigen value informs聽which basis direction (eigen vector ) have more information about data.

Basics of Eigen values and Eigen vectors :

Suppose A is matrix with size 聽N x N.

V is vector with size N.

V is called the eigen vector of A.

A V 聽= 聽聽位 V 聽 聽 聽 聽 ……. (i)

if multiplication of A scale the vector V .

In equation (i) 聽位 is called the eigen value of matrix.

Function eig is used in MATLAB to get the eigen vector and eigen value.

where聽聽位 is diagonal eigenvalues, I is identity matrix and 聽A is given input matrix.

Determinant of 聽matrix A 聽(|A|) will be product of all eigen values.

Set of eigen vector is called the spectrum of matrix.

Symmetric matrix :聽

If transpose of matrix does not change the matrix its called symmetric matrix.

S = S T

where ST is transpose of S matrix.

One of the example of symmetric matrix is co-variance matrix.

The property of symmetric matrix is that if we calculate eigen values and eigen vectors, all eigen values will be real.

Eigen Value Decomposition:

if V1 and V2 correspond to eigen values 位 1 and聽位2 , if聽位1 is not equal to聽位2 then vector V1 and V2 will be orthogonal.

Lets take eigen vectors 聽V = ( V1 , V2 , V3 ….) 聽 (orthogonal matrix)

if 聽V*VT is zero聽we can say columns are orthogonal

and if 聽螞 聽= 聽(聽位 1,聽位 2,聽位 3….) 聽diagonal eigen values.

we can write

S = 聽V聽螞 VT 聽 聽 聽……. 聽(3)

where VT is transpose of V

equation 3 is called the eigen value decomposition.

Lets write the code of eigen value decomposition in MATLAB.

To make sense of 聽Eigen value decomposition input matrix should be symmetry matrix so eigen value will be real. Will calculate co variance matrix which is symmetry matrix.

%%%%%%%%%%%%%%%%%%%%%%%%% Code end %%%%%%%%%%%%%%%%%%%

Results:聽

Input image

Symmetric or Covariance input image generated by Lena image:

Covariance image reconstruction by top 5 eigen values and corresponding vectors:

Loss of information because we used only top 聽5 eigen vector 聽= 27.84 percentage

Top 5 eigen vectors contain聽72.16 percentage聽information of input image

Covariance image reconstruction by 1聽eigen vector:

Loss of information 聽= 72.40聽percentage

Top 1聽eigen vector contain 27.8 percentage聽information of input image

Applications:

Eigen value docomposition is the basic of Sigular value decomposition and Principal Component Analaysis with the help of PCA we can able to reconstruct our original lena image which we will discuss in futher post.

GLCM (gray level co-occurrence matrix) is mainly useful to perform the texture analysis and find the features from image.

As name suggested its work on gray image and try to create sort of 2 d histogram from image.

Main application of GLCM are texture analysis, feature extraction and segmentation.

Steps to Calculate GLCM matrix :

Lets assume image I which is gray image.

Initialize GLCM matrix size 256 x 256 聽(256 is level of GLCM).

Suppose we use zero angle of GLCM means direction of GLCM is horizontal.

Suppose distance of GLCM is 1, means we just look horizontally next pixel to current pixel.

In image 聽I(i,j) get the gray value (suppose value of pixel is a = 聽127 at I(i,j)), and get gray 聽value I(i,j+1) in case of distance 1 and zero degree of 聽GLCM (suppose value of pixel is b = 58 at I(i,j+1)).

聽Go to GLCM matrix co-ordinate聽(a = 127, b=58) and increment the value by 1.

Iterate the full image that will give us GLCM matrix of聽zero degree for聽distance 1.

According to texture type, GLCM distance can be decided.

For road 聽because texture changes so rapidly we consider the small distance to calculate GLCM but for聽bricks, distance of the GLCM matrix should be large.

Angle of GLCM should be selected according to 聽direction of image texture changes, so in brick image we want to consider 2 direction 0 degree and 90 degree.

Calculate features from GLCM matrix, there are many features are available to perform the texture analysis like contrast, correlation, energy , and聽homogeneity etc.

We will get the contrast feature from GLCM matrix which are sufficient to say weather texture is rough or smooth.

Lets write basic GLCM code which calculate the zero degree GLCM for 256 level and get聽contrast feature.

%%%%%%%%%%%%%%%%%%%%%%%%%Code Start here %%%%%%%%%%%%%%%

%%%%% 聽GLCM function start here %%%%%%%%%%%%%

function GLCM_0 = getGLCM0(img_gray, distance) %% function calculate the GLCM matrix at zero degree angle and given distance %% GLCM_0 is GLCM matrix for 0 degree angle. %% img_gray is input gray image %% distance is distance of GLCM calculated

Lest take two input image first is road texture and second is brick image apply the GLCM and get the contrast feature.

Result:聽

Contrast feature of figure. 2 road texture is 聽3.9489e+03

Contrast feature of figure.3 Brick texture is 91.0206

As from result we can say that road texture have so much texture because of that聽its contrast value is so much high if less texture will be present in image contrast value will be low.

So for smooth area in image聽contrast will be low, 聽for rough area contrast will be high.

* Note: GLCM value depend on the size of image we are performing the GLCM so size of image should be fix or normalize.

For more 聽information about GLCM 聽feature analysis please go through the paper聽harlick texture features聽.

%%%%%%%%%%%% Code End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Following are the main 2 D project transform :

聽Euclidean Transform

Similarity Transform

Affine Transform

Projective Transform

Euclidean Transform :

Let define transformation matrix for Euclidean transform

T = [聽R11 聽R12 聽Tx 聽; 聽R21 聽R22 聽Ty ; 0 0 1]

Where R11 , R12, R21 and 聽R22 are rotation matrix and Tx and Ty are translation. So it have have 3 degree of freedom 1 in rotation and 2 in translation.

lets聽take 30 degree rotation angle and -100 pixel translation in both direction

Result of Euclidean Transform

Euclidean Transform dont have property to scale the images.

Similarity Transform:

Let define transformation matrix for聽Similarity聽transform

if in Euclidean transform we added the feature of scale (s) its become similarity tranform

lets聽take 40 degree rotation angle and -100 pixel translation in both direction and 1.5 scaling factor

Affine Transform :

Let define transformation matrix for聽Affine聽transform

T = [ a11 聽a12 聽Tx 聽; a21 聽a22 聽Ty ; 0 0 1]

In Similarity transform if we can able to take different angle as well as different scale it will become affine transform.

lets聽take 40 degree rotation angle in x direction and 30 degree rotation angle in y direction , -100 pixel translation in both direction, 0.7 scaling factor in x direction and 1.5 聽scaling factor in y direction.

Projective Transform : 聽

Define the transformation full matrix which include the homogeneous 聽factor as well.

Projective transform have 8 degree of freedom because we added 2 聽coefficient h31 聽h32 in affine transform matrix which take care of image homogeneous co-ordinate geometry. (might be in furure calibration post i will discuss it about in detail)

In this post, I will discuss the Haar wavelet analysis (wavelet transform) in image processing.

Applications of Haar wavelet transform in image processing are Feature extraction, Texture analysis, Image compression etc.

Wavelet is time frequency resolutions analysis.

As shown in above figure.1 聽its have the information of different time frequencies of signal.

Its like applying a comb of filters bands in signal they are called levels. First we apply full frequency band and have no knowledge of time.

Apply filter which have half bandwidth of signal lose some frequency information but 聽have some knowledge of time resolution called first level decomposition.

Further reduce the filter bandwidth will be second level decomposition.

Haar wavelet is made of two filters one is low pass filter and second high pass filter.

Coefficients of Low pass filter 聽= [0.707, 0.707]

Coefficients of High pass filter 聽= [-0.707, 0.707]

Figure .3 shows the block diagram of first level decomposition.

I is image of size m , n. We take the image column vectors (1d) and apply the low pass filter and high pass filter.

聽Down sample the output and reshape the image I1 of size m, n/2 (size is n/2 because we down sampled the columns by 2).

Apply low and high pass filter in row wise in image I1 again down sample the output and reshape the image (m/2,n/2).

Figure. 5 shows the result of haar wavelet decomposition results at first level.

LL filter output will proceeds for next level decomposition. As its contains most information of input image and looks almost same at down sampled resolution.

HH, LH 聽and HL will have texture information in vertical, horizontal and ramp directions. HH, LH and HL are called haar features.

We can further 聽decompose the LL image into LL1, LH1, HL1 and HH1 images.

MATLAB provide Wavelet Toolbox GUI to perform the wavelet analysis.

%% function haar_wave calculate the haar wave decomposition of given imput image %% Image in :- input gray level image %% LL :- Low Low band output image %% LH :- Low High band output image %% HL :- High Low band output image %% HH :- High High band output image

%% apply low pass filter in one d image column wise image
image_low_pass_1D = filter(filter_L,1,image_vector);
image_high_pass_1D = filter(filter_H,1,image_vector);

% Down sample the image low and high pass image.
temp = 1;
Dimage_low_pass_1D = 0;
Dimage_high_pass_1D = 0;

for i = 1:2:length(image_low_pass_1D)
Dimage_low_pass_1D(temp) = image_low_pass_1D(i);
Dimage_high_pass_1D(temp) = image_high_pass_1D(i);
temp = temp+1;
end

%% reshape the image in 2 D, size will be row/2 , col
Dimage_low_pass = reshape(Dimage_low_pass_1D,round(row/2), col)’;
Dimage_high_pass = reshape(Dimage_high_pass_1D, round(row/2),col)’;

%% conver the image in 1 D but in column vector, I transpose Dimage_low_paas image above.

Image blending is the process to blend (mixing) two images.

One of the important application of image blending is to remove the artifacts while creating the panorama or stitching the images.

Image blending also used to add the object in background or fore ground in images/scenes.

Lets take the two images which we want to blend…..

Following are some of the image blending process :

Suppose first image is a and second image is b

1. Multiply 聽(darken) image blending:

聽 聽 聽 聽 聽 聽 聽

F_blend (a,b) = a *b聽

Darken image blending is multiplication of 聽two images pixel by pixel. Result of blending will be darker compared to both input images as shown in above 聽figure .1

2.Screen or brighter image blending:

聽F_blend (a, b) = 1 – (1-a)* (1-b)

Screen聽image blending is reverse process of darken image blending its make the image much brighter as shown in figure.2 so if in case we want dark area also be brighter we can use screen image blending .

3. Overlay Image blending :

F_blend (a, b) 聽 = 聽 聽 聽 聽 聽1 – 2* (1-a) * (1-b) 聽if 聽(a > .5) 聽 else 聽 2* a * b 聽

Overlay blending provide the results in which darker area will become more darker and brighter area becomes brighter on the basis of base image as shown in figure 3. The image in which we check condition is called the base image here we checked the condition on image a .

4. Weightage image blending :聽

Figure. 4 Weightage image blending.

F_blend (a,b) = 聽(weight * a + (1-weight)*b ) /2

Weightage blending provide the weights to images and take the average of them.聽Figure 4 shows the weightage blending where the weight value is 0.5. Range of weight is 0 to 1.

5. Cross fading blending:

Cross fading is one of the most important blending used to smooth panorama images.

Figure. 5 shows we create the ramp function for input images where ramp intensity is 0 to 1 and width of the ramp is the part of image we want to blend.

We take聽two ramp images according to figure. 5 ad add them to get the result.

We took full ramp to blend the image as shown in figure. 7 . In left most side image 1 effect is dominant and in right most side image 2 is dominant. In middle it will be average.

Figure. 8 聽shows the image feathering by聽step function . Figure 8 we can apply the cross fading in 聽middle strip window to remove the sharp line you can see the result in Figure 9.

In some applications we create image pyramid and apply the cross fading in each gaussian and laplacian level聽and reconstruct the result. That is known as pyramid blending process.

%% assign the value only on index where img1 < .5
overlay_blending(img1<.5) = 2.*img1(img1<.5).*img2(img1<.5); %% assign the value only on index where img1 > .5

%% genrate ramp function which size is widt of image ( full ramp function)
ramp1 = (0:1/col:1);
ramp2 = (1:-1/col:0);

cross_fading = zeros(row,col,channel);

for cols = 1: col

%%% multiply the ramp function with image to get ramp image 1 and ramp image 2 and %%add them to get result
cross_fading(:,cols,:) = (ramp2(cols).*img1(:,cols,:) + ramp1(cols).*img2(:,cols,:));

%% genrate step images and add them
feathering(:,1:col/2,:) = img1(:,1:col/2,:);
feathering(:,col/2:end,:) = img2(:,col/2:end,:);

imshow(feathering)
title(‘image feathering’)

ramp3 = (0:1/100:1);
ramp4 = (1:-1/100:0);

ground = zeros(1,(col/2)-50);
one = ones(1, (col/2)-50);

%% generate the ramp function for cross fading in middle of image with 100 pixel width.
function3 = [ground ramp3 one];
function4 = [one ramp4 ground];

Gaussian and laplacian pyramids are applying gaussian and laplacian filter 聽in an image in聽cascade order with different kernel sizes of gaussian and laplacian filter.

Figure. 1 shows pyramid of image.

Full image resolution is taken at level 0.

At each step up level image resolution is down sample by 2. So if starting image size is 256 X 256 at level 0 in level 1 image size will be 128 X 128.

Gaussian Pyramid :

Figure. 2 shows the Gaussian pyramid block diagram of input image.

Take input image 聽(in_image), 聽apply gaussian filter, output will be level 0 image.

Down sample the level o image by 2, apply gaussian filter, output will be level 1 image.

Down sample聽the level 1 image by 2, apply gaussian filter, output will be level 2 image.

Similar way we can create full pyramid of gaussian.

Every level use same Gaussin filter. (If you are down sampling the image by 2 at each level, size of gassuan filter should not be changed)

Effect of down sampling the image by 2 is equivalent to increasing the bandwidth of gaussian filter by 2.

聽Suppose gassuain filter cutoff frequency is F1 in 0 level if we down sample image by 2 gaussian filter cut of frequency will be 2*F1 for down sampled image.

So by down sampling the image, we are applying different sizes of gaussian in input image. That is also called multi resolution analysis.

Gaussian pyramids are used in SIFT, Surf, Gabor filters for multi resolution features extractor

Figure. 3 Gaussian pyramid results

Figure. 3 shows gaussian pyramid results. Level 2 image is much blurred as compared to level 0.

Laplacian Pyramid:聽

Suppose Level 0, level 1 and level 2 is the output of gaussian pyramid as we discussed above. Figure. 4 shows the block diagram of laplacian pyramid from gaussian pyramid.

Up sample the gaussian level 1 image 聽by 2 聽(level 1_2)and subtract the level 1_2 image from level 0 image, output will be laplacian of level 0.

Upsample the gaussian level 2 image 聽by 2聽(level 2_2) and subtract the level 2_2 image from level 1 gaussian image , output will be laplacian of level1.

Laplacian pyramid try to find out the pass band frequency.

Suppose gaussian level 0 frequency is f1 and gaussian level 1 frequency is f2, so laplacian level 0 frequency will be f2 – f1.

Laplacian pyramid used many time in object detection pre processing steps.

It can help to compute the optical flow for large motion vector.

In this post I will discuss the聽2 D FFT in term of image processing.

Steps we are going to perform:

Read input images and observe FFT of images.

Generate noisy image with random noise.

Analyze the FFT of聽Lena clean聽image and Lena聽noisy image.

Use Gaussian filters in frequency domain to clean image

Analysis of results.

Figure.1 shows the FFT of single color image and white and gray image FFT.

In single color image all pixel are same their is no variations in pixel color.

Frequency of image is almost zeros so we can see small dot at center.

That’s mean all frequency is concentrated near to zero.

In gray and white image color is changed in vertical direction (Y direction), FFT of image shows the frequency distributions in聽Y direction. No frequency changed in聽X direction.

If image is blur (means color variation in image is less) its FFT distribution will be concentrated. That’s help to find out amount of blur in image.

Random noise image have random pixel variation, that shows聽many random frequencies are present in noise image.

FFT of random noise image will be distributed聽in all direction without having any prominent frequency as shown in figure.2.

Figure.3 shows FFT of standard lena image we can see lot of information lie on center and some high prominent frequency content also present in image that makes image sharp.

In lena noisy image random noise is distributed in image but some information is their at center we can see some brightness at center聽.

If we take the filter聽which take out center frequency聽of image we can able to rid of random noise.

Let take the gaussian mask ( as shown in figure4) 聽and multiply it with聽image in frequency domain. (frequency domain multiplication is convolutions in special domain).

Inverse聽the image聽to special domain which is filtered image (shown in figure. 4).

Figure 5 shows the result of image filtering with many sizes of Gaussian mask.

Gaussian filter sigma is 5,聽image is too blurred and noise is removed.

When Gaussian sigma is 57, image聽is not cleaned.

So with the help of聽FFT we can find out the size and distribution of Gaussian filter according to our applications.

Some applications of FFT in image processing

聽Feature extractions.

Image Compression.

Filtering

Find the amount of blur in image.

Many time used to speed up the 2 d convolution,聽operation like erosion, dilation.

MATLAB code:

%%%%%%%%%%%%%%%%%%%%%%code start%%%%%%%%%%%%%

clc;聽聽聽聽聽聽聽 % clear聽 screen

clear all聽 % clear all variables

close all

img = imread(‘lena’,‘jpg’); % load the image

img_gray = rgb2gray(img);

img_gray = double(img_gray);聽聽 % convert image to double data type

[row,col,channel] = size(img);

noise = 255*rand(row,col);聽聽聽聽聽聽聽聽 % generate random noise

To start work in CV or IP we should have a input image聽to do some experiments on it. Go to google and type lena. You will get lena standard image like I added below.

Lena image

Some words about Miss Lena:

Lena is most famous image to perform compression, IP and CV.

Lena digitized picture聽was appear on playboy in 1972.

Lena Soderberg (ne Sj枚枚blom) was last reported living in her native Sweden, happily married with three kids and a job. if you want to more about lena go through following article Lena聽.

Steps we are going to perform:

Read the Lina image as input.

Generate random noise.

Add noise to input image.

Apply average filter and gaussian filter to clean the noisy image

Figure shows the mask used to filter image.

Random noise is random in nature so if we take the average of random noise its tends to cancel out each other.

In above average filter we assign the equal weight to all 25 pixel that is 1/25 .

In Gaussian filter instead of providing equal weight we distribute the weights in gaussian manner so in center max weight will be assign.

Result of聽Average filter and Gaussian filter on noisy image.