Question
#PYTHON# In this exercise you will write code which loads a collection of images (which are all the same size), computes a pixelwise average of
#PYTHON#
In this exercise you will write code which loads a collection of images (which are all the same size), computes a pixelwise average of the images, and displays the resulting average.
The images below give some examples that were generated by averaging "100 unique commemorative photographs culled from the internet" by Jason Salavon. Your program will do something similar.
Write a function in the cell below that loads in one of the sets of images and computes their average. You can use the os.listdir to get the list of files in the directory. As you load in the images, you should compute an average image on the fly. Color images are represented by a 3-dimensional array of size (HxWx3) where the third dimension indexes the red, green and blue channels. You will want to compute a running average of the red, green and blue slices in order to get your final average color image.
You should encapsulate your code in a function called average_imagethat takes the image directory as an input and returns the average of the images in that directory. Your function should implement some error checking. Specifically your function should skip over any files in the directory that are not images (plt.imread will thrown an OSError if the file is not an image). It should ignore images that are not color images. Finally, it should also skip any images which are not the same height and width as the first color image you load in.
In[]: # # these are the only modules needed for problem #2 import numpy as np import os import matplotlib.pyplot as plt In [: def average_image (dirname): Computes the average of all color images in a specified directory an The function ignores any images that are not color images and ignore the same size as the first color image you load in Parameters dirname: str Directory to search for images Returns numpy.array (dtype-float) HxWx3 array containing the average of the images found #[your code here] for file in os.listdir(dirname) filenameos.path.join(dirname,file) if os.path.isfile(filename): #[your code here] return Iaverage [2.2] Write code below which calls your average image0 function twice, once for each set of images Display the resulting average images. Also display a single example image from each set for comparison
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started