<jupyter_start><jupyter_text>[[chapter_collab]]<jupyter_code># Convert BGR image into RGB 
def bgr2rgb(img):
    return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

def normalize(seq, input_start, input_end, output_start, output_end):
    factor = (output_end - output_start) / (input_end - input_start)
    return (output_start + factor * (seq - input_start)).astype(np.uint8)    
layout = (2, 3)
plt.subplot2grid(layout, (0, 1), colspan=1)
plt.imshow(bgr2rgb(img)), plt.title("Original")
plt.subplot2grid(layout, (1, 0))
plt.imshow(blue), plt.title("Blue")
plt.colorbar()
plt.subplot2grid(layout, (1, 1))
plt.imshow(green), plt.title("Green")
plt.colorbar()
plt.subplot2grid(layout, (1, 2))
plt.imshow(red), plt.title("Red")
plt.colorbar()
plt.savefig("T1-Plots/RGB_Plots.png")
plt.show()
# Convert BGR image into HSV 
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Split HSV  into its channels
h, s, v = cv2.split(hsv_img)
# Map Hue to range (0, 255)
h = normalize(h, 0, 179, 0, 255)

layout = (2, 3)
plt.subplot2grid(layout, (0, 1), colspan=1)
plt.imshow(hsv_img), plt.title("HSV")
plt.subplot2grid(layout, (1, 0))
plt.imshow(h), plt.title("HUE")
plt.colorbar()
plt.subplot2grid(layout, (1, 1))
plt.imshow(s), plt.title("Saturation")
plt.colorbar()
plt.subplot2grid(layout, (1, 2))
plt.imshow(v), plt.title("Value")
plt.colorbar()
plt.savefig("T1-Plots/HSV_Plots.png")
plt.show()
def find_bin(num, bins):
    n_bins = len(bins) - 1
    for i in range(n_bins):
        if (num >= bins[i]) and (num < bins[i + 1]):
            return i

    return None


def bin_numbers(seq, bins):
    binnumbers = []
    for num in seq:
        bin_number = find_bin(num, bins)
        binnumbers.append(bin_number)

    return np.array(binnumbers)

def histogram(seq, n_bins=256, range=(0, 256)):
    bins = np.linspace(range[0], range[1], n_bins + 1)
    
    #Find indices where elements should be inserted to maintain order.
    bin_indices = np.searchsorted(bins, seq, side='left')
    hist = np.bincount(bin_indices, minlength=n_bins)

    # Return x axis  and height values to plot 
    return hist, bins


def counter(seq):
    z = Counter(seq.tolist())
    hist = np.zeros(256)
    for el in z:
        hist[el] = z[el]

    bins = np.arange(0, 256)

    return hist, bins

images = {
    'RED': red,
    'GREEN': green,
    'BLUE': blue,
    'HUE': h,
    'SATURATION': s,
    'Value': v
}

for title in images:
    hist_m, bins_m = histogram(images[title].ravel(), 256, [0, 256])
    plt.title(title) # + " mine")
    plt.bar(bins_m[:-1], height=hist_m)
    plt.savefig("T1-Plots/histogram_{}".format(title))
    plt.show()
    hist, bins = np.histogram(images[title].ravel(), 256, [0, 256])
    assert np.equal(hist, hist_m).all(), "My histogram function is not equal to numpy histogram"

    hist_b = np.bincount(images[title].flatten(), minlength=256)
    assert np.equal(hist, hist_b).all(), "My histogram function is not equal to bincount"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1623989218328841, 'fraction_numerical': 0.03099730458221024, 'mean_word_length': 3.9648829431438126, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '494312', 'license': 'MIT', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1157, 'n_words': 266, 'path': '1/Task1.ipynb', 'repo_name': 'buzem/vision-projects', 'size': '2968'}