<button id="ssm6u"><optgroup id="ssm6u"></optgroup></button>
  • 跳轉至

    二值化可視化調參

    概要

    本文講解了如何在RGB色彩空間,HSV色彩空間通過HighGUI的組件進行可視化調參.

    keywords RGB HSV 色彩空間 可視化調參

    例程1:RGB色彩空間GUI可視化調參

    上面介紹的方法, 使用起來還有諸多限制, 調參也不太方便. 既然我們學了HighGUI 組件, 我們就可以制作好用的調參軟件.

    Screenshot_20180204_151945.png

    注意這種方法也有對應的弊端.

    取決于背景與其他物體的顏色對比. 所以阿凱的建議是, 根據ROI顏色統計作為基準, 然后微調滑塊.

    threshold_editor_gui_rgb.py

    # -*- coding: utf-8 -*- 
    '''
        可視化顏色閾值調參軟件
    '''
    
    import cv2
    import numpy as np
    
    # 樣例圖片
    img = cv2.imread("screenshot.png")
    
    # 顏色閾值 Upper
    upperb = None
    # 顏色閾值 Lower
    lowerb = None
    
    # 更新MASK圖像,并且刷新windows
    def updateMask():
        global img
        global lowerb
        global upperb
    
        # 計算MASK
        mask = cv2.inRange(img, lowerb, upperb)
    
        cv2.imshow('mask', mask)
    
    # 更新閾值
    def updateThreshold(x):
    
        global lowerb
        global upperb
    
        minR = cv2.getTrackbarPos('minR','image')
        maxR = cv2.getTrackbarPos('maxR','image')
        minG = cv2.getTrackbarPos('minG','image')
        maxG = cv2.getTrackbarPos('maxG', 'image')
        minB = cv2.getTrackbarPos('minB', 'image')
        maxB = cv2.getTrackbarPos('maxB', 'image')
    
        lowerb = np.int32([minB, minG, minR])
        upperb = np.int32([maxB, maxG, maxR])
    
        print('更新閾值')
        print(lowerb)
        print(upperb)
        updateMask()
    
    cv2.namedWindow('image', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)
    # cv2.namedWindow('image')
    cv2.imshow('image', img)
    
    # cv2.namedWindow('mask')
    cv2.namedWindow('mask', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)
    
    # 紅色閾值 Bar
    ## 紅色閾值下界
    cv2.createTrackbar('minR','image',0,255,updateThreshold)
    ## 紅色閾值上界
    cv2.createTrackbar('maxR','image',0,255,updateThreshold)
    ## 設定紅色閾值上界滑條的值為255
    cv2.setTrackbarPos('maxR', 'image', 255)
    # 綠色閾值 Bar
    cv2.createTrackbar('minG','image',0,255,updateThreshold)
    cv2.createTrackbar('maxG','image',0,255,updateThreshold)
    cv2.setTrackbarPos('maxG', 'image', 255)
    # 藍色閾值 Bar
    cv2.createTrackbar('minB','image',0,255,updateThreshold)
    cv2.createTrackbar('maxB','image',0,255,updateThreshold)
    cv2.setTrackbarPos('maxB', 'image', 255)
    
    # 首次初始化窗口的色塊
    # 后面的更新 都是由getTrackbarPos產生變化而觸發
    updateThreshold(None)
    
    print("調試棋子的顏色閾值, 鍵盤摁e退出程序")
    while cv2.waitKey(0) != ord('e'):
        continue
    
    cv2.destroyAllWindows()
    

    例程2:RGB色彩空間GUI可視化調參

    python3 threshold_editor_gui_hsv.py tiaoyitiao.png
    

    threshold_editor_gui_hsv.py

    # -*- coding: utf-8 -*- 
    '''
        可視化顏色閾值調參軟件
    '''
    
    import cv2
    import numpy as np
    import sys
    
    # 更新MASK圖像,并且刷新windows
    def updateMask():
        global img
        global lowerb
        global upperb
        global mask
        # 計算MASK
        mask = cv2.inRange(img_hsv, lowerb, upperb)
    
        cv2.imshow('mask', mask)
    
    # 更新閾值
    def updateThreshold(x):
    
        global lowerb
        global upperb
    
        minH = cv2.getTrackbarPos('minH','image')
        maxH = cv2.getTrackbarPos('maxH','image')
        minS = cv2.getTrackbarPos('minS','image')
        maxS = cv2.getTrackbarPos('maxS', 'image')
        minV = cv2.getTrackbarPos('minV', 'image')
        maxV = cv2.getTrackbarPos('maxV', 'image')
    
        lowerb = np.int32([minH, minS, minV])
        upperb = np.int32([maxH, maxS, maxV])
    
        print('更新閾值')
        print(lowerb)
        print(upperb)
        updateMask()
    
    def main(img):
        global img_hsv
        global upperb
        global lowerb
        global mask
        # 將圖片轉換為HSV格式
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
        # 顏色閾值 Upper
        upperb = None
        # 顏色閾值 Lower
        lowerb = None
    
        mask = None
    
        cv2.namedWindow('image', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)
        # cv2.namedWindow('image')
        cv2.imshow('image', img)
    
        # cv2.namedWindow('mask')
        cv2.namedWindow('mask', flags= cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)
    
        # 紅色閾值 Bar
        ## 紅色閾值下界
        cv2.createTrackbar('minH','image',0,255,updateThreshold)
        ## 紅色閾值上界
        cv2.createTrackbar('maxH','image',0,255,updateThreshold)
        ## 設定紅色閾值上界滑條的值為255
        cv2.setTrackbarPos('maxH', 'image', 255)
        cv2.setTrackbarPos('minH', 'image', 0)
        # 綠色閾值 Bar
        cv2.createTrackbar('minS','image',0,255,updateThreshold)
        cv2.createTrackbar('maxS','image',0,255,updateThreshold)
        cv2.setTrackbarPos('maxS', 'image', 255)
        cv2.setTrackbarPos('minS', 'image', 0)
        # 藍色閾值 Bar
        cv2.createTrackbar('minV','image',0,255,updateThreshold)
        cv2.createTrackbar('maxV','image',0,255,updateThreshold)
        cv2.setTrackbarPos('maxV', 'image', 255)
        cv2.setTrackbarPos('minV', 'image', 0)
    
        # 首次初始化窗口的色塊
        # 后面的更新 都是由getTrackbarPos產生變化而觸發
        updateThreshold(None)
    
        print("調試棋子的顏色閾值, 鍵盤摁e退出程序")
        while cv2.waitKey(0) != ord('e'):
            continue
    
        cv2.imwrite('tmp_bin.png', mask)
        cv2.destroyAllWindows()
    
    if __name__ == "__main__":
        # 樣例圖片 (從命令行中填入)
        image_path = sys.argv[1]
        # 樣例圖片 (在代碼中填入)
        # img = cv2.imread('cfs_samples.jpg')
        img = cv2.imread(image_path)
        if img is None:
            print("Error: 文件路徑錯誤,沒有此圖片 {}".format(image_path))
            exit(1)
    
        main(img)
    


    韩国精品无码一区二区三区,精品无码一区二区三区AV,欧洲丰满美熟女乱又伦AV,亚洲午夜久久久影院伊人