<jupyter_start><jupyter_text>Objective:  In this problem, histogram command for images is considered. The difference of histogram for different types of images is investigated.   Loading required modules:<jupyter_code>import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Reading image:<jupyter_code>I = mpimg.imread('pollen_dark.tif')<jupyter_output><empty_output><jupyter_text>Displaying image:<jupyter_code>img=plt.imshow(I,cmap='gray',vmin=0, vmax=255)
plt.axis('off')
plt.colorbar()
plt.title('pollen dark')
plt.show()<jupyter_output><empty_output><jupyter_text>Displaying image histogram:<jupyter_code>fig = plt.figure(figsize=(10, 8))
plt.hist(I.flatten(),bins=255,range=(0,255))
plt.title('Histogram of loaded dark image', fontsize=20)
plt.xlabel('Intensity Values', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
plt.show()
h = np.histogram(I.flatten(),bins=256,range=(0,255))<jupyter_output><empty_output><jupyter_text>When using the command in above cell, two vectors will be returned. The first one is height of the values (numbers of specific values) and the other vector is the bins.<jupyter_code>X = np.delete(h[1], 256)
height = h[0]
fig = plt.figure(figsize=(10, 8))
plt.bar(X,height)
plt.title('Histogram of loaded dark image using bar command', fontsize=20)
plt.xlabel('Intensity Values', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
plt.show()
I_float = I.astype(float)
fig = plt.figure(figsize=(10, 8))
plt.hist(I_float.flatten(),bins=256,range=(0,1))
plt.show()<jupyter_output><empty_output><jupyter_text>The histogram is not displaying properly because there are no pixels with intensity values between 0 and 1.<jupyter_code>whos
plt.hist(I, bins=256,range=(0,255))<jupyter_output><empty_output><jupyter_text>The error is because of how we are implementing histogram command. "I" is a 500 by 500 matrix made of separate arrays. So, when using "hist", the command tries to plot all these arrays on top of each other. Technically, this implementation is wrong and also, it is a memory error.<jupyter_code>fig = plt.figure(figsize=(10, 8))
plt.hist(I_float)
plt.title('Histogram of loaded dark image using plt.hist(I_float)', fontsize=20)
plt.xlabel('Intensity Values', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
plt.show()<jupyter_output><empty_output><jupyter_text>The histogram looks different from part (a). When casting image as a float, the intensity values remain the same, but float variable are more precise comparing to unit8. As a result, histogram is divided into precise intensity values and consequently, each individual bin has small number of pixels. The bins are in close adjacency of each other. Based on application and computational power, it can be a useful command.<jupyter_code>fig = plt.figure(figsize=(10, 8))
plt.hist(I_float.flatten())
plt.title('Histogram of loaded dark image without defining bin number', fontsize=20)
plt.xlabel('Intensity Values', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
plt.show()<jupyter_output><empty_output><jupyter_text>The histogram looks different. Because when we do not specify number of bins, python considers random number of bins for histogram. That is why we have different histogram presentation. Setting the number of bins equal to 256 would result in same histogram.<jupyter_code>fig = plt.figure(figsize=(10, 8))
plt.hist(I_float.flatten(),bins=256,range=(0,255))
plt.title('Histogram of loaded dark image with defining bin number as 256' ,fontsize=20)
plt.xlabel('Intensity Values', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
plt.show()
I1 = mpimg.imread('pollen_dark.tif')
I4 = mpimg.imread('pollen_highcontrast.tif')
I2 = mpimg.imread('pollen_light.tif')
I3 = mpimg.imread('pollen_lowcontrast.tif')
fig = plt.figure(figsize=(18, 16))
plt.subplot(4, 4, 1)
plt.imshow(I1,cmap='gray',vmin=0, vmax=255)
plt.title('Dark Image')
plt.axis('off')
plt.subplot(4, 4, 2)
plt.imshow(I2,cmap='gray',vmin=0, vmax=255)
plt.title('Light Image')
plt.axis('off')
plt.subplot(4, 4, 3)
plt.imshow(I3,cmap='gray',vmin=0, vmax=255)
plt.title('Low Contrast Iamge')
plt.axis('off')
plt.subplot(4, 4, 4)
plt.imshow(I4,cmap='gray',vmin=0, vmax=255)
plt.title('High Contrast Image')
plt.axis('off')
plt.subplot(4, 4, 5)
plt.hist(I1.flatten(),bins=256,range=(0,255))
plt.title('Histogram of Dark Image')
plt.subplot(4, 4, 6)
plt.hist(I2.flatten(),bins=256,range=(0,255))
plt.title('Histogram of Light Image')
plt.subplot(4, 4, 7)
plt.hist(I3.flatten(),bins=256,range=(0,255))
plt.title('Histogram of Low Contrast Image')
plt.subplot(4, 4, 8)
plt.hist(I4.flatten(),bins=256,range=(0,255))
plt.title('Histogram of High Contrast Image')
fig.suptitle('Four Image Types and Their Corresponding Histograms', fontsize = 20)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13588634959851761, 'fraction_numerical': 0.039324686020177066, 'mean_word_length': 7.233898305084746, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '235463', 'license': 'CC0-1.0', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1671, 'n_words': 457, 'path': 'Image Histogram/Image Histograms.ipynb', 'repo_name': 'Sinaconstantine/Digital-Image-Processing-EE596-', 'size': '4857'}