圖像處理顏色閾值(藍(lán)色分割)
HSV代碼
import CV2
import numpy as np
def on_trackbar(val):
? ? pass
# 創(chuàng)建滑動條窗口
CV2.namedWindow("Threshold Adjustments")
# 讀取原圖
image = CV2.imread("C:/Users/0.jpg")
# 獲得圖像的尺寸
height, width, _ = image.shape
# 將圖像轉(zhuǎn)換為HSV顏色空間
hsv_image = CV2.cvtColor(image, CV2.COLOR_BGR2HSV)
# 設(shè)定初始藍(lán)色的閾值范圍
init_lower_blue = [0, 0, 0]
init_upper_blue = [255, 255, 255]
# 創(chuàng)建滑動條,設(shè)置回調(diào)函數(shù)
CV2.createTrackbar("Lower H", "Threshold Adjustments", init_lower_blue[0], 179, on_trackbar)
CV2.createTrackbar("Lower S", "Threshold Adjustments", init_lower_blue[1], 255, on_trackbar)
CV2.createTrackbar("Lower V", "Threshold Adjustments", init_lower_blue[2], 255, on_trackbar)
CV2.createTrackbar("Upper H", "Threshold Adjustments", init_upper_blue[0], 179, on_trackbar)
CV2.createTrackbar("Upper S", "Threshold Adjustments", init_upper_blue[1], 255, on_trackbar)
CV2.createTrackbar("Upper V", "Threshold Adjustments", init_upper_blue[2], 255, on_trackbar)
while True:
? ? # 獲取滑動條的當(dāng)前值
? ? lower_hue = CV2.getTrackbarPos("Lower H", "Threshold Adjustments")
? ? lower_saturation = CV2.getTrackbarPos("Lower S", "Threshold Adjustments")
? ? lower_value = CV2.getTrackbarPos("Lower V", "Threshold Adjustments")
? ? upper_hue = CV2.getTrackbarPos("Upper H", "Threshold Adjustments")
? ? upper_saturation = CV2.getTrackbarPos("Upper S", "Threshold Adjustments")
? ? upper_value = CV2.getTrackbarPos("Upper V", "Threshold Adjustments")
? ? # 設(shè)定藍(lán)色的閾值范圍
? ? lower_blue = np.array([lower_hue, lower_saturation, lower_value])
? ? upper_blue = np.array([upper_hue, upper_saturation, upper_value])
? ? # 根據(jù)閾值范圍分割圖像
? ? mask = CV2.inRange(hsv_image, lower_blue, upper_blue)
? ? result = CV2.bitwise_and(image, image, mask=mask)
? ? # 顯示原圖和結(jié)果圖像
? ? CV2.imshow("Original Image", image)
? ? CV2.imshow("Result Image", result)
? ? # 按下ESC鍵退出循環(huán)
? ? key = CV2.waitKey(1) & 0xFF
? ? if key == 27:
? ? ? ? break
# 關(guān)閉窗口
CV2.destroyAllWindows()
Lab代碼
import CV2
import numpy as np
def on_trackbar(val):
? ? pass
# 創(chuàng)建滑動條窗口
CV2.namedWindow("Threshold Adjustments")
# 讀取原圖
image = CV2.imread("C:/Users/0.jpg")
# 獲得圖像的尺寸
height, width, _ = image.shape
# 將圖像轉(zhuǎn)換為LAB顏色空間
lab_image = CV2.cvtColor(image, CV2.COLOR_BGR2LAB)
# 設(shè)定初始藍(lán)色的閾值范圍
init_lower_blue = [0, 0, 0]
init_upper_blue = [255, 255, 255]
# 創(chuàng)建滑動條,設(shè)置回調(diào)函數(shù)
CV2.createTrackbar("Lower L", "Threshold Adjustments", init_lower_blue[0], 255, on_trackbar)
CV2.createTrackbar("Lower A", "Threshold Adjustments", init_lower_blue[1], 255, on_trackbar)
CV2.createTrackbar("Lower B", "Threshold Adjustments", init_lower_blue[2], 255, on_trackbar)
CV2.createTrackbar("Upper L", "Threshold Adjustments", init_upper_blue[0], 255, on_trackbar)
CV2.createTrackbar("Upper A", "Threshold Adjustments", init_upper_blue[1], 255, on_trackbar)
CV2.createTrackbar("Upper B", "Threshold Adjustments", init_upper_blue[2], 255, on_trackbar)
while True:
? ? # 獲取滑動條的當(dāng)前值
? ? lower_L = CV2.getTrackbarPos("Lower L", "Threshold Adjustments")
? ? lower_A = CV2.getTrackbarPos("Lower A", "Threshold Adjustments")
? ? lower_B = CV2.getTrackbarPos("Lower B", "Threshold Adjustments")
? ? upper_L = CV2.getTrackbarPos("Upper L", "Threshold Adjustments")
? ? upper_A = CV2.getTrackbarPos("Upper A", "Threshold Adjustments")
? ? upper_B = CV2.getTrackbarPos("Upper B", "Threshold Adjustments")
? ? # 設(shè)定藍(lán)色的閾值范圍
? ? lower_blue = np.array([lower_L, lower_A, lower_B])
? ? upper_blue = np.array([upper_L, upper_A, upper_B])
? ? # 根據(jù)閾值范圍分割圖像
? ? mask = CV2.inRange(lab_image, lower_blue, upper_blue)
? ? result = CV2.bitwise_and(image, image, mask=mask)
? ? # 顯示原圖和結(jié)果圖像
? ? CV2.imshow("Original Image", image)
? ? CV2.imshow("Result Image", result)
? ? # 按下ESC鍵退出循環(huán)
? ? key = CV2.waitKey(1) & 0xFF
? ? if key == 27:
? ? ? ? break
# 關(guān)閉窗口
CV2.destroyAllWindows()