Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Just need the code for the function __init__() and detect_motion() with the exact parameters given. It needs to work properly to pass the tests given.
Just need the code for the function __init__() and detect_motion() with the exact parameters given. It needs to work properly to pass the tests given.
Question 3: Build the class MotionDetection. 1 ### Question 3: Implement the "MotionDetection class 2 3 class MotionDetection (object): 4 5 def _init__(self, num_sigmas-4., discount=0.96): 6 *****Motion detection implemented via averagerator. 7 @param num_sigmas: by how many standard deviations should a pixel 8 differ from the average for motion to be detected. This is 9 the kappa of the above explanation. 18 @param discount: discount factor for the averagerator. 11 12 13 14 15 16 ### YOUR CODE HERE I def detect_motion(self, img): "Detects motion. @param img: anh x W X 3 image. @returns: an h x w boolean matrix, indicating where motion occurred. A pixel is considered a motion pixel if one of its color bands deviates by more than num_sigmas standard deviations from the average. ### YOUR CODE HERE 17 18 19 20 21 Let's write a detect_motion function to facilitate our experiments. It will take a list of images, and compute the motion detection of each. If the motion detection contains more than 500 motion pixels, it puts the detection, and the index of the image, into a list of results. 4 1 def detect_motion(image_list, num_sigmas=4., discount=0.96): 2. **"Takes as input: 3 @param image_list: a list of images, all of the same size. @param num_sigmas: a parameter specifying how many standard deviations a 5 pixel should be to count as detected motion. 6 @param discount: the discount factor for the averagerator. 7 8 detector - MotionDetection (num_sigmas=num_sigmas, discount-discount) 9 detected motion = [] 10 for i, img in enumerate(image_list): 11 motion = detector detect_motion(img) 12 if np. sum(motion) > 500: 13 detected_motion.append((i, motion)) 14 return detected_motion 15 [] 1 # Compute the motion detections. 2 motions = detect_motion(images_as_arrays[:60]) 3 We can finally visualize the detected motions. [] ") 1 import matplotlib.pyplot as plt 2 for i, min motions: 3 # We only print images where there are at least 500 pixels of motion. 4 if np.sum(m) > 500: 5 print("Motion at image", i, ",", np. sum(m), 6 # We first show the image, for reference. 7 plt.imshow(images_as_arrays[i] / 255) 8 plt.show() 9 # And then the motion detection, 10 plt.imshow (m) 11 plt.show() 12 Here are some final tests. 1 ### 15 points: Tests for motion detection 2 3 motions = detect_motion(images_as_arrays[:60]) 4 motion idxs [i for i, in motions] 5 assert motion_idxs [1, 10, 47, 48, 49, 57, 58, 59] 6 assert np. sum(motions [6][1]) 1199 7 == 8 We can see that the motion detection does a very reasonable job of detecting the passing cars and bycicle, while almost entirely suppressing the tree that is shaking in the wind. As the tree shaking is constant, its pixels have very high standard deviation. If we plot the standard deviation of each pixel, we get a very good impression of how much noise or regular motion took place at that pixel. 1 a = DiscountedAveragerator(0.96) 2 for i, img in enumerate(images_as_arrays): 3 a.add(img) 4 # We display the final sigma. 5 sigma = np.max(a.std, axis=2) 6 plt.imshow(sigma, cmap='gnuplot') 7 plt.colorbar 8 plt.show 9 # Let's compare with the last image. 10 plt.imshow(images_as_arrays [-1] / 255) 11 plt.show() 12. We see how the constantly-shaking trees give rise by far to the greatest pixel standard deviationStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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