python-opencv顏色分析小工具

a819411321發表於2020-12-29

效果
在這裡插入圖片描述最左邊是一個控制元件欄用於修改3個通道的畫素值。
imshow_img視窗顯示5張圖,從左到右依次是原圖,紅色通道圖,綠色通道圖,藍色通道圖,合成圖。
switch,是選擇將三個通道的畫素值的圖按或或者與進行合併。
程式碼如下。

import cv2
import glob
import numpy as np

def nothing(x):
    pass
    
#圖片資料夾路徑 結尾必須是/或者\\
leaf_path='../學習用煙/oil/'
img_list=glob.glob(leaf_path+'*.jpg')


cv2.namedWindow('Tunning')
cv2.resizeWindow('Tunning', 100, 100)
cv2.createTrackbar('index','Tunning',0,len(img_list)-1,nothing)
cv2.createTrackbar('r_value','Tunning',100,240,nothing)
cv2.createTrackbar('r_value2','Tunning',110,255,nothing)
cv2.createTrackbar('g_value','Tunning',100,240,nothing)
cv2.createTrackbar('g_value2','Tunning',110,255,nothing)
cv2.createTrackbar('b_value','Tunning',100,240,nothing)
cv2.createTrackbar('b_value2','Tunning',110,255,nothing)
cv2.createTrackbar('swich','Tunning',0,1,nothing)
cv2.namedWindow('inshow_img')
cv2.resizeWindow('inshow_img', 1500, 600)
while True:
    index = cv2.getTrackbarPos('index', 'Tunning')
    r_value=cv2.getTrackbarPos('r_value', 'Tunning')
    g_value = cv2.getTrackbarPos('g_value', 'Tunning')
    b_value = cv2.getTrackbarPos('b_value', 'Tunning')
    r_value2=cv2.getTrackbarPos('r_value2', 'Tunning')
    g_value2 = cv2.getTrackbarPos('g_value2', 'Tunning')
    b_value2 = cv2.getTrackbarPos('b_value2', 'Tunning')
    choice_value = cv2.getTrackbarPos('swich', 'Tunning')
    if (index != -1):
        img = cv2.imread(img_list[index])
        if b_value<b_value2:
            use_b_value2=b_value2
        else:
            use_b_value2 = b_value+10
        if r_value<r_value2:
            use_r_value2=r_value2
        else:
            use_r_value2 = r_value+10
        if g_value<g_value2:
            use_g_value2=g_value2
        else:
            use_g_value2 = g_value+10
        b_img = cv2.inRange(img[:, :, 0], b_value, use_b_value2)
        g_img = cv2.inRange(img[:, :, 1], g_value, use_g_value2)
        r_img = cv2.inRange(img[:, :, 2], r_value, use_r_value2)
        b_imshow = cv2.cvtColor(b_img, cv2.COLOR_GRAY2BGR)
        g_imshow = cv2.cvtColor(g_img, cv2.COLOR_GRAY2BGR)
        r_imshow = cv2.cvtColor(r_img, cv2.COLOR_GRAY2BGR)
        temp1 = np.zeros(b_img.shape, np.uint8)
        temp2 = temp1.copy()
        if choice_value==0:
            cv2.bitwise_or(b_img,g_img,temp1)
            cv2.bitwise_or(temp1,r_img,temp2)
        else:
            cv2.bitwise_and(b_img, g_img, temp1)
            cv2.bitwise_and(temp1, r_img, temp2)
        temp2_imshow = cv2.cvtColor(temp2, cv2.COLOR_GRAY2BGR)
        imshow_green_hstack = np.zeros([img.shape[0], 10, img.shape[2]], np.uint8)
        imshow_green_hstack[:,:,1]=255
        imshow_img = np.hstack((img, imshow_green_hstack, b_imshow, imshow_green_hstack,
                                g_imshow,imshow_green_hstack, r_imshow,imshow_green_hstack,temp2_imshow))
        imshow_all_resize = cv2.resize(imshow_img, (1600, 600))
        cv2.imshow('inshow_img', imshow_all_resize)
        c = cv2.waitKey(1)
        if c == ord('q'):
            break

相關文章