In: Computer Science
Since images are two dimensional arrays of pixels, and we can represent a pixel as a one-dimensional list of three integers, we can represent an image as a three dimensional matrix (that is, a list of lists of lists) of integers in Python. In the following problems, you will be manipulating matrices of this format in order to alter image files in various ways.
Part 4: Edge Detection Filter
We want to highlight pixels that are significantly brighter (in at least one of the color components) than their surrounding pixels. A sudden shift in brightness like this is bound to occur along any sharp visible edge. To accomplish this, we’ll take each pixel, multiply its color values by 8, and then subtract the color values of the 8 pixels that surround it on the grid. Pixels that are very similar to their surrounding pixels will then end up with a color close to black, since 8 times the color component of that pixel minus the color components of its 8 neighbors should end up around 0.
This means that for a pixel located at (x,y) in the original image with original red color value r(x,y), you can compute the red component for the pixel the new image r’(x,y) with the following formula:
r’(x,y) = 8*r(x,y) - r(x-1,y-1) - r(x,y-1) - r(x+1,y-1) - r(x-1,y) - r(x+1,y) - r(x-1,y+1) - r(x,y+1) - r(x+1,y+1)
A similar formula applies for the green and blue components. Note that this may produce a value that is above the maximum color component value of 255: if this occurs then you should just set the value to 255. Similarly, if the formula generates a negative number, just set the value to 0.
For pixels along the boundaries of the picture that do not have 8 neighbors, ignore the formula and set their pixel values to black ([0, 0, 0]), to avoid the issue of going out of bounds.
Constraints:
Do not import/use any Python modules except for copy (see part C hints)
Do not change the function names or arguments.
Your submission should have no code outside of the function definitions (comments are fine).
You are permitted to write helper functions outside of the ones provided in the template..
Examples:
edge_detect([[[0,0,10], [0,0,20], [0,0,30], [0,0,200]],
[[255,120,20], [70,120,30], [0,120,40], [0,0,60]],
[[0,120,30], [255,120,40], [0,120,60], [0,120,50]],
[[255,120,40], [255,120,60], [255,120,50], [255,120,40]]])
Output:
[[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [50, 255, 0], [0, 255, 0], [0, 0, 0]],
[[0, 0, 0], [255, 0, 0], [0, 120, 110], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]
Code has been explained in the comments iself
Please refer to screenshots for the indentation
Please comment before any downvote , will resolve the issue asap
Code
import copy
def edge_detect(pixels):
   output = copy.deepcopy(pixels)
   rows, columns = len(pixels), len(pixels[0])
   print(rows,columns)
   print("func called")
   # print(pixels)
   for i in range(0,rows):   
       # print(i)
       for j in range(0,columns):
           red_neighbours =
0
           green_neighbours
= 0
           blue_neighbours
= 0
           if i==0 or j==0
or i==rows-1 or j==columns-1: #checking boundary elements
          
    output[i][j] = [0,0,0]  
           
        # set oundary elements to
zero
          
    continue
          
           for r in
range(i-1,i+2):          
            # negative
sum of 8x8 mask of element
          
    for c in range(j-1,j+2):
          
        red_neighbours -=
pixels[r][c][0]
          
        green_neighbours -=
pixels[r][c][1]
          
        blue_neighbours -=
pixels[r][c][2]
          
          
           red_neighbours
+= pixels[i][j][0] # add the center pixels to get neighbour
negative sum
           green_neighbours
+= pixels[i][j][1]
           blue_neighbours
+= pixels[i][j][2]
          
           red_neighbours
+= 8*pixels[i][j][0]       
    # add 8 times centre pixel from formula
           green_neighbours
+= 8*pixels[i][j][1]
           blue_neighbours
+= 8*pixels[i][j][2]
          
           red_neighbours =
min(red_neighbours,255) # clip pixel to 255 max
           red_neighbours =
max(red_neighbours,0)      
    #clip pixel to 0 min
          
           green_neighbours
= min(green_neighbours,255)
           green_neighbours
= max(green_neighbours,0)
          
           blue_neighbours
= min(blue_neighbours,255)
           blue_neighbours
= max(blue_neighbours,0)
          
           output[i][j][0]
= red_neighbours # copy ans to output
           output[i][j][1]
= green_neighbours
           output[i][j][2]
= blue_neighbours
          
   print(output)      
   return output
          
   
          
   
edge_detect([[[0,0,10], [0,0,20], [0,0,30], [0,0,200]],
       [[255,120,20], [70,120,30],
[0,120,40], [0,0,60]],
[[0,120,30], [255,120,40], [0,120,60], [0,120,50]],
[[255,120,40], [255,120,60], [255,120,50], [255,120,40]]])
  
Code ScreenShot

output Screenshot
