After converting the original *.jpg file, using the "reference white color" method (the idea is from here)
def whitepatch_balancing(image, from_row, from_column, row_width, column_width):
fig, ax = plt.subplots(1,2, figsize=(10,5))
ax[0].imshow(image)
ax[0].add_patch(Circle((from_column, from_row),
linewidth=3,
edgecolor='r', facecolor='none'));
ax[0].set_title('Original image')
image_patch = image[from_row:from_row+row_width, from_column:from_column+column_width]
image_max = (image*1.0 / image_patch.max(axis=(0, 1))).clip(0, 1)
ax[1].imshow(image_max);
ax[1].set_title('Whitebalanced Image with MAX value of white')
plt.show()
return image_max
if __name__ == '__main__':
image = imread('img.jpg')
balanced_img = whitepatch_balancing(image, 60, 1230, 20, 20)
if the original image (in RGB format) looked like this: [[[131 152 205], [120 143 195], …, [135 150 191]]]
then the resulting array after conversion looks something like this: [[[0.99242424 0.98701299 1. ], [0.90909091 0.92857143 1. ], …, [0.84090909 0.64285714 0.21025641]]]
using matplotlib.pyplot as plt, you can display the picture and see that the color correction has occurred original and converted images
but if you try to save the converted array to a file *.jpeg or *.png – we get just a black picture (all pixel values are less than one)
how can the resulting result (the converted image) be adequately saved to a file?
>Solution :
Your balanced_image has been scaled between 0 and 1, so you will need to rescale it back between 0, 255 before saving it. Your rgb values are <1 for all channels which is basically black.
balanced_img = whitepatch_balancing(image, 60, 70, 60, 70)
cv2.imwrite('test.png', balanced_img*255)