Автоматическая цветовая калибровка изображения на основе двух цветов (белого и синего)

#python-3.x

Вопрос:

Я хочу создать скрипт автоматической настройки цвета для новых изображений. Где белый и определенный синий цвета, найденные на изображении, имеют известный цвет RGB с самого начала, и с помощью этого откалибруйте изображения в правильном цветовом пространстве. Прилагается изображение с известными цветами RBG (белый: 255,255,255, синий: 54,201,208). Изображение синей коробки на белом фоне . Я могу сегментировать синюю рамку на изображении, но оттуда я застрял.

Я пробовал некоторые методы, но не могу понять, нужно ли делать это автоматически и на основе двух цветов. Это лучшее, что я нашел до сих пор здесь, на StackOverflow: (код не мой, найден здесь, в стеке), что напоминает задачу, которую я хочу выполнить, но я хочу сделать ее автоматической и основанной на 2 цветах:

 def hist_match(source, template):
oldshape = source.shape
source = source.ravel()
template = template.ravel()

# get the set of unique pixel values and their corresponding indices and
# counts
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
                                        return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)

# take the cumsum of the counts and normalize by the number of pixels to
# get the empirical cumulative distribution functions for the source and
# template images (maps pixel value --> quantile)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]

# interpolate linearly to find the pixel values in the template image
# that correspond most closely to the quantiles in the source image
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape)

B = im[:,:, 0]
G = im[:,:, 1]
R = im[:,:, 2]

R= np.array(R).astype('float')
G= np.array(G).astype('float')
B= np.array(B).astype('float')

# Extract pixels that correspond to pure white R = 255,G = 255,B = 255
B_white = R[190, 205]
G_white = G[220, 230]
R_white = B[230, 245]

print (B_white)
print (G_white)
print (R_white)

# Compensate for the bias using normalization statistics
R_balanced = R / R_white
G_balanced = G / G_white
B_balanced = B / B_white

R_balanced[np.where(R_balanced > 1)] = 1
G_balanced[np.where(G_balanced > 1)] = 1
B_balanced[np.where(B_balanced > 1)] = 1

B_balanced=B_balanced * 255
G_balanced=G_balanced * 255
R_balanced=R_balanced * 255

B_balanced= np.array(B_balanced).astype('uint8')
G_balanced= np.array(G_balanced).astype('uint8')
R_balanced= np.array(R_balanced).astype('uint8')

im[:,:, 0] = (B_balanced)
im[:,:, 1] = (G_balanced)
im[:,:, 2] = (R_balanced)

# Notice saturation artifacts 
cv2.imshow('frame',im)
cv2.waitKey()
cv2.destroyAllWindows()