Python#RGB-HSV

def RGB_HSV(img):
    b, g, r = cv2.split(img)
    rows, cols = b.shape
    H = np.ones([rows, cols], "float")
    S = np.ones([rows, cols], "float")
    V = np.ones([rows, cols], "float")
    print(rows, cols)
    for i in range(0,rows):
        for j in range(0, cols):
            MAX = max((b[i, j], g[i, j], r[i, j]))
            MIN = min((b[i, j], g[i, j], r[i, j]))
            V[i, j] = MAX
            if V[i, j] == 0:
                S[i, j] = 0
            else:
                S[i, j] = (V[i, j] - MIN)/V[i, j]
            if MAX == MIN:
                H[i, j] = 0
            elif V[i, j] == r[i, j]:
                H[i, j] = (60*(float(g[i, j]) - b[i, j])/(V[i, j] - MIN))
            elif V[i, j] == g[i, j]:
                H[i, j] = 60 * (float(b[i, j]) - r[i, j]) / (V[i, j] - MIN) + 120
            elif V[i, j] == b[i, j]:
                H[i, j] = 60 * (float(r[i, j]) - g[i, j]) / (V[i, j] - MIN) + 240
            if H[i,j] < 0:
                H[i, j] = H[i, j] + 360
            H[i, j] = H[i, j]/2
            S[i, j] = 255*S[i, j]
    result = cv2.merge((H, S, V))
    result = np.uint8(result)
    cv2.imshow("V channel", result[:, :, 2])
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return result

 

你可能感兴趣的:(python)