Name Error: name 'img' is not defined

Can someone help me fix this Name error please. It is in line 7
Name error: name ‘img’ is not defined

import cv2
import numpy as np
from matplotlib import pyplot as plt
import scipy.fftpack as fp

#do dft saving as complex output
dft=np.fft.fft2(img, axes=(0,1))

#apply shift of origin to center of image
dft_shift=np.fft.fftshift(dft)

#generate spectrum from magnitude image (for viewing only)

mag=np.ans(dft_shift)
spec=np.log(mag)/20

#create white circle mask on black background an invert so black circle on white background (Low pass)

radius=32
mask=np.zeros_like(img)
cy=mask.shape[0]//2
cx=mask.shape[1]//2
cv2.circle(mask,(cx,cy),radius,(255,255,255),-1)[0]
mask=255-mask

#create white circle mask on black background and invert so black circle on white background (High pass)
radius=32
mask=np.zeros_like(img)
cy=mask.shape[0]//2
cx=mask.shape[1]//2
cv2.circle(mask,(cx,cy),radius,(255,255,255),-1)[0]
mask=255-mask

#blur the mask
mask2=cv2.GaussianBlur(mask,(19,19),0)

#apply mask to dft_shift
dft_shift_masked=np.multiply(dft_shift,mask)/255

#shift origin from center to upper left corner
back_ishift=np.fft.ifftshift(dft_shift)

#do idft saving as complex output
img_back=np.fft.ifft2(back_ishift,axes=(0,1))

#combine complen real and imaginary components to form (the magniture for) the original image again
img_back=np.abs(img_back).clip(0,255).astype(np.uint8)

The error is exactly as described in the message. You use the name img without first defining it. You need to define it before you use it. What do you expect img to be?

3 Likes