{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"WdFUrY9aLUhI"},"source":["## CS535/EE514 - Spring 2023 - Assignment 0 - Part B\n","\n","\n","\n","#### Important Instructions:\n","- You are advised to follow good programming practies including approriate variable naming and making use of logical comments \n","\n","\n","The university honor code should be maintained. Any violation, if found, will result in disciplinary action. \n"]},{"cell_type":"markdown","metadata":{"id":"dClE8ipfLWKI"},"source":["Double click here to enter your name and roll number: \n","Name: \n","\n","Roll Number: \n",""]},{"cell_type":"markdown","source":["Run this cell to mount your Google Drive.
\n","Files from your drive can be uploaded and used in your projects here easily."],"metadata":{"id":"jGjB0Winr_uN"}},{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"id":"XJomA642EL-Z"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Setup"],"metadata":{"id":"bo-kd-r5ftrz"}},{"cell_type":"markdown","metadata":{"id":"rVQ72cRFIMtP"},"source":["\n","> This section loads some required libraries used in this notebook: **numpy**, **pandas**, **cv2**, **skimage**, **PIL**, **matplotlib**\n","\n","* [Numpy](https://www.numpy.org/) is an array manipulation library, used for linear algebra, Fourier transform, and random number capabilities.\n","* [Pandas](https://pandas.pydata.org/) is a library for data manipulation and data analysis.\n","* [CV2](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html) is a library for computer vision tasks.\n","* [Skimage](https://scikit-image.org/) is a library which supports image processing applications on python.\n","* [Matplotlib](https://matplotlib.org/) is a library which generates figures and provides graphical user interface toolkit.\n","\n","\n","\n","\n","\n","\n","\n"]},{"cell_type":"markdown","source":["**Install packages/libraries.**
\n","Run the following code to install the relevant libraries on your machine.
Make sure you've installed all the previous libraries from PA1_PartA.ipynb.\n"],"metadata":{"id":"-E-RJnOMfrij"}},{"cell_type":"code","source":["!pip install seaborn"],"metadata":{"id":"ZoiKwsFofl1b"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Import packages/libraries.**
\n","Run the following code to check if the relevant libraries are installed on your machine.\n"],"metadata":{"id":"RuTM0sR0gmaJ"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"otRAC8KaKMAe"},"outputs":[],"source":["import matplotlib\n","from mpl_toolkits import mplot3d\n","from matplotlib import pyplot as plt\n","from matplotlib import cm\n","from matplotlib import image as mpimg\n","from matplotlib.pyplot import figure\n","%matplotlib inline\n","import seaborn as sns\n","import numpy as np\n","import matplotlib.pylab as pl\n","from matplotlib.colors import ListedColormap"]},{"cell_type":"markdown","source":["# Task 6: Image Manipulation (20 marks)"],"metadata":{"id":"XFUG6e__g3B3"}},{"cell_type":"markdown","source":["# Part 1 : Image as a numpy array
\n","In this part you will read an image uploaded onto the drive. Then you will display and crop the image using matplotlib. Next, you will have to randomly place markers on your image using matplotlib.
\n","After that, analyze the RGB channels by plotting their values using seaborn. Extract a single channel by indexing the image."],"metadata":{"id":"uxsyiNi8v8kI"}},{"cell_type":"markdown","source":["Read image from file to memory using the given command:
\n","$img = np.array(mpimg.imread(‘path’))$
\n","Replace *path* in the above command with the path of your image e.g. *intro/cat.jpg*"],"metadata":{"id":"_2D1vEKJgUZk"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"iYqeVbkCgfDz"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Now run the command:
\n","$img.setflags(write=1)$
\n","This allows us to manipulate the image"],"metadata":{"id":"piVJ0JlwNtLr"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"jTYKHvHjN1vN"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Display your image using the [plt.imshow](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.imshow.html) function."],"metadata":{"id":"6hyx8QLzN5iw"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"adHVe07wOEU1"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Crop the image.\n"],"metadata":{"id":"lu377N7cOXah"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"oENHyrUVOYKJ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Create 50 randomly placed markers on your image. You can check the following\n","[link](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) for learning about markers.\n"],"metadata":{"id":"o1zgKxJdOYno"}},{"cell_type":"code","source":["import random"],"metadata":{"id":"0iqhOktdil7h"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"_K5q6uPwOfYw"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Carry out color analysis by accessing the RGB values and plotting them. Use the\n","$seaborn$ library."],"metadata":{"id":"NUgqZeaIOauK"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"pZlA_bpzOfzi"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["View only the red values.
\n","*Hint: Set the blue and green values to 0*"],"metadata":{"id":"oIgDfuDUOc5Y"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"PY7OMx-JOe4K"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Part 2: Image Manipulation and Analysis"],"metadata":{"id":"R7pprsioxpMA"}},{"cell_type":"code","metadata":{"id":"6dy-iP-VTibt"},"source":["import numpy as np\n","import pandas as pd\n","import cv2 as cv \n","from google.colab.patches import cv2_imshow # for image display\n","from skimage import io\n","from PIL import Image \n","import matplotlib.pylab as plt"],"execution_count":null,"outputs":[]},{"cell_type":"code","source":["from google.colab.patches import cv2_imshow"],"metadata":{"id":"lJXfnUuK68II"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"6MYXdmP7fElG"},"source":["\n","> In this step we will read images from urls using skimage, and display them using openCV, please note the difference when reading image in RGB and BGR format. The default input color channels are in BGR format for openCV. So, make sure you convert it into RGB.\n"]},{"cell_type":"markdown","metadata":{"id":"hPYOeezmsWFj"},"source":["Image source examples:\n","\n","[Place Kitten](https://placekitten.com/) - use the base Place Kitten URL followed by a width and height separated by backslashes ''/''. For example, use the URL `https://placekitten.com/500/300` to fetch a cat image with a width of 500px and height of 300px.\n","\n","[NC State University Libraries Special Collections](https://d.lib.ncsu.edu/collections/catalog) - browse the site to find an image thumbnail. Right-click on the thumbnail and select \"Copy Image Address\". The address will look like this: `https://iiif.lib.ncsu.edu/iiif/0051230/square/300,/0/default.jpg`. Replace the word \"square\" with the word \"full\" and replace \"300\" with \"800\" to access the full image at a width of 800px.\n","\n","[Google Image search](https://www.google.com/imghp?hl=en) - search for an image. Left-click one of the returned images, then right-click on the full image, and then select \"Copy Image Address\"."]},{"cell_type":"markdown","source":["Gather any 5 URLs from the above links, and store them in a list.\n"],"metadata":{"id":"3Jj4u1wLytln"}},{"cell_type":"code","source":["urls = None\n","# Enter code here\n","# ----------------"],"metadata":{"id":"-arBWeKwyz9S"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Using a for loop, for each url do the following:
\n","\n","\n","1. Read the image from the url as *og_img*. The skimage library will be useful for this task.\n","2. The image read will be in BGR format. Convert it to RGB and store as *final_img*. The cv2 library will be useful for this task.\n","3. Use the $cv.hconcat$ to combine the *og_im*g and *final_img* side by side and store as *frame*. \n","4. Display the *frame*. The cv2 library will be useful for this task.\n","\n","Make sure you store all the final_img(s) in an array for later use."],"metadata":{"id":"QJ99R-WSy61v"}},{"cell_type":"code","source":["og_img = None\n","final_img = None\n","frame = None\n","final_imgs = []\n","# Enter code here\n","# ----------------"],"metadata":{"id":"BP7R2eui3nWI"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["From the array of final_img(s) read any 1 image and store it in *image*."],"metadata":{"id":"VqsUGvJ55qqg"}},{"cell_type":"code","source":["image = None\n","# Enter code here\n","# ----------------"],"metadata":{"id":"eXnX5mYV5xL2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Run the cell below to check the dimensions of the image. Try testing it for other images as well from your array of images."],"metadata":{"id":"vSVvOcud570Y"}},{"cell_type":"code","source":["# Check the image matrix data type (could know the bit depth of the image)\n","print(image.dtype)\n","# Check the height of image \n","print(image.shape[0])\n","# Check the width of image \n","print(image.shape[1])\n","# Check the number of channels of the image\n","print(image.shape[2])"],"metadata":{"id":"XRE3tUEp5xUn"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"JlHHA4CDogVd"},"source":["### Generate Histogram of color image and grayscale image\n","Sometimes you want to enhance the contrast in your image or expand the contrast in a particular region while sacrificing the detail in colors that don’t vary much, or don’t matter. A good tool to find interesting regions is the histogram. To create a histogram of our image data, we use the matplot.pylab `hist()` function.\n","\n","More info: [Histogram](https://www.geeksforgeeks.org/matplotlib-pyplot-hist-in-python/)"]},{"cell_type":"markdown","metadata":{"id":"2V0pqbfAxTIn"},"source":["Display the histogram of R, G, B channel.
\n","Helpful functions:\n","cv.calcHist(), plt.xlim()\n"]},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"6u-DOv576Z1n"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Convert the image into a grayscale image. Display the image and the histogram of the grayscale image."],"metadata":{"id":"nm8YyTjA7G0_"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"luksJiwo7Q8n"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Part 3: Noise and Edge Detection"],"metadata":{"id":"VzOgMvzPAVGG"}},{"cell_type":"markdown","source":["We will be using the image of a Rose in this part. Download the [image](https://drive.google.com/file/d/14gonL4aN4SnI-R389-AmWf_ItrqskrRE/view?usp=sharing).
\n","Upload it to your drive and read the image as $rose$ as done in Part 1.
\n","Convert the image into grayscale and store in $gray$.
\n","Display the image $rose$ and $gray$."],"metadata":{"id":"LhE_t8TVEj9Y"}},{"cell_type":"code","source":["rose = None\n","gray = None\n","# Enter code here\n","# ----------------"],"metadata":{"id":"-ug4ir_NEjVy"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Adding Noise to the Image**"],"metadata":{"id":"Rhb6zMaOC2Eb"}},{"cell_type":"markdown","source":["*salt_pepper* is a function that adds noise to the $gray$ image.
\n","Pass a reasonable value of *prob* in the range (0,1) and call the function.
\n","Store the resulting image as $noise$."],"metadata":{"id":"xmw7TBivGhiH"}},{"cell_type":"code","source":["def salt_pepper(prob):\n"," # Extract image dimensions\n"," row, col = gray.shape\n","\n"," # Declare salt & pepper noise ratio\n"," s_vs_p = 0.5\n"," output = np.copy(gray)\n","\n"," # Apply salt noise on each pixel individually\n"," num_salt = np.ceil(prob * gray.size * s_vs_p)\n"," coords = [np.random.randint(0, i - 1, int(num_salt))\n"," for i in gray.shape]\n"," output[coords] = 1\n","\n"," # Apply pepper noise on each pixel individually\n"," num_pepper = np.ceil(prob * gray.size * (1. - s_vs_p))\n"," coords = [np.random.randint(0, i - 1, int(num_pepper))\n"," for i in gray.shape]\n"," output[coords] = 0\n"," cv2_imshow(output)\n","\n"," return output"],"metadata":{"id":"fY6Uk5E_GL0v"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["noise = None\n","# Call function here\n","# ----------------"],"metadata":{"id":"rUbX5HJ0HJjA"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Applying Filter to remove noise**
\n","https://www.graphicsmill.com/docs/gm/minimum-maximum-median-filters.htm
\n","The link gives a bried explanation of what minimum and maximum filters are.
\n","We will apply these to remove the noise from the image."],"metadata":{"id":"ZfTYJ3tQH8Zx"}},{"cell_type":"code","source":["from scipy.ndimage import maximum_filter, minimum_filter"],"metadata":{"id":"lJ31rZNVIYf-"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Find documentation for the functions below:
\n","[maximum_filter](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.maximum_filter.html)
\n","[minimum_filter](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.minimum_filter.html)"],"metadata":{"id":"56KnfwH_Ml2n"}},{"cell_type":"code","source":["# Apply changes to function here:\n","\n","def remove_noise(img):\n"," # Apply maxmum_filter on the img and store it in maxf\n"," # Apply minimum_filter on the img and store it in minf\n"," # Return the filtered img\n"," return img\n","\n","# ----------"],"metadata":{"id":"YU1d7-3rKg_X"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Now call the *remove_noise* function on the *noise* image and display the new image."],"metadata":{"id":"Wr4b96ucMEmm"}},{"cell_type":"code","source":["result = None\n","# Call function here: \n","# ------------\n","# Display:\n","cv2_imshow(result)\n","# ------------"],"metadata":{"id":"mVetRY0tL5RC"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Edge Detection**
\n","We will use the Canny Edge Detector to detect edges in our grayscale image.
\n","Use the following link as documentation of the function you should use:
\n","https://java2blog.com/cv2-canny-python/
\n","*Note: You do not need to specify the optional parameters.*
"],"metadata":{"id":"B2IBweU7OOZn"}},{"cell_type":"code","source":["# Enter code here\n","# ----------------"],"metadata":{"id":"Mld_2yj3ONWO"},"execution_count":null,"outputs":[]}]}