{"nbformat":4,"nbformat_minor":0,"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.7.3"},"colab":{"name":"Copy of imageprocessing.ipynb","provenance":[{"file_id":"1qcVpg4K-d2JF1LvUDWmgLtO7VsHNHE-S","timestamp":1595440397698}],"collapsed_sections":[]}},"cells":[{"cell_type":"code","metadata":{"id":"SLViiPTx2emR","colab_type":"code","colab":{}},"source":["import matplotlib.pyplot as plt # For plotting\n","import numpy as np # For the linear algebra\n","\n","%matplotlib inline\n","\n","from scipy.misc import face"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"YtMzgNOzE1yF","colab_type":"text"},"source":["### Note to instructor:\n","\n","These python notebooks can be used in one of two ways: \n","* If students don't know Python, you could clear the outputs from all the cells (View->Clear all outputs) and walk through each cell and discuss the outputs with the class.\n","* If student's know Python, you can delete the appropriate code and ask students to fill in."]},{"cell_type":"markdown","metadata":{"id":"j9DUC11a2emV","colab_type":"text"},"source":["### Viewing images as matrices"]},{"cell_type":"code","metadata":{"id":"53otULWn2emV","colab_type":"code","colab":{}},"source":["w = 255\n","b = 0\n","\n","A = np.array([ [w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,b,b,b,w,w,w,w],[w,w,w,w,b,b,b,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,b,b,b,b,b,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w], [w,w,w,w,b,w,b,w,w,w,w], [w,w,w,b,w,w,w,b,w,w,w], [w,w,b,w,w,w,w,w,b,w,w] ])\n","print('The size of the image is ', A.shape)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"KGxKjQXFHK1X","colab_type":"code","colab":{}},"source":["print('The matrix A has entries \\n', A)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"FPJLFEoNDmgF","colab_type":"code","colab":{}},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"v8jL4_aPDnYH","colab_type":"text"},"source":["Exercise: Based on the entries of the matrix, on a piece of paper, sketch out the hidden object. Can you describe it?"]},{"cell_type":"code","metadata":{"id":"cI0w0Xgk2emb","colab_type":"code","colab":{}},"source":["plt.imshow(A, cmap ='gray',vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"UaDtuyvV3okU","colab_type":"text"},"source":["### Transpose\n","\n","The next command will plot the transpose of a matrix.\n","\n","\n","Exercise: Can you describe the transpose of the matrix in relation to the image? "]},{"cell_type":"code","metadata":{"id":"DuvQZ1Ie3HvP","colab_type":"code","colab":{}},"source":["plt.imshow(A.T, cmap=plt.cm.gray,vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"gpQ0FY7t356W","colab_type":"text"},"source":["### Inverting the colors\n","\n","Inverting the colors means making darker colors light and vice versa. Mathematically this can be accomplished by the formula\n","\n","$$ 255*I - A ,$$\n","\n","where $I$ is the matrix of all ones.\n","\n","Exercise: Describe the image when we invert the colors. "]},{"cell_type":"code","metadata":{"id":"hTrYr3KY3YW8","colab_type":"code","colab":{}},"source":["plt.imshow(255-A,cmap=plt.cm.gray,vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"cLJZ_m7QGcaG","colab_type":"code","colab":{}},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"9yLyl4cdGdXZ","colab_type":"text"},"source":["### Brightening/dimming the colors\n","\n","In this next example, we divide the entries of $A$ by $5.$ Can you guess what happens to the corresponding image?"]},{"cell_type":"code","metadata":{"id":"kasIrHre2emj","colab_type":"code","cellView":"form","colab":{}},"source":["#@title\n","\n","plt.imshow(A/5,cmap = 'gray', vmin=0,vmax=255)\n","plt.colorbar()"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"15_pLXgW2emm","colab_type":"code","colab":{}},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"bClh7_Yt2emu","colab_type":"text"},"source":["### Adding and subtracting two images\n","\n","\n","We first create a simple image which can also be represented as a $10 \\times 10$ matrix and plot it to see how it looks."]},{"cell_type":"code","metadata":{"id":"r1n4UIbp2emv","colab_type":"code","colab":{}},"source":[" # Second image\n","B = np.array([ [w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,b,b,w],[w,w,w,w,w,w,w,w,b,b,w],[w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,w,w,w],[w,b,b,w,w,w,w,w,w,w,w],[w,b,b,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w] ])"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"QFu360aVG4t5","colab_type":"code","colab":{}},"source":["plt.imshow(B, cmap = 'gray',vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"HyoN3uKfNY14","colab_type":"text"},"source":["Exercise: describe the image obtained by averaging A and B. That is: $\\frac{1}{2}(A+B)$."]},{"cell_type":"code","metadata":{"id":"T4rCCglLHphx","colab_type":"code","colab":{}},"source":["plt.imshow(0.5*(A+B), cmap = 'gray', vmin=0,vmax=255)\n","plt.colorbar()"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"nsrot8RuIZn4","colab_type":"code","colab":{}},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"NHlxu7P1IbVa","colab_type":"text"},"source":["### Reflecting the image\n","\n","\n","In the lesson we also looked at reflections across the x-axis and reflections across the y-axis. How does this work in the example with our human?\n","\n","First recall that reflection can be performed using the \"reverse\" identity matrix. We first compute the $11 \\times 11$ \"reverse\" identity matrix $P$."]},{"cell_type":"code","metadata":{"id":"rvfJMs9kMUkP","colab_type":"code","colab":{}},"source":["P = np.eye(11)\n","P = P[::-1,:] ## Reverses the rows of the permutation matrix\n","print('The reverse identity matrix is \\n ', P)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"oO3UhmXPrfPa","colab_type":"text"},"source":["\n","We learned from our guided notes that it matters which matrix is on the left and which one is on the right. So in this case if you multiplied matrix $A$ by the “reverse identity matrix” $P$ from the left\n","$$ R_1 = P A$$ \n","\n"," and then repeated it on the right \n"," \n"," $$ R_2 = A P,$$ \n"," what would you expect to happen in each case. Be specific about your hypothesis and then run the Python program to check your guess."]},{"cell_type":"code","metadata":{"id":"e1-t38czMnKE","colab_type":"code","colab":{}},"source":["plt.imshow(P @ A, cmap = 'gray', vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"ffHzedDsMun6","colab_type":"code","colab":{}},"source":["plt.imshow(A @ P, cmap = 'gray', vmin=0,vmax=255)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"b33WnGTMsyJb","colab_type":"text"},"source":["### Blurring \n","\n","Finally, we looked at blurring in this lesson as an application of averaging the entries directly around each entry. This is very relevant in the world of photography look at this example again by watching it in Python.\n"]},{"cell_type":"code","metadata":{"id":"_jGx2NY6sYta","colab_type":"code","colab":{}},"source":["F = (1/9)*np.ones((3,3))\n","from scipy.signal import convolve2d\n","\n","B = convolve2d(A,F,mode='same')\n","plt.imshow(B, cmap='gray', vmin=0,vmax=255)"],"execution_count":null,"outputs":[]}]}