美文网首页
openCV API

openCV API

作者: MWhite | 来源:发表于2019-01-14 15:12 被阅读0次

    下面列出的是api,单纯线性执行以下代码可能会跑不通

    环境安装

    • conda create -n XXX(新建一个conda环境)
    • Activate XXX (打开conda的某一个环境)
    • pip install opencv-python (安装opencv-python)

    基本操作

    import cv2
    pic= cv2.imread('filename.png',0)  # 0为只读灰度,1为读BGR图像
    cv2.imshow('xxx',pic) # 在xxx窗口显示pic图片
    cv2.watKey(0)
    cv2.destoryAllWindows()
    cv2.imwrite('filename.png',pic)
    

    视频

    cap = cv2.VideoCapture('vtest.avi') 
    while(cap.isOpened()):
        ret, frame = cap.read() # 读一帧
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)        
            cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q')  :
             break
    cap.release()
    cv2.destroyAllWindows()
    
    # 获取摄像头
    cap = cv2.VideoCapture(-1) #摄像头编号。
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')#  注意编码器
    out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
    

    绘图

    # Draw a diagonal blue line with thickness of 5 px
    cv2.line(img,(0,0),(511,511),(255,0,0),5)
    cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
    cv2.circle(img,(447,63), 63, (0,0,255), -1)
    cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
    
    # 多边形
    pts=np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
    pts=pts.reshape((-1,1,2))
    cv2.polylines(img,[pts],True,(0,255,255))
    
    # 文字
    font=cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),6) 
    

    鼠标

    # 对某个窗口应用鼠标事件函数
    cv2.namedWindow('image')
    cv2.setMouseCallback('image',draw_circle)
    

    图像代数运算

    cv2.add()
    cv2.subtract(img,80)
    cv2.multiply(img,1.5)
    cv2.addWeighted(img1,0.7,img2,0.3,0)
    
    img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) # 图片img2转换为灰度值
    ret, mask_front = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY) # 门限 灰度值大于175为1(白) 小于175为0
    mask_inv = cv2.bitwise_not(mask_front) # 取反
    img1_bg = cv2.bitwise_and(img1,img1,mask = mask_front) # 图片1扣背景
    img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv) # 图片2扣内容(背景为白色,内容灰度值较低)
    result = cv2.add(img1_bg,img2_fg) # 合并
    

    几何变换

    res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
    
    M=cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6) # 获得对应的旋转矩阵
    dst=cv2.warpAffine(img,M,(cols,rows)) # 仿射
    
    # 仿射
    pts1=np.float32([[50,50],[200,50],[50,200]])
    pts2=np.float32([[10,100],[200,50],[100,250]])
    M=cv2.getAffineTransform(pts1,pts2)
    
    # 透视
    pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
    pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
    M=cv2.getPerspectiveTransform(pts1,pts2)
    

    直方图

    cv2:calcHist(images; channels; mask; histSize; ranges[; hist[; accumulate]])
    hist = cv2.calcHist([img],[0],None,[256],[0,256])
    hist,bins = np.histogram(img.ravel(),256,[0,256])
    
    # 均衡化灰度图
    equ = cv2.equalizeHist(img)
    

    卷积

    # 计算卷积
    dst = cv2.filter2D(img,-1,kernel)
    
    blur3 = cv2.blur(img,(3,3)) # 均值
    median = cv2.medianBlur(img,5) # 中值
    blur = cv2.GaussianBlur(img,(5,5),0) # 高斯
    laplacian=cv2.Laplacian(img,-1) # 拉普拉斯
    sobelx=cv2.Sobel(img,-1,1,0,ksize=5) #sobel x方向1 y方向0
    

    复变

    f = np.fft.fft2(img)
    fshift = np.fft.fftshift(f)
    
    magnitude_spectrum = 20*np.log(np.abs(fshift)) # 进行对数处理,可视化
    
    f_ishift = np.fft.ifftshift(fshift)
    img_back = np.fft.ifft2(f_ishift)
    

    形态学

    kernel = np.ones((5,5),np.uint8) 
    #kernel  = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
    erosion = cv2.erode(img,kernel,iterations = 1) # 腐蚀
    dilation = cv2.dilate(img,kernel,iterations = 1) # 膨胀
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # 开
    closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) # 闭
    gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) # 膨胀-腐蚀 result = cv2.absdiff(dilate,erode)
    

    形态学-阈值

    ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
    # 自适应阈值
    th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
    th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
    
    
    cv2.cvtColor(input_image,flag) # 色彩转换
    mask=cv2.inRange(hsv,lower_blue,upper_blue)  #构建掩模
    

    相关文章

      网友评论

          本文标题:openCV API

          本文链接:https://www.haomeiwen.com/subject/tvbddqtx.html