<jupyter_start><jupyter_text>Lab - Image Processing and Analysis in Python with OpenCVWelcome! After completing this lab you will: 1. Learn to download, read and display images using Python, OpenCV and Matplotlib  2. Learn to apply various Image Processing techniques using Python and OpenCV  3. Understand about the importance of various Image Processing techniques  4. Analyze images using Histograms              Table of Contents            OpenCV        Downloading and plotting images in Python using OpenCV and Matplotlib        Image Processing using OpenCV        Analyzing Images using Histograms    Exercises            Estimated Time Needed: 1 hr   OpenCV [Homepage](http://opencv.org/)- OpenCV stands for: Open Source Computer Vision Library- includes C++, C, Python and Java interfaces  Import the OpenCV package **Did you know?** The package is called **`cv2`** in Python. The `2` in `cv2` doesn't refer to a particular package version.<jupyter_code>import cv2
print(cv2.__version__)<jupyter_output><empty_output><jupyter_text>Downloading and plotting images in Python using OpenCV and Matplotlib  Download an image Let's first download an image, using the `urllib` package. We'll need to specify a `url` of an image and a `filename`. We'll be downloading the following image of <NAME> and <NAME>, the President of the U.S. and Prime Minister of Canada, respectively.<jupyter_code>import urllib.request

#Set the url and filename
trumptrudeau_url = "https://upload.wikimedia.org/wikipedia/commons/7/76/Donald_Trump_Justin_Trudeau_2017-02-13_02.jpg"
trumptrudeau_filename = "trumptrudeau.jpg"


urllib.request.urlretrieve(trumptrudeau_url, trumptrudeau_filename) # downloads file as "trumptrudeau.jpg"<jupyter_output><empty_output><jupyter_text>**Verify that the image has downloaded**At this point, you should now see the file listed in the files directory in the left-sidebar of the JupyterLab environment. If this side menu is hidden, you can go to `View`>`View Left-Sidebar`.Alternatively, you can run the command below to check the files in your current directory:<jupyter_code>import os
os.listdir(os.curdir) #shows all files in current directory
#Is trump_filename in your directory?
print(trumptrudeau_filename in os.listdir(os.curdir))<jupyter_output><empty_output><jupyter_text>**Plotting images in Jupyter Notebooks** Next, let's display the image into this notebook, using OpenCV. We'll also be borrowing from the plotting library, `matplotlib`, to help display the images.<jupyter_code>from matplotlib import pyplot as plt
%matplotlib inline

trumptrudeau = cv2.imread(trumptrudeau_filename)

plt.imshow(trumptrudeau)<jupyter_output><empty_output><jupyter_text>Image Processing using OpenCV Hm... the Presidents are looking a little _blue_ in the above photo, wouldn't you say? **Fix colors when displaying images**<jupyter_code>img_corrected = cv2.cvtColor(trumptrudeau, cv2.COLOR_BGR2RGB)

plt.imshow(img_corrected)<jupyter_output><empty_output><jupyter_text>**Remove the axes around the image**<jupyter_code>plt.axis("off") #remove axes ticks
plt.imshow(img_corrected)<jupyter_output><empty_output><jupyter_text>**Change the size of the displayed image in the notebook** Note that this simply changes the display size of the image, not the actual image dimensions<jupyter_code>from pylab import rcParams

rcParams['figure.figsize'] = 10, 12

plt.axis("off") #remove axes ticks
plt.imshow(img_corrected)<jupyter_output><empty_output><jupyter_text>**Convert to Grayscale**<jupyter_code>gray_trumptrudeau = cv2.cvtColor(trumptrudeau, cv2.COLOR_BGR2GRAY)

plt.imshow(gray_trumptrudeau, cmap = 'gray')
plt.axis("off") #remove axes ticks
plt.title('Grayscale Image')<jupyter_output><empty_output><jupyter_text>**Canny Edge Detection**Canny Edge Detection is an algorithm used to detect edges in an image, and was developed by <NAME> in 1986.  Full OpenCV documentation on Canny Edge Detection: [here](https://docs.opencv.org/master/da/d22/tutorial_py_canny.html)<jupyter_code>rcParams['figure.figsize'] = 10, 12


edges = cv2.Canny(img_corrected, 
                  threshold1=100, 
                  threshold2=200)

plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])<jupyter_output><empty_output><jupyter_text>Try different levels of threshold:<jupyter_code>rcParams['figure.figsize'] = 10, 12

edges = cv2.Canny(img_corrected, 
                  threshold1=1,   ## try different values here
                  threshold2=200) ## try different values here

plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
rcParams['figure.figsize'] = 10, 12

edges = cv2.Canny(img_corrected, 
                  threshold1=100,   ## try different values here
                  threshold2=500) ## try different values here

plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])<jupyter_output><empty_output><jupyter_text>**Want to know the parameters of a function?** Try using a question mark `?` before a method to display the help doc for the method.<jupyter_code>?cv2.Canny<jupyter_output><empty_output><jupyter_text>Analyzing Images using Histograms **Histograms - Grayscale** Histograms allow you to quickly determine the number of pixels that are on a scale "black pixel" (0) to "white pixel" (255). Execute the code cell below to see the histogram of the image of President Trump and President Trudeau:<jupyter_code>import cv2
import numpy as np
from matplotlib import pyplot as plt

rcParams['figure.figsize'] = 8,4

plt.hist(gray_trumptrudeau.ravel(),256,[0,256])
plt.title('Histogram of Grayscale trumptrudeau.jpg')
plt.show()<jupyter_output><empty_output><jupyter_text>**What's in the histogram?**Like any other histogram, this graph shows the **count** of something. In this case, it's the _number of pixels in the image_ that are either _dark_ (towards the left of the graph) or _lighter_ (towards the right of the graph). **Why are histograms important**?- If you're concerned about whether the image is properly exposed (not too dark and not too bright), then histograms are a great way of checking the distribution of your lighter and darker pixels!- If your images are overexposed (too bright) or underexposed (too dark), you can fix exposure with OpenCV in Python (although you may lose some image quality). - This might be important if you're developing an app where users are taking photos and your app is trying to classify the objects inside the image. If it's too dark or too light, the objects may not be recognizable. **How do you read the histogram?**- On the **x-axis**, the values normally range from 0 (black) to 255 (white). So darker pixels are on the left, and whiter pixels are to the right. In the `matplotlib` chart above, the x-axis goes from 0 to 256, since each pixel is represented as a bin start from 0 to 0.99, and ends with 255 to 255.99.   - The **y-axis** shows the number of pixels found in the image on the scale of black to white.**Observations:**The grayscale histogram above shows that: - lots of pixels around `x = 30`, which are **darker pixels**.- lots of pixels around `x = 200`, which are **lighter pixels**.**Interpretation:**- the image appears to have lots of very dark, and very light pixels, but not a lot of pixels in between. Looking at the image of the Presidents, this is readily apparent -- the darker pixels seem to come from the black suits and the fireplace. A lot of the lighter pixels come from the furniture and walls. **Histograms - Color scale (RGB)**  Interpreting RGB histogramsLike grayscale histograms, RGB also shows the count distribution of pixels from black (`x=0`) to full color (`x=255`). Here is a color image of the presidents:<jupyter_code>rcParams['figure.figsize'] = 10, 12

plt.axis("off") #remove axes ticks
plt.imshow(img_corrected)<jupyter_output><empty_output><jupyter_text>And the following is an RGB histogram for the above image  RGB histogram<jupyter_code>rcParams['figure.figsize'] = 8, 4

color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([trumptrudeau],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()<jupyter_output><empty_output><jupyter_text>In the histogram above, you can observe that:- of the lighter pixels (around `x >= 200`), there are more red pixels than green or blue pixels. This is likely because of the redness in the faces of the presidents.- there seems to be a similar amount of dark red, dark blue, and dark green pixels (around `x<50`)- in terms of middle-tones, there seems to be more blue pixels than green or red. This is likely because of the neckties worn by the presidents.  Exercises **Exercise 1** 1.1. Download an image of your choosing, and display it  1.2. Convert the image to grayscale, and display it<jupyter_code># 1.1. Download an image of your choosing and display it

# Write your code below:<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- The answer is below:You should specify the download_image_url and download_image_filenamedownload_image_url = ""download_image_filename = ""urllib.request.urlretrieve(download_image_url, download_image_filename)img = cv2.imread(download_image_filename)plt.imshow(img)---><jupyter_code># 1.2. Convert the image to grayscale and display it

# Write your code below:<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- The answer is below:gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)plt.imshow(gray_img, cmap = "gray")plt.axis("off") remove axes ticksplt.title('Grayscale Image')---> **Exercise 2** 2.1. Using your grayscale image from Exercise 1, display its grayscale histogram  2.2. Using your color image from Exercise 1, display its color histogram  2.3. Look at your histogram and think about how its pixels are distributed. What are some observations that you can make?<jupyter_code># 2.1. Using your grayscale image from Exercise 1, display its grayscale histogram  

# Write your code below:<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- The answer is below:rcParams['figure.figsize'] = 8,4plt.hist(gray_img.ravel(),256,[0,256])plt.title('Histogram of Grayscale Image')plt.show()---><jupyter_code># 2.2. Using your color image from Exercise 1, display its color histogram  

# Write your code below:<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09225446857582165, 'fraction_numerical': 0.01739381126273304, 'mean_word_length': 5.23547034152187, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 81, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '295020', 'license': 'MIT', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3031, 'n_words': 1187, 'path': 'Image_Processing_with_OpenCV.ipynb', 'repo_name': 'prakHr/opencv-computer_vision', 'size': '10427'}