Question
Basic edge detection using Python Tasks: 1. Make a file edgedetect_assgn.py , read the input image file and convert it to 8-bit grayscale format. In
Basic edge detection using Python Tasks:
1. Make a file edgedetect_assgn.py , read the input image file and convert it to 8-bit grayscale format. In this format, each pixel is represented using an integer in the range [0, 256).
2. Make a function named read_colorimg(). Add dummy pixels with a value of 0 along the image boundary, so that the 3x3 mask does not fall outside the actual image. Pad the image with boundary zeroes. Convert the pixel data to a list and return. The function returns a list of lists representing the pixel values. Let us name this 2D list pixel_values.
3. Using list comprehension, create a list of lists to store the updated pixel information. Let us name this new_pixel_values. The length of the list is numb_rows and the length of each sublist is numb_colns. Initialize all the values to 0.
4. Represent the values comprising the 3x3 image mask using a tuple of tuples. Let us name this 2D tuple mask. You can use the values in Mask 1 or Mask 2.
5. Use mask to calculate a new value for every pixel in the image, excluding the dummy pixels. Since pixel_values is a 2D data structure, use a nested for loop to iterate through every pixel. For each pixel, the following are the tasks that you have to implement:
(a) Call a function get_slice_2d_list() (to be implemented by you), which takes as input the pixel values, the position of the pixel being calculated, and returns the 3x3 patch of surrounding pixels as a list of lists. For example, when updating the pixel shown in Figure 2, calling this function from within the nested for loop should return neighbor_pixels = [[7, 5, 4], [3, 5, 3], [2, 1, 7]]. Use list slicing on pixel_values to extract the required neighboring pixels. Use list comprehension to create the 2D list neighbor_pixels.
(b) Call a function flatten() (to be implemented by you), which takes as input a 2D list or a 2D tuple, and returns a 1D list. For example, when this function is given [[7, 5, 4], [3, 5, 3], [2, 1, 7]] as input, it should return [7, 5, 4, 3, 5, 3, 2, 1, 7]. When it is given ((-1, -1, -1), (-1, 8, -1), (-1, -1, -1)), it returns [-1, -1, -1, -1, 8, -1, -1, -1, -1]. Using this function, flatten neighbor_pixels and mask. Use list comprehension in the flatten() function to create a 1D list from the 2D list.
(c) Apply the 1D list containing the mask values on the 1D list containing the neighborhood pixels using the map() function and a lambda. This lambda should multiply the corresponding elements of the two flattened lists. Your implementation should look as follows: map (lambda ..., list1, list2)
Note: This may not be the most e_cient way to perform element-wise multiplication of the 3x3 patch of pixels and the mask. However, it is important to learn how to use the map() function.
(d) Use the output of map() function and sum all the multiplication results to obtain the new value for the pixel. See the summation_sign in Figure 2.
(e) Set the new value by appropriately indexing new_pixel_values. Remember that the 2D list new_pixel_values does not has dummy pixels.
6. Make a function verify_result() to verify your result against the correct result (computed using the scipy package). If your computations are correct, "True result" will be printed on the console. Otherwise, it will output "False result".
7. Make a function view_images() to view the original image and the detected edges.
(a) Input image Figure 2: Illustration of edge detection using an example maskStep 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