You are on page 1of 4

(1)Image (It should be in working directory or we need to specify path)

cv2_imread()
cv2_imshow()
cv2_imwrite()

import numpy as np

import cv2
img = cv2_imread('filename.jpg',0)
// 1:colored image = cv2.IMREAD_COLOR
// 0:grayscaled image = cv2.IMREAD_GRAYSCALE
//-1:loads image as such including alpha
channel image = cv2.IMREAD_UNCHANGED
cv2.imshow('anyname',img)
k=cv2.waitKey(0) // if 0 or a ?is pressed it waits
indefinately for an event to happen
if k==27: // esc=27
cv2_destroyAllWindows() // if we want to destroy specific window,
destroyWindow(arg)
else if k==ord('s'): // waits for saving
cv2.imwrite('filename.png',img)
cv2.destroyAllWindows()

*If you are using a 64-bit machine, you will have to modify k = cv2.waitKey(0)
line as follows : k = cv2.waitKey(0) & 0xFF

generally image is getting saved to the exact dimensions of the image but what if
image is too large and you want to add trackbar for image size:

cv2.nameWindow('anyname',cv2.WINDOW_NORMAL)
cv2.WINDOW_NORMAL = we can change window size
cv2.WINDOW_AUTOSIZE = default size

Remaining METAPLOIT LIB

(2) Video

vid = cv2.VideoCapture(0) //0 or -1 for webcam, 1 for extenal

while(true): // vid.isOpened() = true


ret, frame = vid.read() // ret is return value based on frame
gray = cv2.cvtColor(frame,cv2.Color_BGR2GRAY)
cv2.imshow('anyname',gray)

if cv2.waitKey(1) & 0xFF == ord('q'):


break

vid.release()
cv2.destroyAllWindows()

*if you want to display video just change that 0 used for webcam to filename
(.avi)
*for saving video we have screenshots there

(3) shapes
add black(zeros) img
img=numpy.zeros((512,512,3),numpy.uint8)
(512 and 512 are x and y dimensions acc. to forth quadrant)
(numpy.uint8: unsigned int 8 bit(255), unsigned: color are always non negative
(>0))

Line: cv2.line(img,(x1,y1),(x2,y2),(B,G,R),h)
(x1,y1) = starting point coordinate,
(x2,y2) = ending point coordinate

Rectangle: cv2.rectangle(img,(x1,y1),(x2,y2),(B,G,R),h)
(x1,y1) = top left coordinate
(x2,y2) = bottom right coordinate

circle: cv2.circle(img,(x,y),r,(B,G,R),h)
(x,y) = center , r=radius

ellipse: cv2.ellipse(img,(x,y),(a,b),p,q,(B,G,R),h)
(x,y) = cneter, (a,b) = (major radius, minor radius) ,p and q starting
and ending angle
polygone: pts = np.array([x1,y1],[x2,y2],[x3,y3]],np.int32)
cv2.polylines(img,[pts],True,(B,G,R))

Add text: font=cv2.FONT_HERSHEY_SIMPLEX


cv2.putText(img,'text',(x1,y1),font,size of font,(B,G,R),h,line type)
(x1,y1) = starting point
line type = cv2.LINE_AA (optional)

(4) Mouse events

for printing mouse events:

events = [i for i in dir(cv2) if 'EVENT' in i]


print events

cv2.setMouseCallback('img',function(i.e. draw_circle))
def draw_circle(events,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
draw circle

(5) Trackbar

cv2.createTrackbar('name of trackbar','image that is attached with',default value,


max value, funtion)

in our function mostly trackbar returns the value of its position in other words it
simply passthem
and in order to get them,
i.e. r=cv2.getTrackbarPos('R','image')

so for g and b and pass that to


img[:]=[b,g,r]

(6) Edit pixels


img[x,y] = [B,G,R]
make red pixels zero //2 for red, 1 for green and 0 for blue
img[:,:,2] = 0
Adding / blending image (Remaining)
cv2.add()
-blending (adjust transparency)
cv2.addWeighted(img1,x,img2,y) //x+y=1

-bitwise addition (so that we can add image with full opaqueness)
screenshot

(7) performance measurement in python


clock and frequency measurement
bookmark*

(8) RGB to Gray and RBG to HSV (hue saturation value) (h=179,s=255,v=255)
used in object tracking and draw something using your hand?

'Hue' represents the color, 'saturation' represents the amount to which that
respective color is mixed with white and 'value' represents the amount to which
that respective color is mixed with black.

hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#detect blue object,
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
mask = cv2.inrange(hsv,lower_blue,upper_blue)
final = bitwise_and(frame,frame, mask = mask)
//ret, frame = cv2.VideoCapture(0)
//0 for webcam

how we are gonna find particular color for object?


i.e. for red
red = np.uint8([[[0,0,255]]])
hsv_red = cv2.cvtColor(red,cv2.COLOR_BGR2HSV)
print hsv_red
now lower and upper boundaries for particular color is
lower = (H-10,100,100) and upper = (H+10,255,255) //H is hue in HSV
(9) Transformation of Images
(a) Scaling:
*Shrinking = cv2.INTER_AREA
*zoom = cv2.INTER_CUBE or cv2.INTER_LINEAR
res = cv2.resize(img,(rows,cols),interpolation = cv2.INTER_*)

(b) Traslation:
M = np.float32([[1,0,fx],[0,1,fy]])
dst = cv2.warpAffine(img,M,(cols,rows))

(c) Rotation:
M = cv2.getRotationMatrix2D((cols,rows),alpha,scale)
alpha = angle we need to rotate
dst = cv2.warpAffine(img,M,(cols,rows))

(d) affineTransformation:
*Need three not colinear points
pts1 = np.float32([[x1,y1],[x2,y2],[x3,y3]])
pts2 = np.float32([[x1',y1'],[x2',y2'],[x3',y3']]) //shifted
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
(e) perspectiveTransformation:
*Instead of three four points to be taken
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(cols,rows))

(10) Simple, Adaptive and otsu's thresholding

(c) Otsu's thresholding ( histogram of image who has two peak,so we can take
center value)
cv2.threshold(img,0,255,function + cv2.thresh_otsu)

[LPF is used for smoothing and HPF is used for finding the edges]

for filtering:
blur = cv2.GaussianBlur(img,(k,k),0) //here (k,k) is width and height of
kernal) and 0 is standard deviation (if it is zero, it takes automatically from
kernal)
th = cv2.threshold(blur,0,255,function + cv2.thresh_otsu)

kernel = np.ones((5,5),np.uint8)

(11) blurring, smoothing or filtering


(a) averaging : takes avg of nearby and replace it
(b) gaussian: blur = cv2.GaussianBlur(img,(k,k),0)
(c) median = cv2.medianBlur(img,5)
(d) bilateral : blur = cv2.bilateralFilter(img,9,75,75)?

(12) erosion = cv2.erode(img,kernel,iteration = 1) //decrease the thickness of


letter
dilation = cv2.dilate(img,kernel,iteration = 1) //increase it

(13) Image gradient:


laplacian = cv2.Laplacian(img,datatype)
//datatype: cv2.CV_64F (cause taking 8U (uint8) gives only positive
part)

(14) edge
laplacian = cv2.Laplacian(img,cv2.CV_64F) //cv2.CV_8U is np.uint8 but
cv2.CV_64F is good for edge detection

(15)

image[100:100:2] = 255 //meaning at pixel position (100,100) make


red(2) to 255

You might also like