
Python Code
import cv2
def run_histogram_equalization(image_path):
rgb_img = cv2.imread(image_path)
# convert from RGB color-space to YCrCb
ycrcb_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2YCrCb)
# equalize the histogram of the Y channel
ycrcb_img[:, :, 0] = cv2.equalizeHist(ycrcb_img[:, :, 0])
# convert back to RGB color-space from YCrCb
equalized_img = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR)
cv2.imshow('equalized_img', equalized_img)
cv2.waitKey(0)
Explanation
Histogram Equalization (HE) is a statistical approach for spreading out intensity values. In image processing, HE is used for improving the contrast of any image, that is- to make the dark portion darker and the bright portion brighter.
For a grey-scale image, each pixel is represented by the intensity value (brightness); that is why we can feed the pixel values directly to the HE function. However, that is not how it works for an RGB-formatted color image. Each channel of the R, G, and B represents the intensity of the related color, not the intensity/brightness of the image as a whole. And so, running HE on these color channels is NOT the proper way.
We should first separate the brightness of the image from the color and then run HE on the brightness. Now, there are already standardized colorspaces that encode brightness and color separately, like- YCbCr, HSV, etc.; so, we can use them here for separating and then re-merging the brightness. The proper way:
Convert the colorspace from RGB to YCbCr >> Run HE on the Y channel (this channel represents brightness) >> Convert back the colorspace to RGB
Postscript
For HSV colorspace, HE should be run on the V channel. However, the Y channel of YCbCr is the better representer for brightness than the V channel of HSV. So, using the YCbCr format produces a more correct result for HE.

Postscript 2
HE is too a naive technique and often produces peculiar colors and small artifacts. This is because it does not care about outliers and the location of a pixel. So, extensions like- Contrast Limited Adaptive HE, Brightness preserving Bi-HE, etc. are used more commonly. Also, different noise reduction functions are executed in the post-processing phase for improving the final output.