Question: import matplotlib.pyplot as plt import numpy as np def main ( ) : im = loadImage ( ' 3 0 0 _ 2 6 a

import matplotlib.pyplot as plt
import numpy as np
def main():
im = loadImage('300_26a_big-vlt-s.jpg')
(im,Dphi)= opticalSystem(im,300)
images = gerchbergSaxton(im,10,Dphi)
saveFrames(images)
def loadImage(name):
im = plt.imread(name)/255
if len(im.shape)>2:
im =(im[:,:,0]+im[:,:,1]+im[:,:,2])/3
im[im <0]=0
im[im >1]=1
return im
def opticalSystem(im,width):
im = occultSquare(im,width)
(IMa,IMp)= dft2(im)
rng = np.random.default_rng(12345)
imR = rng.random(im.shape)
(_,Dphi)= dft2(imR)
im = idft2(IMa,IMp-Dphi)
return (im,Dphi)
def occultSquare(im,width):
h, w = im.shape
center_h = h //2
center_w = w //2
half_width = width //2
im[center_h - half_width:center_h + half_width, center_w - half_width:center_w + half_width]=0
return im
def dft2(im):
IM = np.fft.rfft2(im)
IMa = np.abs(IM)
IMp = np.angle(IM)
return (IMa,IMp)
def idft2(IMa,IMp):
IM = IMa *(np.cos(IMp)+1j * np.sin(IMp))
im = np.fft.irfft2(IM)
im[im <0]=0
im[im >1]=1
return im
def gerchbergSaxton(im,maxIters,Dphi):
(IMa,IMp)= dft2(im)
images =[]
for k in range(maxIters+1):
print("Iteration %d of %d"%(k,maxIters))
if k ==0:
corrected_IMp = IMp
elif k == maxIters:
corrected_IMp = IMp + Dphi
else:
corrected_IMp = IMp +(k / maxIters)* Dphi # Linear interpolation
im = idft2(IMa, corrected_IMp)
images.append(im)
return images
def saveFrames(images):
shape =(images[0].shape[0], images[0].shape[1],3)
image = np.zeros(shape, images[0].dtype)
maxIters = len(images)-1
for k in range(maxIters+1):
image[:, :, :]= images[k][:, :, np.newaxis] # Equal parts of red, green, and blue
plt.imshow(image)
plt.title(f'Coronagraph Iteration: {k}') # Adjusted title
plt.axis('off') # Hide axes
plt.savefig(f'coronagraph{k}.png')
plt.show()
main() this is my python code: please make the follwoing changes in my python code: In the saveFrames function, insert plt.subplot(1,2,1) and plt.subplot(1,2,2) statements so that images are shown on the right side only. Rename the occultSquare function to occultCircle, both where it is defined and also where it is invoked. Revise its definition so that instead of blacking out, or occulting, a square of the specified width, the function occults a circle with that diameter, or width.
The occulted group of pixels must be at the centre of the image. Verify that your program runs.Modify the occultCircle function to return a second argument, mask, that is a 2D NumPy array of boolean entries. Initialize it, using np.full, to have the same shape as the first input argument, im, but with the value False. For every entry of im you occult, assign the corresponding entry of mask to True. Modify the invocation in opticalSystem of occultCircle so that its output arguments are assigned to two variables, the first of which, im, is the same as before. Verify that your program runs.Modify opticalSystem to return a third argument, mask, the mask obtained from occultCircle. Modify main to accept the third output argument from opticalSystem and to pass it as a fourth input argument to gerchbergSaxton. Modify gerchbergSaxton to accept a fourth input argument, mask, and to return a second output argument, errors, initialized to an empty list. Modify saveFrames to
accept a second input argument, errors. So that you can verify your program runs, modify main to accept the errors argument from gerchbergSaxton and to pass it on to saveFrames.
In gerchbergSaxton, compute a float, error, from the result, im, of each idft2 invocation and the
fourth input argument, mask. Append each such float, error, to the list, errors, that the function
returns. The error is the sum total of the squared values of im entries where corresponding mask
entries are True. Outsource computation to a function, occultError, with two input arguments and one output argument, that you define and invoke appropriately. Verify that your program runs.Examine the computed list, errors, with the Variable Explorer using a breakpoint inside saveFrames.
Expected values can be read approximately from the coronagraph_v2a\*.png files. Compute the maximum, maxErrors, of errors within saveFrames. Without altering the right-side image, make and annotate the left-side plot as indicated in the given .png files. Verify your program works.Examine the coronagraph_v2b\*.png files. To meet the implied requirements, modify saveFrames. Remove all plt.subplot(.) and any plt.axis('off') statements. Plot the graph first with correct limits. Using a plt.imshow statement with an extent=(.) argument, where you determine the (.)expression, add the image to the graph (plotted lines will show). Use plt.gca().set_aspect(?), where you must compute from maxIters and maxErrors, so image is crrct.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!