opencv常用api
一、图像及视频读写
img=cv2.imread('jiang.jpg',1) cv2.imwrite('jiang.jpg',img) cv2.imshow('jiang',img) cap=cv2.VideoCapture(0) ret,frame=cap.read()
cap.isOpened() cap.open() width,hight=cap.get(3),cap.get(4) cap.set(propId,value) gray=cv2.ctvColor(img,color_BGR2GRAY)
cap=cv2.VideoCapture('hello.avi') cv2.waitKey(25) fourcc=cv2.VideoWrier_fourcc(*'MJPG') out=cv2.VideoWriter('jiang.avi',fourcc,25.0,(640,480))
out.write(frame)
二、窗口操作相关:
cv2.namedWindow('jiang',[cv2.WINDOW_NORMAL,cv2.WINDOW_AUTOSIZE]) k=cv2.waitKey(0)&0xFF cv2.destroyWindow('jiang') cv2.destroyAllWindows()
cv2.setMouseCallback('windowname',callfunc)
三、绘图api
cv2.line(img,(0,0),(511,511),(0,255,0),5) cv2.rectangle() cv2.circle() cv2.ellipse() cv2.putText()
四、图像属性
img.shape() img.size() img.dtype() img.item(10,20,2)
五、图像加\二值化\位运算
dst=cv2.addWeighted(img1,0.7,img2,0.3,0) ret, mask = cv2.threshold(grayImage, 175, 255, cv2.THRESH_BINARY) cv2.bitwise_and()
六、拆分合并通道
b,g,r=cv2.split(img) img=cv2.merge([b,g,r])
七、程序运算效率相关
e1 = cv2.getTickCount() e2 = cv2.getTickCount() time = (e2 - e1)/ cv2.getTickFrequency()
八、按颜色选取图像区域
lower_blue=np.array([110,50,50]) upper_blue=np.array([130,255,255]) mask=cv2.inRange(hsv,lower_blue,upper_blue)
九、图像几何变换
移位:M=np.float32([[1,0,50][0,1,100]]) newPic = cv2.warpAffine(img,M,(cols,rows)) cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)
interpolation:['INTER_AREA', 'INTER_LINEAR' , 'INTER_CUBIC']
旋转:M = cv2.getRotationMatrix2D((rows/2,cols/2),45,0.6) newPic = cv2.warpAffine(img,M,(cols,rows))
仿射变换:M = cv2.getAffineTransform(pt1,pt2) newPic = cv2.warpAffine(img,M,(2*cols,2*rows))
newPic=cv2.resize(newPic,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_CUBIC)
透视变换:M=cv2.getPerspectiveTransform(pts1,pts2) dst=cv2.warpPerspective(img,M,(300,300))
pts1:原图上四个点,任意三点不能共线