Professional Documents
Culture Documents
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
(2) Video
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))
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
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')
-bitwise addition (so that we can add image with full opaqueness)
screenshot
(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
(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))
(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)
(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)