
Und hier ist Code zum Erstellen dieser Punkte: (Contour_Classifier.py)
Code: Select all
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure, draw
def read_binary_image(file_path):
# Open the file and read line by line
with open(file_path, 'r') as file:
lines = file.readlines()
height, width = len(lines), len(lines[0])
print(height, width)
# Process lines into a 2D numpy array
image_data = []
for i in range(height + 2):
arr = []
for j in range(width + 2):
arr.append(0)
image_data.append(arr)
for i in range(2, height + 1):
for j in range(2, width + 1):
if(lines[i - 2][j - 2] != '1'):
image_data[i][j] = 0
else:
image_data[i][j] = 1
# Convert list to numpy array for easier manipulation
image_array = np.array(image_data)
return image_array
def display_image(image_array):
# Display the binary image using matplotlib
plt.imshow(image_array, cmap="gray")
plt.axis('off') # Hide axes
plt.show()
# Example usage
file_path = 'KOREKT\images\sbetmeni.txt' # Replace with the path to your file
image_array = read_binary_image(file_path)
#display_image(image_array)
#----------------------------------------------------------------------------------------------------------
#-------------------------------------------Finding Contours-----------------------------------------------
#----------------------------------------------------------------------------------------------------------
contours = measure.find_contours(image_array, level=0.5, positive_orientation='high')
fixed_contours = []
for contour in contours:
fixed_contour = np.column_stack((contour[:, 1], contour[:, 0])) # Swap (row, column) to (column, row)
fixed_contour[:, 1] = image_array.shape[0] - fixed_contour[:, 1] # Invert the y-axis
# Normalize coordinates between [0, 1]
fixed_contour[:, 0] /= image_array.shape[1] # Normalize x (width)
fixed_contour[:, 1] /= image_array.shape[0] # Normalize y (height)
fixed_contour[:, 0] *= 250 # Normalize x (width)
fixed_contour[:, 1] *= 250 # Normalize y (height)
fixed_contours.append(fixed_contour)
contours = fixed_contours
print(fixed_contours[0])
def visualize_colored_contours(contours, title="Colored Contours"):
# Create a plot
plt.figure(figsize=(8, 8))
for i, contour in enumerate(contours):
# Extract X and Y coordinates
x, y = zip(*contour)
# Plot the points with a unique color
plt.plot(x, y, marker='o', label=f'Contour {i+1}')
plt.title(title)
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.grid(True)
plt.axis("equal")
plt.show()
# Visualize the normalized contours
visualize_colored_contours(contours)
< /code>
Jetzt gehen wir zum Hauptteil, wo wir den Fourier -Serienalgorithmus implementieren. Ich teile die Zeitinteral (t) in die Menge der bereitgestellten Punkte und mache davon, dass alle Punkte relativ zu t gleiche Entfernungen zwischeneinander haben. Ich verwende die Annäherung an Integral als Summe der Punkte, wie in der Formel angegeben. /> Und hier ist Code implementiert es (Fourier_coefficients.py): < /p>
import numpy as np
def calculate_Fourier(points, num_coefficients):
complex_points = []
for point in points:
complex_points.append(point[0] + 1j * point[1])
t = np.linspace(0, 1, len(complex_points), endpoint=False)
c_k = np.zeros(num_coefficients, dtype=np.complex128)
for i in range(num_coefficients):
c_k[i] = np.sum(complex_points * np.exp(-2j * np.pi * i * t) * t[1])
return c_k
Und nun animiere ich auf der nächsten Folie den gesamten Prozess, wobei i Außerdem habe ich einen zusätzlichen Codeausschnitt zum Erstellen eines GIF geschrieben. Wenn meine Implementierung korrekt wäre, sollte es keine Schwierigkeiten geben, eine Batman-Form zu erstellen, aber wir können im gesamten GIF wirklich seltsame Phänomene beobachten.
Dies ist ein Codeausschnitt für diesen Teil
import numpy as np
import matplotlib.pyplot as plt
import imageio
from Fourier_Coefficients import calculate_Fourier
from Countour_Classifier import contours
# List to store file names for GIF creation
png_files = []
# Generate plots iteratively
for i in range(len(contours[0])):
contour_coefficients = []
for contour in contours:
contour_coefficients.append(calculate_Fourier(contour, i))
# Fourier coefficients (complex numbers) and frequencies
coefficients = contour_coefficients[0] # First contour
frequencies = np.arange(len(coefficients))
# Time parameters
t = np.linspace(0, 1, len(coefficients)) # One period
curve = np.zeros(len(t), dtype=complex)
# Use the first (i + 1) coefficients
for j in range(len(coefficients)):
c, f = coefficients[j], frequencies[j]
curve += c * np.exp(1j * 2 * np.pi * f * t)
# Plotting
plt.figure(figsize=(8, 8))
plt.plot(curve.real, curve.imag, label="Trajectory", color="blue")
plt.scatter(0, 0, color="black", label="Origin")
plt.axis("equal")
plt.title(f"Fourier Series with {i + 1} Coefficients")
plt.xlabel("Real Part (X)")
plt.ylabel("Imaginary Part (Y)")
plt.legend()
plt.text(-0.5, -0.5, f"Using {i + 1} coefficients", fontsize=12, color="red")
# Save the figure as a PNG file
filename = f"fourier_{i + 1}_coefficients.png"
plt.savefig(filename)
plt.close()
# Append the file name to the list
png_files.append(filename)
# Create a GIF from the PNG files
gif_filename = "fourier_series.gif"
with imageio.get_writer(gif_filename, mode='I', duration=0.5) as writer:
for filename in png_files:
image = imageio.imread(filename)
writer.append_data(image)
print("Plots saved as PNG files and GIF created as 'fourier_series.gif'.")
< /code>
Nun ist dies das Ergebnis
gif < /p>
Beobachtung #1 < /strong>
Wenn die Koeffizientenzahl ist 0, 1, 2 oder 3 Es zeichnet nichts. Holen Sie sich die wackelige kreisförmige Form, wobei der untere Teil des Bildes etwas identischer ist, das er ursprünglich vorstellen kann, aber durch durcheinander gebracht auf seinen Flügeln
Beobachtung #3 < /p>
Wenn wir uns dem Len nähern (Complex_numbers), ändert sich die situacion und wir erhalten diese seltsamen Formen, von kreisförmiger < /p>
Beobachtung # 4
Wenn wir den len (komplex_number) übertreffen, zeichnet es einen zufälligen Kauderwelsch
Beobachtung #5 < /p>
Wenn die Anzahl der Abteilungen innerhalb des T -Werts in der Animation.Py -Code geändert wird, erhalten wir völlig unterschiedliche Bilder. < /p>
Bearbeiten 1
Hier sind tatsächliche .txt -Daten für weitere Tests bereitgestellt. >
Nach all diesen Informationen können Sie mir helfen, was mit meinem Code falsch ist, < /P.>