Question

In: Computer Science

Since images are two dimensional arrays of pixels, and we can represent a pixel as a...

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]]]

Solutions

Expert Solution

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


Related Solutions

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
For two dimensional particle in a box, how many electrons can we get at most in...
For two dimensional particle in a box, how many electrons can we get at most in the first energy level and why?
Build a two dimensional array out of the following three lists. The array will represent a...
Build a two dimensional array out of the following three lists. The array will represent a deck of cards. The values in dCardValues correspond to the card names in dCardNames. Note that when you make an array all data types must be the same. Apply dSuits to dCardValues and dCardNames by assigning a suit to each set of 13 elements. dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14'] dSuits = ["Clubs","Spades","Diamonds","Hearts"] Once assigned your two dimensional array should resemble this : 2...
Given a pixel location (x1, y1), we can calculate z_i for this point as (d- ax1-...
Given a pixel location (x1, y1), we can calculate z_i for this point as (d- ax1- by1)/c. This takes 2 multiplications and a division for each pixel. Suppose the z_i value for (x1, y1) is known, find an easier way to find the z_i for (x1+1, y1) and (x1, y1+1) (pixels bordering (x1, y1))? Does this lead to a more efficient way of calculating z_i for each pixel? Explain.
Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is...
Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is easier and more efficient? Why? Can be compared to any other well used language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT