Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this problem, you need to implement the three function bodies related to blurring an image: 1. get_pixel_at(pixel_grid, i,j) 2. average_of_surrounding(pixel_grid, i,j) 3. blur(pixel_grid) We

image text in transcribed
image text in transcribed
image text in transcribed
For this problem, you need to implement the three function bodies related to blurring an image: 1. get_pixel_at(pixel_grid, i,j) 2. average_of_surrounding(pixel_grid, i,j) 3. blur(pixel_grid) We suggest you implement them in that order. Below are some specifics about each of these functions. In particular, blur should call average_of_surrounding, and average_of_surrounding should call get_pixel_at. Note that the smallest possible pixel_grid is one that contains a single pixel (e.g. [ [1] ]). You can assume that you will never be given a pixel_grid smaller than this. get_pixel_at This function should return the pixel in pixel grid at row 1 and column j or 0 if there is no row i or no column j. This function can be done in about 46 lines of code, although it is fine if yours is slightly longer. The function test_get_pixel_at () can be used to test that you have implemented get_pixel_at correctly. test_get_pixel_at () uses the assert statement (described on slide 27 in the lecture slides about functions). Notice that there is already a commented out call to immediately after its definition. Un-comment the call to test_get_pixel_at(). The way our program is written, as soon as get_pixel_at and test_get_pixel_at have been defined, the function test_get_pixel_at () will be called. Try running your program as described above and pay close attention to notice if any test cases fail, which will look something like this: test_get_pixel_at () creates a test pixel grid and executes a sequence of assert statements. Assert statements check to make sure things that should be true actually are true, such as get_pixel_at(test_grid, 0,0)=1. If any of the assertions in fail, an error message will be printed. If you see such a message this means that you have not implemented get_pixel_at correctly. Look at the test_grid in test_get_pixel_at and the message printed to figure out what is wrong with your implementation of get_pixel_at . average_of_surrounding This function should return the average of the values of: the pixel at location i,j and the eight pixels surrounding it in the given pixel_grid. This is the algorithm described in the background section. In the code we have given you, we expect you to sum the nine pixels into the variable pixel_sum and divide it by 9 using truncating intesger division with the Python 3 11 operator. You should not change this line of the code; this is how we intend for the average to be calculated. You will probably need to add anywhere from 6-10 lines of code to this function. It can be done using loops; however, for this homework using loops is not required. The function test_average_of_surrounding is similar to test_get_pixel_at (). It can be used to do some basic checks to test that you have implemented average_of_surrounding correctly. Un-comment the call to test_average_of_surrounding() now. Try running your program as described above and pay close attention to notice if any test cases fail. If your tests pass, there will be no additional output in your terminal window. blur Given a (a rectangular grid of pixels), blur should return a new grid of pixels of the same size and shape. For each pixel in the given pixel grid, you should compute its average and store it in the same location in the new grid you are creating. Finally, you should return the new grid. You will probably need to add anywhere from 8-15 lines of code to this function. We strongly recommend starting with an empty list and appending new values to the list to create your new grid, as other approaches (e.g. copying) are likely to result in hard to track down bugs. We have given you a new list, blurry_grid, to start with. Here is a sample pixel_grid and the new blurred grid that should be returned: pixel_grid Here is a sample pixel_grid and the new blurred grid that should be returned: pixel_grid [[1,2,3],[4,5,6],[7,8,9]] new blurred grid [[1,2,1],[3,5,3],[2,4,3]] There is no test function provided for blur. You may write onetif you wish, using the two test functions above as models. Writing a test function for blur is not required but we encourage you to get in the practice of writing test functions. You can examine the output of your blurring algorithm visually after you have finished Problem 3 below. You won't know whether you have blurred the image exactly according to the algorithm until after you have finished Problem 4 and can compare the output of small blurred test_grids. After you have implemented blur and are convinced that get_pixel_at and average of surrounding are working properly, move on to Problem 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are some ways to infl uence an informal team?

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago