Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Don\'t use PIL, simpy manipulate the arrays of pixels We can read and display im

ID: 3680891 • Letter: D

Question

Don't use PIL, simpy manipulate the arrays of pixels

We can read and display images in Python as follows. import numpy as np import scipy.misc as sm import matplotlib.pyplot as plt from matplotlib.pyplot import figure,imshow,gray, title u=sm. imread('barbara_512.png', 1) figure(); imshow(u); gray(); title('Barbara'); plt.show() In image processing we can think of the image u as a function of two variables u(x,y) defined on some region Ohms elements of R^2. While implementing some algorithms, one has to maintain proper boundary conditions. For example, Neumann boundary condition implies that Vu n = 0 on the boundary partial derivative Ohms, where n is the outward pointing unit normal to the boundary. One way to achieve this is to extend the boundary by some factor. For example, if u is a 512 times 512 image, we obtain another image U with size 514 times 514. The image U is obtained expanding the image u by 1 pixel unit on all four sides, and the expansion is done by repeating the boundary values, i.e. the image U has top two rows same as the first row of u, bottom two rows same as the bottom row of u, and so one. Sometimes we need reflecting boundary conditions, while maintaining the Neumann condition at the boundary. This is achieved by first expanding the image by 1 pixel on all sides. Then reflecting the interior (n-1) pixels, outside this boundary. Write a Python code reflect(u, n) to do this.

Explanation / Answer

#!/usr/bin/python

import numpy as np
import time

reflect(u, n):
   p = np.random,randint(1,6, (499, 499))
   zero = 0
   full = 255

   blackColor = [zero, zero, zero]
   whiteColor = [full, full, full]
   RedColor   = [zero,zero,full]
   greenColor = [zero, full, zero]
   blueColor = [full, zero, zero]

   rows, columns = p.shape

   q = np.zeros((rows, columns, 3), np.unit8)

   for a in xrange(rows):
       for b in xrange(columns):
           c = p[a,b]
           c = p.item(a,b)
           if c == 0:
               q[a,b] = greenColor
               q1.itemset((a,b,0), greenColor[0])
               q1.itemset((a,b,1), greenColor[1])
               q1.itemset((a,b,2), greenColor[2])

           elif c == 1:
               q[a,b] = blueColor
               q1.itemset((a,b,0), blueColor[0])
               q1.itemset((a,b,1), blueColor[1])
               q1.itemset((a,b,2), blueColor[2])

           elif c == 2:
               q[a,b] = redColor
               q1.itemset((a,b,0), redColor[0])
               q1.itemset((a,b,1), redColor[1])
               q1.itemset((a,b,2), redColor[2])

           elif c == 3:
               q[a,b] = whiteColor
           else:
               q[a,b] = blackColor

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote