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)

























No comments:

Post a Comment