opencv HSV颜色空间中的颜色校准

m2xkgtsf  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(139)

目前我正在使用OpenCV ColorCorrectionModel来校准我的照片的颜色。
但是它似乎只支持RGB颜色空间。我希望使用HSV,因为我将把这些图像输入深度学习模型,而颜色是对这些图像进行分类的一个重要标准。
是否有任何现有的库来计算HSV空间中的颜色校正模型?
这些照片是用24色色块色卡拍摄的。

eiee3dmh

eiee3dmh1#

考虑到RGB范围输入,下面的代码输出检测到颜色的HSV范围。我希望这是你正在寻找的:

highhsv = [0,0,255]
lowhsv = [0,0,0]
sct = mss()

while(1):
    # Get current HSV values
    r1 = cv2.getTrackbarPos('R High','Colour Adjustment')
    g1 = cv2.getTrackbarPos('G High','Colour Adjustment')
    b1 = cv2.getTrackbarPos('B High','Colour Adjustment')
    r2 = cv2.getTrackbarPos('R Low','Colour Adjustment')
    g2 = cv2.getTrackbarPos('G Low','Colour Adjustment')
    b2 = cv2.getTrackbarPos('B Low','Colour Adjustment')

    # Get current capture area
    x = cv2.getTrackbarPos('X','Capture Area')
    y = cv2.getTrackbarPos('Y','Capture Area')
    width = cv2.getTrackbarPos('Width','Capture Area')
    height = cv2.getTrackbarPos('Height','Capture Area')

    # Change color bar on changing trackbar (high or low)
    if or1 != r1 or og1 != g1 or ob1 != b1:
        or1 = r1; og1 = g1; ob1 = b1
        colour[:] = [b1,g1,r1]
        highhsv = cv2.cvtColor(colour,cv2.COLOR_BGR2HSV)
        highhsv = [int(highhsv[0][0][0]),int(highhsv[0][0][1]),int(highhsv[0][0][2])]
    elif or2 != r2 or og2 != g2 or ob2 != b2:
        or2 = r2; og2 = g2; ob2 = b2
        colour[:] = [b2,g2,r2]
        lowhsv = cv2.cvtColor(colour,cv2.COLOR_BGR2HSV)
        lowhsv = [int(lowhsv[0][0][0]),int(lowhsv[0][0][1]),int(lowhsv[0][0][2])]
    cv2.imshow('Colour Adjustment',colour)
    # Capture screen
    area = {'top': y, 'left': x, 'width': width + 300, 'height': height + 250}
    sct.get_pixels(area)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    img_np = np.array(img)

    # Convert and find colours from captured images
    hsv = cv2.cvtColor(img_np, cv2.COLOR_BGR2HSV)
    rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
    higher = np.array(highhsv)
    lower = np.array(lowhsv)
    mask = cv2.inRange(hsv,lower,higher)
    res = cv2.bitwise_and(rgb,rgb,mask=mask)

    # Display HSV over captured image
    cv2.putText(res,'High HSV: '+str(highhsv),(5,210 + height),textFont,textScale,(textColour,textColour,textColour),2)
    cv2.putText(res,'Low HSV: '+str(lowhsv),(5,240 + height),textFont,textScale,(textColour,textColour,textColour),2)

    # Show image with only specified color range
    cv2.imshow('Found Colours', res)
    # Adjust window sizes
    cv2.resizeWindow('Colour Adjustment',300,25)
    cv2.resizeWindow('Capture Area',300,0)

    # Break loop with esc key
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

相关问题