{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Lecture 02: Digital Image Basics (exercises)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX1: open image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 1. Open the image 'Popocatepetl_HD.jpg' located the subfolder /images, using various libraries: scikit-image, \n", " matplotlib, \n", " PIL, \n", " imageio. \n", "
\n", " Notice any difference?\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX2: get image charactaristics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 2. Find image characteristics:
\n", " - what type of variable is returned? what data type does it contain?
\n", " - how many bands?
\n", " - image size width/height? \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX3: plot color image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 3. Plot image using matplotlib. Add axis labels and title.
\n", " Hint: use matplotlib's pyplot.imshow() function
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX4: plot individual bands, play with colormaps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 4. Unpack the Red, Green, and Blue bands from the 3D array, and plot in seperate plots.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 4 bis. Test various colormaps using the option 'cmap' in pyplot.imshow(). Add the colorbar.
\n", "

Check out matplotlib's colormaps.


\n", " Try options 'vim' and 'vmax'. Try adding '_r' to the name of the colormap used. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX5: crop image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 5. Crop image to see only summit and ash plume
\n", " Hint: use numpy array \"slicing\" \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 5 bis. Get the RGB values of the blue sky (take the top-left pixel in the image) and the grass (bottom-left pixel).
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX6: plot crop box on image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 6. Plot the cropped region as a red rectangle on full resolution image.
\n", " Hint: use matplotlib's patches module \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 6-bis. Plot full resolution + crop box in one subplot, and the zoomed image in a second subplot.
\n", " Hint: use matplotlib's pyplot.subplots() function\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX7: plot histogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 7. Plot histogram of the red, green, and blue bands on a single plot.
\n", " Hint: use matplotlib's pyplot.hist() function
\n", " Hint: \"unravel\" the band to parse a 1D array to the function hist()\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX8: convert image to grayscale" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 8. Convert the color image to grayscale and plot.
\n", " 1. try an equally weighted mean of the R,G,B bands. Is this satisfying?
\n", " 2. try a weighted mean of the R,G,B bands, using luma's linear combination to approximate the luminance: Y = 0.2126*R + 0.7152*G + .0722 B. Is more satisfying?
\n", " 3. try using scikit-image \n", " color.rgb2gray() function
\n", "
\n", " 8 bis. Notice what happens to the data type? Plot a histogram of the grayscale image.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX9: plot profile" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 9. Plot a profile going through image, horizontally across the ash plume.
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX10: reduce the number of grayscale levels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 10. Reduce the number of colors used to 4 (=2 bits). Plot the histogram of the resulting image.
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX11: shift colors from color image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 11. Enhance colors by changing values of each band. \n", "
    \n", "
  1. try shifting all channels in the image by 40%, and plot. Is this what you expected?
  2. \n", "
  3. try clamping the channels, so that you end up with values in the range 0-255 with data type uin8 (or in the range 0-1 with data type float).
    Plot and compare with the original image.
  4. \n", "
  5. Create new bands R_f, G_f and B_f, altering the pixel color to red (R=255, G=0, B=0) where mask=1.
    Use numpy's where() function to create these new bands.
  6. \n", "
  7. plot histograms of both the original and modified image. Explain.
  8. \n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## EX12: segment from colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 12. Turn the sky red!
\n", "
    \n", "
  1. Crop a portion of the sky and analyze the histograms of the three bands.
  2. \n", "
  3. Create a mask to isolate the sky pixels.
    Use numpy's where() function with as many thresholds wanted, and assign 1 where conditions are True, 0 where conditions are false.
    EX: mask = np.where((R < 150) & (G > 100) & (B > 200), 1, 0)
    Plot the mask with a color bar to see if you are happy with the isolated pixels.
  4. \n", "
  5. Create new bands R_f, G_f and B_f, altering the pixel color to red (R=255, G=0, B=0) where mask=1.
    Use numpy's where() function to create these new bands.
  6. \n", "
  7. Plot the resulting image. Is your sky red?
  8. \n", "
\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }