简体   繁体   中英

Colour Calibration in HSV colour space

Currently I'm using OpenCV ColorCorrectionModel to calibrate my photos' colour.

However it only seem to support the RGB colour space. I hope to use HSV because I'm going to feed these images into deep learning models, and colour is an important criteria in classifying these images.

Is there any existing library to compute the color correction model in HSV space?

The photos are taken with a 24-patch ColorChecker.

Considering an RGB range input, the code below outputs HSV range with detected colours. I hope this is what you are looking for:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM