08448380779 Call Girls In Friends Colony Women Seeking Men
Basic Python2.7 and open cv3 Cheatsheet
1. 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
gray=cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
COLOR_RGB2 YCrCb
'COLOR_BGR2RGB'
flags
2. 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
Starting Co-
ordinate value
Starting Co-
ordinate value
Ending Co-
ordinate value
Colour of the
line in B,G,R
Thickness of line
Line style /flag
cv2.line(image,(0,0),(100,100),(255,0,0),5,cv2.LINE_AA)
image
Top-left Co-
ordinate value
bottom right Co-
ordinate value
Colour of the
line in B,G,R
Thickness of line
cv2.rectangle(image,(0,0),(100,100),(255,0,0),5)
image
center Co-
ordinate value
major axis, minor
axis length
Colour of the
line in B,G,R
Angle of rotation in anti
clockwise direction
cv2.ellipse(image,(0,0),(100,100),(255,0,0),0)
3. To add text to images
To resize an image
resized = cv2.resize(image,(rows_in_resized, columns_in_resized), cv2.INTER_LINEAR)
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)
Text to be written
on image
Position on image
where text to be
inserted
Thickness of line
FONT flag
cv2.putText(img, 'Text to be added ',(100,100),cv2.FONT_ITALIC,4,(255,255,255),2,cv2.LINE_AA)
image
Line Type flag
Font
Size
Font
Colour
cv2.INTER_AREA
cv2.INTER_CUBIC
Interp
olatio
n flag
4. 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
Visit my blog:
enthusiaststudent.blogspot.in
Keywords:
cv2.imread, cv2.imshow, cv2.waitKey, cv2.destroyAllWindows, cv2.cvtColor, cv2.destroyWindow, cv2.line,
cv2.rectangle, cv2.ellipse, cv2.putText, cv2.resize, cv2.threshold, cv2.THRESH_BINARY,
cv2.THRESH_OTSU, cv2.findContours, cv2.flip, cv2.add, cv2.absdiff, cv2.bitwise_not, cv2.Canny