美文网首页
python+opencv基础图像处理

python+opencv基础图像处理

作者: Reyuwei | 来源:发表于2019-06-19 14:54 被阅读0次
    1. import
    import cv2
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    1. read
    img = cv2.imread(imagename,cv2.IMREAD_COLOR)
    // cv2.IMREAD_COLOR - load in bgr color mode
    // cv2.IMREAD_GRAY - load in grayscale mode
    // cv2.IMREAD_UNCHANGED - load original image (with alpha channel)
    
    1. convert and resize
    img = cv2.cvtColor(semantic_mask, cv2.COLOR_BGR2RGB)
    h,w, _ = img.shape
    img_resize = cv2.resize(img, (w,h), interpolation=cv2.INTER_CUBIC)
    
    1. show
    plt.figure()
    plt.imshow(img)
    
    img_gray= cv2.cvtColor(semantic_mask, cv2.COLOR_RGB2GRAY)// convert to gray image
    plt.figure()
    plt.imshow(img_gray,cmap='gray')
    
    1. inRange
    color_lbound = np.array([2,100,200]) -20
    color_hbound = np.array([2,100,200])+20
    mask = cv2.inRange(semantic_mask, color_lbound, color_hbound)
    image[mask>0] = new_color
    
    1. threshold and bit operation
    ret, mask = cv2.threshold(img_gray, 1, 200, cv2.THRESH_BINARY)
    mask_inv = cv2.bitwise_not(mask)
        
    img_bg = cv2.bitwise_and(bg,bg,mask = mask_inv)
    img_fg = cv2.bitwise_and(fg,fg,mask = mask)
    

    相关文章

      网友评论

          本文标题:python+opencv基础图像处理

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