import sys import os import numpy as np import cv2 def split_rgb(image): '''Split the target image into its red, green and blue channels. image - a numpy array of shape (rows, columns, 3). output - three numpy arrays of shape (rows, columns) and dtype same as image, containing the corresponding channels. Please make sure the output shape has only 2 components! For instance, (600, 800) instead of (600, 800, 1) ''' red = None green = None blue = None # Insert your code here.---------------------------------------------------- blue = image[:, :, 0] green = image[:, :, 1] red = image[:, :, 2] #--------------------------------------------------------------------------- return red, green, blue def main(): ''' This function applies your split script to images. It will search through the images/part0 subfolder, and apply your splitting function to each one. It will then save the resulting images. ''' imagesfolder0 = os.path.abspath(os.path.join(os.curdir, 'images', 'part0')) print '''Searching for images in {} folder (will ignore red, green, or blue in the name)'''.format(imagesfolder0) exts = ['.bmp', '.pbm', '.pgm', '.ppm', '.sr', '.ras', '.jpeg', '.jpg', '.jpe', '.jp2', '.tiff', '.tif', '.png'] for dirname, dirnames, filenames in os.walk(imagesfolder0): for filename in filenames: name, ext = os.path.splitext(filename) if ext in exts and 'red' not in name and 'green' not in name and \ 'blue' not in name: print "Splitting image {}.".format(filename) img = cv2.imread(os.path.join(dirname, filename)) red, green, blue = split_rgb(img) for values, color, channel in zip((red, green, blue), ('red', 'green', 'blue'), (2,1,0)): img = np.zeros((values.shape[0], values.shape[1], 3), dtype = values.dtype) img[:,:,channel] = values print "Writing image {}.".format(name+color+ext) cv2.imwrite(os.path.join(dirname, name+color+ext), img) def test(): '''This script will perform a unit test on your function, and provide useful output. ''' x = (np.random.rand(4,4,3) * 255).astype(np.uint8) if __name__ == "__main__": print "Input:\n{}".format(x) usr_red, usr_green, usr_blue = split_rgb(x) true_red = x[:,:,2] true_green = x[:,:,1] true_blue = x[:,:,0] for usr_out, true_out, name in zip((usr_red, usr_green, usr_blue), (true_red, true_green, true_blue), ('red', 'green', 'blue')): if usr_out == None: if __name__ == "__main__": print "Error- {} has value None.".format(name) return False if not usr_out.shape == true_out.shape: if __name__ == "__main__": print "Error- {} has shape {}. Expected shape is {}.".format(name, usr_out.shape, true_out.shape) return False if not usr_out.dtype == true_out.dtype: if __name__ == "__main__": print "Error- {} has dtype {}. Expected dtype is {}.".format(name, usr_out.dtype, true_out.dtype) return False if not np.all(usr_out == true_out): if __name__ == "__main__": print "Error- {} has value:\n{}\nExpected value:\n{}".format(name, usr_out, true_out) return False if __name__ == "__main__": print "Success - all outputs correct." return True if __name__ == "__main__": # Testing code print "Performing unit test." t = test() print "Unit test: {}".format(t) if t: main()
import sys, os import numpy as np import cv2 def interlace(evens, odds): '''Reconstruct the image by alternating rows of evens and odds. evens - a numpy array of shape (rows, columns, 3) containing the even rows of the output image. odds - a numpy array of shape (rows, columns, 3) containing the odd rows of the output image. This function should return an image. Row 0 of the output image should be row 0 of evens. Row 1 of the output image should be row 0 of odds. Then row 1 of evens, then row 1 of odds, and so on. The resulting image will have as many rows as image 1 and 2 combined, equal to both in number of columns, and have 3 channels. ''' outimg = None # Implement your function here --------------------------------------------- size = evens.shape outimg = np.zeros( (size[0]*2, size[1], size[2]), 'uint8' ) for i in range(size[0]): outimg[2*i,:, :] = evens[i, :, :] outimg[2*i + 1,:, :] = odds[i, :, :] #--------------------------------------------------------------------------- return outimg def main(): ''' This code will attempt to reconstruct the images in the images/part1 folder, and save the output. ''' imagesfolder = os.path.join('images', 'part1') print "part 1 : attempting to interlace images evens.jpg and odds.jpg" evens = cv2.imread(os.path.join(imagesfolder, 'even.jpg')) odds = cv2.imread(os.path.join(imagesfolder, 'odd.jpg')) if evens == None or odds == None: print "Error - could not find even.jpg and odd.jpg in {}".format(imagesfolder) sys.exit(0) together = interlace(evens, odds) if not together == None: cv2.imwrite(os.path.join(imagesfolder, 'together.jpg'), together) def test(): '''This script will perform a unit test on your function, and provide useful output. ''' x = (np.random.rand(4,4,3) * 255).astype(np.uint8) xeven = x[np.arange(0,x.shape[0], 2), :, :] xodd = x[np.arange(1,x.shape[0], 2), :, :] if __name__== "__main__": print "Input:\n even:\n{}\n odd:\n{}".format(xeven, xodd) usr_out = interlace(xeven, xodd) if usr_out == None: if __name__ == "__main__": print "Error- output has value None." return False if not usr_out.shape == x.shape: if __name__ == "__main__": print "Error- output has shape {}. Expected shape is {}.".format( usr_out.shape, x.shape) return False if not usr_out.dtype == x.dtype: if __name__ == "__main__": print "Error- output has dtype {}. Expected dtype is {}.".format( usr_out.dtype, x.dtype) return False if not np.all(usr_out == x): if __name__ == "__main__": print "Error- output has value:\n{}\nExpected value:\n{}".format( usr_out, x) return False if __name__ == "__main__": print "Success - all outputs correct." return True if __name__ == "__main__": # Testing code t = test() print "Unit test: {}".format(t) if t: main()
import sys import os import numpy as np import cv2 def greyscale(image): '''Convert an image to greyscale. image - a numpy array of shape (rows, columns, 3). output - a numpy array of shape (rows, columns) and dtype same as image, containing the average of image's 3 channels. Please make sure the output shape has only 2 components! For instance, (512, 512) instead of (512, 512, 1) ''' output = None # Insert your code here.---------------------------------------------------- output = (image.sum(2)/3).astype(np.uint8) #--------------------------------------------------------------------------- return output def main(): '''Convert images to greyscale. It will search through the images/part2 subfolder, and apply your function to each one, saving the output image in the same folder. ''' imagesfolder = os.path.join('images', 'part2') print '''part 2 : Searching for images in {} folder (will ignore if grey in the name)'''.format(imagesfolder) exts = ['.bmp', '.pbm', '.pgm', '.ppm', '.sr', '.ras', '.jpeg', '.jpg', '.jpe', '.jp2', '.tiff', '.tif', '.png'] for dirname, dirnames, filenames in os.walk(imagesfolder): for filename in filenames: name, ext = os.path.splitext(filename) if ext in exts and 'grey' not in name: print "Attempting to split image {}.".format(filename) img = cv2.imread(os.path.join(dirname, filename)) grey = greyscale(img) print "Writing image {}".format(name+"grey"+ext) cv2.imwrite(os.path.join(dirname, name+"grey"+ext), grey) def test(): '''This script will perform a unit test on your function, and provide useful output. ''' x = (np.random.rand(4,4,3) * 255).astype(np.uint8) if __name__ == "__main__": print "Input:\n{}".format(x) usr_grey = greyscale(x) true_grey = (x.sum(2)/3).astype(np.uint8) if usr_grey == None: if __name__ == "__main__": print "Error- output has value None." return False if not usr_grey.shape == true_grey.shape: if __name__ == "__main__": print "Error- output has shape {}. Expected shape is {}.".format( usr_grey.shape, true_grey.shape) return False if not usr_grey.dtype == true_grey.dtype: if __name__ == "__main__": print "Error- output has dtype {}. Expected dtype is {}.".format( usr_grey.dtype, true_grey.dtype) return False if not np.all(usr_grey == true_grey): if __name__ == "__main__": print "Error- output has value:\n{}\nExpected value:\n{}".format( usr_grey, true_grey) return False if __name__ == "__main__": print "Success - all outputs correct." return True if __name__ == "__main__": # Testing code t = test() print "Unit test - {}".format(t) if t: main()