from drawtool import DrawTool import numpy as np dt = DrawTool() dt.set_XY_range(0,10, 0,10) dt.set_aspect('equal') dt.set_axes_off() def is_approx_grey(r, g, b): d = 30 # Copy over your code for this function from neuro1.py def nongrey_to_blue(pixels): pixels2 = np.copy(pixels) # Copy over your code for this function from neuro1.py return pixels2 def to_grey(pixels, i, j): # We've written this for you. Notice how a potential float # is converted into an integer. return int ( (pixels[i,j,0] + pixels[i,j,1] + pixels[i,j,2])/3 ) def count_blue(pixels): # Write code to count the number of pure-blue pixels, that is, # pixels for which r=0, g=0, b=255. return num_blue def make_common(pixels1, pixels2, pixels3): # m is the minimum amongst # rows in each of the three images. # n is ithe minimum # columns amongst the three m = min(pixels1.shape[0], pixels2.shape[0], pixels3.shape[0]) n = min(pixels1.shape[1], pixels2.shape[1], pixels3.shape[1]) # We'll make a pixel array that's the minimal size pixels4 = dt.make_pixel_array(m, n) # For each i and j in pixels4, if at least 2 of the three images # have a blue pixel in the i,j location, set pixels4[i,j] to blue. # Otherwise, set it to a grey value that's the average of the # grey values of the three pixels at that location. For this # purpose, convert each r,g,b to grey and use that. Remember # to convert to an integer. return pixels4 def contrast(pixels1, pixels2): m = min(pixels1.shape[0], pixels2.shape[0]) n = min(pixels1.shape[1], pixels2.shape[1]) pixels3 = dt.make_pixel_array(m, n) # For every i,j pixel in pixels3: # if pixels1[i,j] and pixels2[i,j] are both blue # make pixels3[i,j] blue # Otherwise, if pixels1[i,j] is blue but pixels2[i,j] is not, then # set pixels3[i,j] to GREEN # Otherwise, if pixels2[i,j] is blue but pixels1[i,j] is not, then # set pixels3[i,j] to RED # If neither are blue, set pixels3[i,j] to the average of the # corresponding R,G,B values of pixels1[i,j] and pixels2[i,j] return pixels3 pixels1 = dt.read_imagefile('task1-subject1-sagittal.jpg') pixels1 = nongrey_to_blue(pixels1) pixels2 = dt.read_imagefile('task1-subject2-sagittal.jpg') pixels2 = nongrey_to_blue(pixels2) pixels3 = dt.read_imagefile('task1-subject3-sagittal.jpg') pixels3 = nongrey_to_blue(pixels3) # pixels_task1 will have the subject-averaged task1 image: pixels_task1 = make_common(pixels1, pixels2, pixels3) num_blue = count_blue(pixels_task1) print('Common volume for task 1 =', num_blue) # Should print 985 (approximately) # Note: on some Windows versions you get 993 pixels1 = dt.read_imagefile('task2-subject1-sagittal.jpg') pixels1 = nongrey_to_blue(pixels1) pixels2 = dt.read_imagefile('task2-subject2-sagittal.jpg') pixels2 = nongrey_to_blue(pixels2) pixels3 = dt.read_imagefile('task2-subject3-sagittal.jpg') pixels3 = nongrey_to_blue(pixels3) # pixels_task2 will have the subject-averaged task2 image: pixels_task2 = make_common(pixels1, pixels2, pixels3) num_blue = count_blue(pixels_task2) print('Common volume for task 2 =', num_blue) # Should print 705 (approximately) # The contrast() function should compute the contrast image: pixels_contrast = contrast(pixels_task1, pixels_task2) num_blue = count_blue(pixels_contrast) print('Common volume for tasks 1 and 2 =', num_blue) # Should print 121 (approximately) dt.draw_image(pixels_contrast) # NOTE: you can use dt.draw_image() to draw any of the earlier # images as you put together your module pdf. dt.display()