Question Details

No question body available.

Tags

python arrays loops multidimensional-array dft

Answers (1)

March 27, 2026 Score: 0 Rep: 507 Quality: Low Completeness: 80%

For 2D such as image processing one can do the DFT for each row and column then add the results.

For example if the DFT of row 1 produces 1 and the DFT of column 1 produces 2 then in the 2D DFT the cell at the location of 1,1 will have the value of 1+2=3

That is false. At best, in some very specific cases, you can multiply the DFT of the two axis, but your example is not one of them.

Your best shot at debuging your implementation is to display the results, using pyplot for instance. You'll see immediately what is wrong.

The following code should do what you want, though it simply uses numpy for the 2D DFT.

import re
import numpy as np
import matplotlib.pyplot as plt

pixel = re.compile(r'\((?P\d{1,3}),(?P\d{1,3}),(?P\d{1,3})\)')

with open('image.txt', 'r') as imagefile: lines = imagefile.readlines()

image = []

for n, line in enumerate(lines): image.append([]) for m in pixel.finditer(line): image[n].append( [ int(m.group('R')), int(m.group('G')), int(m.group('B')), ] )

image = np.array(image)

imagefft = np.abs(np.fft.fft2(image, axes=(0, 1))) imagefft = imagefft / imagefft.max()

fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(image) ax2.imshow(image_fft) fig.show()

Original image next to its DFT