def edge_highlight(self, image):        """        Returns a new image with the edges highlighted         # Check output        >>> img_proc = PremiumImageProcessing()        >>> img = img_read_helper('img/test_image_32x32.png')        >>> img_edge = img_proc.edge_highlight(img)        >>> img_exp = img_read_helper('img/exp/test_image_32x32_edge.png')        >>> img_exp.pixels == img_edge.pixels # Check edge_highlight output        True        >>> img_save_helper('img/out/test_image_32x32_edge.png', img_edge)        """        # YOUR CODE GOES HERE #        kernel = [[-1, -2, -1],          [0, 0, 0],          [1, 2, 1]        ]        pixels = image.pixels        new_pixels = []        for y in range(len(pixels)):            new_row = []            for x in range(len(pixels[y])):                neighbors = []                for dy in range(-1, 2):                    for dx in range(-1, 2):                        nx = x + dx                        ny = y + dy                        if 0 <= nx < len(pixels[y]) and 0 <= ny < len(pixels):                            neighbors.append(pixels[ny][nx])                 new_r = sum([neighbors[i][j][0] * kernel[i][j] for i in range(3) for j in range(3)])                new_g = sum([neighbors[i][j][1] * kernel[i][j] for i in range(3) for j in range(3)])                new_b = sum([neighbors[i][j][2] * kernel[i][j] for i in range(3) for j in range(3)])                new_r = min(255, max(0, new_r))                new_g = min(255, max(0, new_g))                new_b = min(255, max(0, new_b))                new_row.append([new_r, new_g, new_b])            new_pixels.append(new_row)        return RGBImage(new_pixels) A method that highlights the edges in an image. To highlight edges, we will want to ignore all color data. Therefore, your first step should be to convert all pixels into single values. For the following RGB Image [  [[215, 209, 223], [108, 185, 135], [ 54, 135,  36]],  [[184,  20, 245], [ 32,  52, 249], [243, 155,  96]],  [[108,  66, 116], [ 65, 200,  34], [  2, 213, 238]]  ] This would become (use floor division when calculating average) [  [215, 142,  75],  [149, 111, 164],  [ 96,  99, 151]  ] The next step is to apply something called a kernel. It is a type of 2D array that acts like a mask, being applied to every pixel. It looks like this [  [-1, -1, -1],  [-1,  8, -1],  [-1, -1, -1]  ] To calculate the output for the pixel at row=0, col=0, you multiply all of the mask’s values with all of the pixel values, then add them together.  Since the mask is centered at the top left, you can ignore the weights outside of the image. The colored values are the relevant weights that we will use for this single operation. [  [-1, -1, -1],  [-1,  8, -1],  [-1, -1, -1]  ] Then, centered at 0,0 we multiply each weight by the relevant value [  [215 *  8, 142 * -1, …],  [149 * -1, 111 * -1, …],  [       …,        …, …]  ] masked_value = 215*8 + 142*-1 + 149*-1 + 111*-1 = 1318 -> 255 Note that the masked_value needs to be between 0 and 255 in your code. Next, we will show the calculation for the value at row=1, col=1. This will use the full mask. [  [215 * -1, 142 * -1,  75 * -1],  [149 * -1, 111 *  8, 164 * -1],  [ 96 * -1,  99 * -1, 151 * -1] ] masked_value = 215*-1 + 142*-1 + 75*-1 + 149*-1 + 111*8 + 164*-1 + 96*-1 + 99*-1 + 151*-1 = -203 -> 0 This is what the two calculations we have done would look like in the output matrix. Note that it is certainly possible to have values between 0 and 255 in the output, but that these examples did not yield that. [  [255,   …,   …],  [  …,   0,   …],  [  …,   …,   …]  ] Finally, you will want to convert this into an RGBImage object. Simply use the single intensity value for all 3 channels. [  [[255,255,255],             …,   …],  [            …, [  0,  0,  0],   …],  [            …,             …,   …]  ]      This process of applying a mask to all of the pixels in an image is called convolution, and is the technique that Convolutional Neural Networks use. no imports

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

    def edge_highlight(self, image):
        """
        Returns a new image with the edges highlighted

        # Check output
        >>> img_proc = PremiumImageProcessing()
        >>> img = img_read_helper('img/test_image_32x32.png')
        >>> img_edge = img_proc.edge_highlight(img)
        >>> img_exp = img_read_helper('img/exp/test_image_32x32_edge.png')
        >>> img_exp.pixels == img_edge.pixels # Check edge_highlight output
        True
        >>> img_save_helper('img/out/test_image_32x32_edge.png', img_edge)
        """
        # YOUR CODE GOES HERE #
        kernel = [[-1, -2, -1],
          [0, 0, 0],
          [1, 2, 1]
        ]
        pixels = image.pixels
        new_pixels = []
        for y in range(len(pixels)):
            new_row = []
            for x in range(len(pixels[y])):
                neighbors = []
                for dy in range(-1, 2):
                    for dx in range(-1, 2):
                        nx = x + dx
                        ny = y + dy
                        if 0 <= nx < len(pixels[y]) and 0 <= ny < len(pixels):
                            neighbors.append(pixels[ny][nx])

                new_r = sum([neighbors[i][j][0] * kernel[i][j] for i in range(3) for j in range(3)])
                new_g = sum([neighbors[i][j][1] * kernel[i][j] for i in range(3) for j in range(3)])
                new_b = sum([neighbors[i][j][2] * kernel[i][j] for i in range(3) for j in range(3)])
                new_r = min(255, max(0, new_r))
                new_g = min(255, max(0, new_g))
                new_b = min(255, max(0, new_b))
                new_row.append([new_r, new_g, new_b])
            new_pixels.append(new_row)
        return RGBImage(new_pixels)

A method that highlights the edges in an image.

To highlight edges, we will want to ignore all color data. Therefore, your first step should be to convert all pixels into single values.

For the following RGB Image

[

 [[215, 209, 223], [108, 185, 135], [ 54, 135,  36]],

 [[184,  20, 245], [ 32,  52, 249], [243, 155,  96]],

 [[108,  66, 116], [ 65, 200,  34], [  2, 213, 238]] 

]

This would become (use floor division when calculating average)

[

 [215, 142,  75],

 [149, 111, 164],

 [ 96,  99, 151] 

]

The next step is to apply something called a kernel. It is a type of 2D array that acts like a mask, being applied to every pixel. It looks like this

[

 [-1, -1, -1],

 [-1,  8, -1],

 [-1, -1, -1] 

]

To calculate the output for the pixel at row=0, col=0, you multiply all of the mask’s values with all of the pixel values, then add them together. 

Since the mask is centered at the top left, you can ignore the weights outside of the image. The colored values are the relevant weights that we will use for this single operation.

[

 [-1, -1, -1],

 [-1,  8, -1],

 [-1, -1, -1] 

]

Then, centered at 0,0 we multiply each weight by the relevant value

[

 [215 *  8, 142 * -1, …],

 [149 * -1, 111 * -1, …],

 [       …,        …, …] 

]

masked_value = 215*8 + 142*-1 + 149*-1 + 111*-1 = 1318 -> 255

Note that the masked_value needs to be between 0 and 255 in your code.

Next, we will show the calculation for the value at row=1, col=1. This will use the full mask.

[

 [215 * -1, 142 * -1,  75 * -1],

 [149 * -1, 111 *  8, 164 * -1],

 [ 96 * -1,  99 * -1, 151 * -1]

]

masked_value = 215*-1 + 142*-1 + 75*-1 + 149*-1 + 111*8 + 164*-1 + 96*-1 + 99*-1 + 151*-1 = -203 -> 0

This is what the two calculations we have done would look like in the output matrix. Note that it is certainly possible to have values between 0 and 255 in the output, but that these examples did not yield that.

[

 [255,   …,   …],

 [  …,   0,   …],

 [  …,   …,   …] 

]

Finally, you will want to convert this into an RGBImage object. Simply use the single intensity value for all 3 channels.

[

 [[255,255,255],             …,   …],

 [            …, [  0,  0,  0],   …],

 [            …,             …,   …] 

]     

This process of applying a mask to all of the pixels in an image is called convolution, and is the technique that Convolutional Neural Networks use.

no imports

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Program on Numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education