<jupyter_start><jupyter_text>OpenCV and matplotlibA histogram representsthe distribution of pixel intensities (whether color or grayscale)in an image. It can be visualized as a graph (or plot)that gives a high-level intuition of the intensity (**pixel value**)distribution.<jupyter_code># 
# Based environment:
# 
from __future__ import print_function
from  matplotlib import pyplot as plt
%matplotlib inline
import cv2
import numpy as np
import pandas as pd
image = cv2.imread("../img_source/must_logo.png")
plt.imshow(image)
plt.title('Original')
plt.show()<jupyter_output><empty_output><jupyter_text>Color Histogram<jupyter_code>chans=cv2.split(image)
colors=("r","b","g")

plt.figure()
plt.title("’Flattened’ Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")

for (chan, color) in zip(chans,colors):
    hist=cv2.calcHist([chan],[0],None,[256],[0,256])
    plt.plot(hist,color=color)
    plt.xlim([0,256])

fig = plt.figure()
ax = fig.add_subplot(131)
hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None,
[32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and B")
plt.colorbar(p)

ax = fig.add_subplot(132)
hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None,
[32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and R")
plt.colorbar(p)

ax = fig.add_subplot(133)
hist = cv2.calcHist([chans[0], chans[2]], [0, 1], None,
[32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for B and R")
plt.colorbar(p)

print("2D histogram shape: {}, with {} values".format(hist.shape, hist.flatten().shape[0]))

hist = cv2.calcHist([image], [0, 1, 2],None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
print("3D histogram shape: {}, with {} values".format(hist.shape, hist.flatten().shape[0]))

plt.show()
cv2.waitKey(0)<jupyter_output>2D histogram shape: (32, 32), with 1024 values
3D histogram shape: (8, 8, 8), with 512 values<jupyter_text>Mask Histogram<jupyter_code>def plot_histogram(image, title, mask = None):
    chans = cv2.split(image)
    colors = ("b", "g", "r")
    plt.figure()
    plt.title(title)
    plt.xlabel("Bins")
    plt.ylabel("# of Pixels")

    for (chan, color) in zip(chans, colors):
        hist = cv2.calcHist([chan], [0], mask, [256], [0, 256])
        plt.plot(hist, color = color)
        plt.xlim([0, 256])

mask = np.zeros(image.shape[:2], dtype = "uint8")
cv2.rectangle(mask, (15, 15), (130, 100), 255, -1)
plt.subplot(1,2,1)
plt.imshow(mask)
plt.title("Mask")

masked = cv2.bitwise_and(image, image, mask = mask)
plt.subplot(1,2,2)
plt.imshow(masked)
plt.title("Mask Applied to Imag")<jupyter_output><empty_output><jupyter_text>Expanding<jupyter_code>BLUE=[255,0,0]
replicate = cv2.copyMakeBorder(image,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(image,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(image,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(image,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(image,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)

# plt.subplot(231),plt.imshow(image,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')<jupyter_output><empty_output><jupyter_text>Equalization<jupyter_code>image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
eq = cv2.equalizeHist(image)
plt.imshow(np.hstack([image,eq]))
plt.title("Histogram Equalization")
plt.show()<jupyter_output><empty_output><jupyter_text>Grayscale Histogram<jupyter_code>image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

hist = cv2.calcHist([image], [0], None, [256], [0, 256])

plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1786647659776669, 'fraction_numerical': 0.06248515086718936, 'mean_word_length': 5.578125, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '138889', 'license': 'MIT', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1690, 'n_words': 309, 'path': 'notebooks/03_OpenCV_and_matplotlib.ipynb', 'repo_name': 'Tim-eyes/example_driven_guide_opencv', 'size': '4209'}