How To Do Histogram Matching Using OpenCV - Automatic Addison
How To Do Histogram Matching Using OpenCV - Automatic Addison
Automatic Addison
Build the Future
In this tutorial, you will learn how to do histogram matching using OpenCV.
Histogram matching (also known as histogram specification), is the
Sell On TikTok
transformation of an image so that its histogram matches the histogram of an
Open
Reachofnew
image audiences.
your choiceWhat happens
(we’ll on TikTok,
call this image doesn’t stay on
of your TikTok.the
choice TikTok
“reference image”).
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 1/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
We want the image above to match the histogram of the reference image below.
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 2/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
After performing histogram matching, the output image needs to look like this:
Then, to make things interesting, we want to use this mask to mask the output
image.
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 3/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
Mask
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 4/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
Directions
Below is the source code for the program that makes everything happen. Make
sure you copy and paste this code into a single Python file (mine is named
histogram_matching.py). Then put that file, as well as your source, reference,
and mask images all in the same directory (or folder) in your computer. Once you
have done that, run the code using the following command (note: mask image is
optional):
Source Code
/usr/bin/env python
'
lcome to the Histogram Matching Program!
age:
python histogram_matching.py <source_image> <ref_image> [<mask_image>]
'
f calculate_cdf(histogram):
"""
This method calculates the cumulative distribution function
:param array histogram: The values of the histogram
:return: normalized_cdf: The normalized cumulative distribution function
:rtype: array
"""
# Get the cumulative sum of the elements
cdf = histogram.cumsum()
return normalized_cdf
f calculate_lookup(src_cdf, ref_cdf):
"""
This method creates the lookup table
:param array src_cdf: The cdf for the source image
:param array ref_cdf: The cdf for the reference image
:return: lookup_table: The lookup table
:rtype: array
"""
Sell On TikTok
lookup_table = np.zeros(256) Open
lookup_val = 0 What happens on TikTok, doesn’t stay on TikTok. TikTok
Reach new audiences.
for src_pixel_val in range(len(src_cdf)):
lookup_val
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 6/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
f match_histograms(src_image, ref_image):
"""
This method matches the source image histogram to the
reference signal
:param image src_image: The original source image
:param image ref_image: The reference image
:return: image_after_matching
:rtype: image (array)
"""
# Split the images into the different color channels
# b means blue, g means green and r means red
src_b, src_g, src_r = cv2.split(src_image)
ref_b, ref_g, ref_r = cv2.split(ref_image)
# Compute the normalized cdf for the source and reference image
src_cdf_blue = calculate_cdf(src_hist_blue)
src_cdf_green = calculate_cdf(src_hist_green)
src_cdf_red = calculate_cdf(src_hist_red)
ref_cdf_blue = calculate_cdf(ref_hist_blue)
ref_cdf_green = calculate_cdf(ref_hist_green)
ref_cdf_red = calculate_cdf(ref_hist_red)
# Use the lookup function to transform the colors of the original
# source image
blue_after_transform = cv2.LUT(src_b, blue_lookup_table)
green_after_transform = cv2.LUT(src_g, green_lookup_table)
red_after_transform = cv2.LUT(src_r, red_lookup_table)
Sell Onimage_after_matching
return TikTok Open
Reach new audiences. What happens on TikTok, doesn’t stay on TikTok. TikTok
f mask_image(image, mask):
"""
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 7/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
# Resize the mask to be the same size as the source image
resized_mask = cv2.resize(
mask, (image.shape[1], image.shape[0]), cv2.INTER_NEAREST)
f main():
"""
Main method of the program.
"""
start_the_program = input("Press ENTER to perform histogram matching..."
# A flag to indicate if the mask image was provided or not by the user
mask_provided = False
try:
image_mask_name = sys.argv[3]
mask_provided = True
except:
print("\nNote: A mask was not provided.\n")
image_mask = cv2.imread(cv2.samples.findFile(image_mask_name))
__name__ == '__main__':
print(__doc__)
main()
cv2.destroyAllWindows()
Sample Output
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 9/10
7/2/22, 12:49 AM How to Do Histogram Matching Using OpenCV – Automatic Addison
Automatic Addison /
Proudly powered by WordPress
https://automaticaddison.com/how-to-do-histogram-matching-using-opencv/ 10/10