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

Do not use PIL, do array manipulations. Take the arrays on the left and move to

ID: 3680894 • Letter: D

Question

Do not use PIL, do array manipulations. Take the arrays on the left and move to the right, etc.

We can read and display images in Python as follows. In image processing we can think of the image u as a function of two variables u(x, y) defined on some region Ohm element of R^2. While implementing some algorithms, one has to maintain proper boundary conditions. For example, Neumann boundary condition implies that delta u multiplication dot n = 0 on the boundary partial derivative Ohm, 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

OpenCV comes with functions for reading and writing images as well as matrix operations and math libraries.

First, let’s load the image and display it on screen:

Loading and Displaying an ImagePython

# import the necessary packages

import cv2

# load the image and show it

image = cv2.imread("jurassic-park-tour-jeep.jpg")

cv2.imshow("original", image)

cv2.waitKey(0)

Resizing an image and open cv

# we need to keep in mind aspect ratio so the image does

# not look skewed or distorted -- therefore, we calculate

# the ratio of the new image to the old image

r = 100.0 / image.shape[1]

dim = (100, int(image.shape[0] * r))

# perform the actual resizing of the image and show it

resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

cv2.imshow("resized", resized)

cv2.waitKey(0)

You could use np.repeat along both axes of the 3x3 img array:

>>> img.repeat(2, axis=0).repeat(2, axis=1)

array([[0, 0, 0, 0, 0, 0],

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

       [0, 0, 1, 1, 0, 0],

       [0, 0, 1, 1, 0, 0],

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

       [0, 0, 0, 0, 0, 0]])

Another way is to calculate the Kronecker product of img and an array of the appropriate shape filled with ones:

>>> np.kron(img, np.ones((2,2)))

array([[ 0., 0., 0., 0., 0., 0.],

       [ 0., 0., 0., 0., 0., 0.],

       [ 0., 0., 1., 1., 0., 0.],

       [ 0., 0., 1., 1., 0., 0.],

       [ 0., 0., 0., 0., 0., 0.],

       [ 0., 0., 0., 0., 0., 0.]])

note-the above code can be useful for the given question.

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