Friday, 9 December 2016

Python 2.7 OpenCV3 tutorial

Reading Images
To read an image as it is
            >>>import cv2
            >>> a=cv2.imread('D:/1.jpg')
                                    (or)
            >>> a=cv2.imread('D:/1.jpg',-1) #the default
                                    (or)
            >>> a=cv2.imread('D:/1.jpg',cv2.IMREAD_UNCHANGED)
To  show an image
            >>> cv2.imshow('window_name',a)
            >>>cv2.waitKey(0)
window_name can be any name u like. cv2.waitKey(milliseconds) returns ASCII value of the key pressed on the Keyboard
To destroy the window created through imshow
To destroy all windows,
            >>>cv2.destroyAllWindows()
To destroy a specific window only,
            >>>cv2.destroyWindow('window_name')
To check the dimensions of the image read
>>> import cv2
>>> image=cv2.imread('1.png')
>>> image.shape
(450, 300, 3)
#450 rows, 300 columns and 3 planes
>>> (rows,columns,channels)=image.shape

Converting an image into gray scale  





To find different flags available in opencv
To find the list of color conversion possible
            >>> flags= [i for i in dir(cv2) if i.startswith('COLOR_')]
            >>> print flags
To find the flags for events in opencv
            events=[i for i in dir(cv2) if 'EVENT' in i]
            print events
To find the different line flags available
            >>> flags= [i for i in dir(cv2) if i.startswith('LINE_')]
            >>> print flags
To find the different FONT flags available
            fonts=[i for i in dir(cv2) if 'FONT' in i]
            print fonts

To draw a Line 



To draw a rectangle 


 To Draw an Ellipse

                                                                               
To add text to images 



 To resize an image

  
To copy an image
image=cv2.imread(‘C:/1.jpg’)
copied=image.copy()

Convert image to binary image, based on threshold
>>> gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
>>> ret1,thresh1=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
>>> ret1
149.0 # threshold value decided by OTSU and all the pixels intensity above this threshold is made 255
#thresh1 is the binary image.
>>> ret,thresh=cv2.threshold(gray,50,255,cv2.THRESH_BINARY)
>>> ret
50.0# threshold value decided by u i.e, second argument
To find contours
_,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


To flip an image
rimg=cv2.flip(img,1) #vertical flip
fimg=cv2.flip(img,0) #horizontal flip
Image Arithmetic

Adding two images

added=cv2.add(img1,img2)

added=cv2.addWeighted(img1,0.7,img2,0.3,0)

Absolute difference of two images

difference= cv2.absdiff(img1,img2) 
                                                                               
Creating the negative of an binary image

binaryInvert=cv2.bitwise_not(binary_imag)

 Find the edges of an image using Canny
edges = cv2.Canny(image,100,200)

Gradient Filters

1. sobelx=cv2.Sobel(image,cv2.CU_64F,1,0,5)
   sobely=cv2.Sobel(image,cv2.CU_64F,0,1,5)

2. lap=cv2.Laplacian(image,cv2.CU_64F,0,1,5)

























Sunday, 23 October 2016

Creating GUI using guide in MATLAB to retreive images in a folder and display

function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 13-Dec-2015 00:03:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)

% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

first=get(handles.edit1,'string');
foldername=strcat('C:\',first,'\'); #PATH TO FOLDER IS IN RED COLOUR
imname=strcat(foldername,'\*.png');
n = length(imname);   
imgs=dir(imname);
filename=strcat(foldername,'\',imgs(1).name);
Cval = imread(filename);
handles.C = Cval;
axes(handles.axes1);
imshow(Cval);
handles.output = hObject;
guidata(hObject, handles);

filename2=strcat(foldername,'\',imgs(2).name);
Cval = imread(filename2);
handles.C = Cval;
axes(handles.axes2);
imshow(Cval);
handles.output = hObject;
guidata(hObject, handles);


All the code above in green colour is automatically generated by guide in MATLAB
U have to create a figure using guide in matlab and give it the name untitled.fig as the code has the name untitled.
Also you have to create a folder named actress i.e, same as the name of the folder u type in the search box in the below figure.


Output:

Monday, 17 October 2016

Preprocessing satellite images to find crowns of trees using MATLAB

MATLAB Program: 
clear all;
close all;
g=imread('test_area_1.tif');
l=[100/255 75/255 50/255];
h=[255/255 255/255 250/255];
b=imadjust(g,[l;h],[0;1]);
H = padarray(2,[2 2]) - fspecial('gaussian' ,[5 5],2); % create unsharp mask
d=imfilter(b,H);
subplot(131);imshow(g);title('
original');
subplot(132);imshow(b);title('Contrast stretched image');
subplot(133);imshow(d);title('sharpened image');


By contrast stretching the image using imadjust and by applying high pass filter for sharpening, the results are as shown below:








 

Friday, 29 April 2016

Histogram of Oriented Gradients (HoG)

1) First apply Gaussian Smoothing Mask on the image.
2) The given image is divided into 16x16 blocks each of 2x2 cell and each cell is 8x8.  Block1 is as shown in the above figure WITH 50% Overlap



If the image is of size 64x128, we get 105 blocks.

3) Compute gradient magnitude and gradient direction for each block.

4) We look at gradient direction and quantize orientation into any of  9 bins (0- 180 degrees) as shown below












5) If direction is not in one of the bins we use interpolation.
6) Concatenate all descriptors i.e, 105 blocks each of 9 dimensions as we have 9 bins.  Hence we get a total of 3780 descriptors .(each block is further divided into 2x2 cells each of size 8 so 2*2 =4 and for each 8x8 , 9 bins and hence 4*9*105=3780)
7)We plot histogram
x-axis of histogram is the bin values say (20 degrees, 40 degrees...as shown in the above figure).
y-axis is the count. We count how strong the gradient direction is using gradient magnitude( vote).

To Learn more about HoG watch the video at the link given below https://www.youtube.com/watch?v=0Zib1YEE4LU
To learn more about descriptors like SIFT etc.. follow the below link
https://gilscvblog.com/2013/08/26/tutorial-on-binary-descriptors-part-1/
https://www.youtube.com/watch?v=4ESLTAd3IOM




Thursday, 14 January 2016

Steps in Canny Edge Detector

Steps in Canny Edge Detector
1. Gaussian Smoothing
2.Find Derivatives of smoothed image
3.Magnitude and Orientation of gradient is found
4.Non-maximum Supression
Supresses the pixels that are not local maxima
if (gradient of a pixel is less than the gradient of its neighbouring pixels)
then  supress or make zero the intensities of that pixel.
else
maintain
5.Hysterisis Thresholding
If the gradient magnitude of a pixel is above hithres
  then it is edge pixel.

If the gradient magnitude of a pixel is below lowthres
  then it is not edge pixel.

If the gradient magnitude of a pixel is below hithres and above lowthres
  then check for connectedness.
      If connectedness of that pixel is high
       then it is edge pixel
      else
       not edge pixel

You can understand it better if u watch the below video
https://www.youtube.com/watch?v=lC-IrZsdTrw

Viola & Jones Object Detection Algorithm well Explained with simple example



Viola & Jones Object Detection Algorithm

            Viola Jones Face Detection Algorithm [1] uses Haar features as shown in Figure 1. The steps in the algorithm are
  1. Integral Image Calculation
  2. Feature Computation
  3. Adaboost Feature Selection
  4. Classifier Cascade

            All the haar features (shown in Figure 1) in different scales are used to produce approximately 1,80,000 features. Viola Jones uses 24x24 window as the base window size to evaluate the haar features.

 Figure 1. Haar Features Used in Viola Jones [1]
1. Integral image Calculation
            Consider an image with pixel intensities as shown in Figure 1.1

5
2
5
2
3
6
3
6
5
2
5
2
3
6
3
6

Figure 1.1 Original Image

Integral Image of the above image is calculated as follows
            For each pixel, we draw a line as follows. All the pixel intensities above the line must be added to get the integral image.
5
2
3
6



 The value of first pixel remains the same. 


5
2
3
6
  


The value of first row second column value changes from 2 to 7 

5
2
3
6
                                                                           
So, in place of 6 we get 6+5+2+3 = 16
            We calculate like this for all the pixels in the image and the resultant image is called integral image.
2. Feature Computation
            A haar classifier as shown in Figure 1.2 is run on the image and we sum all the pixels under black region and subtract from sum of all pixels under white region. If the difference is above some threshold, the feature matches. This computation will be easy, if we calculate the integral image.





  Figure 1.2 Haar Classifier
5
7
12
14
8
16
24
62
13
23
36
46
16
32
48
64

Figure 1.3 Haar Classifer When Run On Integral Image
            Sum of pixels  under black region =  5+32-(7+16)=14 (same as 6+2+6 =14 in given image). Sum of pixels  under white region =  7+48-(12+32)=11 (same as 3+5+3 in given image)
3. AdaBoost
            Adaboost is used to train Strong Classifier which is linear combination of weak classifier. It also decides whether a feature is relevant or not. The steps in Adaboost are:
  1. Training set of positive and negative examples (Ex: faces and non-faces images).
  2. Initially all the positive training images are given weights equal to  and negative training images are given weights equal to .
  3. All the 1,80,000 Haar features or weak  classifiers are run on the training images
  4. A good  threshold (for ex: decision tree)  such that any image above  threshold is face and below threshold is non-face is determined.
  5. Now, Error rate is calculated as sum of weights of images misclassified by each weak classifier. Of the 1,80,000 error rates choose the weak classifier with lowest error rate.
            The chosen weak classifier is added to the strong classifier. Now, increase the weights of misclassified images and decrease the weights of correctly classified by normalizing the weights. Again repeat the  steps 3 to 5  for 1,80,000 times and all the Haar features are run on the images with updated weights and each round selects one weak classifier, which is added as linear combination to obtain final Strong Classifier. The output of weak classifier is 1 or 0 for classifying the image as face or non face.
4. Cascading Of Stages
            After all the rounds of Adaboost, we build a strong classifier which is a linear combination of selected weak classifiers (let’s say, 2,000). Instead of running all the 2,000 weak classifiers on the 24x24 window of test image, we build a cascade of classifiers. This will reduce computation cost as Stage1 immediately rejects windows that are non-faces.
Figure 1.4. Cascade of Stages to Reject Non-Face Windows Immediately [1]
To train a cascade, we must choose
  • Number of stages or Strong classifiers in cascade
  • Number of weak classifiers in strong Classifier (which is done by Adaboost)
For this we do Manual Tweaking, which is a heuristic algorithm to train the cascade
  1. Select Maximum Acceptable False Positive rate.
  2. Select Minimum Acceptable True Positive rate.
  3. Threshold for each Strong Classifier (which is decided by Adaboost)
Let the User select the Target Overall False Positive for all the stages
Until Target Overall False Positive is met
    Add new Stage
            Until Maximum Acceptable False Positive rate and Minimum Acceptable
            True Positive rate are met   
                           Keep adding weak classifiers and train Strong Classifier using Adaboost.
You can listen to the video at below link 
https://www.youtube.com/watch?v=WfdYYNamHZ8
References
[1] Viola, Paul, and Michael Jones. "Rapid object detection using a boosted cascade of simple features." Computer Vision and Pattern Recognition, 2001. CVPR 2001. Proceedings of the 2001 IEEE Computer Society Conference on. Vol. 1. IEEE, 2001.