-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
noise.py
117 lines (88 loc) · 3.84 KB
/
noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import matplotlib.pyplot as plt
def generate_random_image(width, height):
"""Generate a random RGB image."""
return np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
def brightness(pixel):
"""Calculate the brightness of a pixel."""
return np.sum(pixel) / 3
def sort_pixels(image, iteration, total_iterations):
"""Sort a portion of the image based on pixel brightness."""
height, width, _ = image.shape
portion_height = height // total_iterations
start_row = iteration * portion_height
end_row = min((iteration + 1) * portion_height, height)
for row in range(start_row, end_row):
sorted_row = sorted(image[row, :, :], key=brightness)
image[row, :, :] = sorted_row
return image
# Parameters
width, height = 256, 256
total_iterations = 10
# Generate a random image
image = generate_random_image(width, height)
# Iteratively apply the sorting algorithm and display the progress
plt.figure(figsize=(10, 10))
for iteration in range(total_iterations):
sort_pixels(image, iteration, total_iterations)
plt.subplot(1, total_iterations, iteration + 1)
plt.imshow(image)
plt.axis('off')
plt.tight_layout()
# plt.show()
import cv2
def create_pixel_sorting_video(image, total_iterations, output_file):
height, width, _ = image.shape
# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, 20.0, (width, height))
for iteration in range(total_iterations):
# Apply sorting to a portion of the image
sort_pixels(image, iteration, total_iterations)
# Write the frame
out.write(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
# Release everything when the job is finished
out.release()
# Parameters for the video
total_iterations_for_video = 100 # More iterations for a smoother video
output_video_file = '/g/Code/neat-starpages/starpages/pixel_sorting_simulation.mp4'
# Generate a new random image
image_for_video = generate_random_image(width, height)
# Create the pixel sorting video
create_pixel_sorting_video(image_for_video, total_iterations_for_video, output_video_file)
output_video_file
def rgb_to_hsv(pixel):
"""Convert an RGB pixel to HSV."""
return cv2.cvtColor(np.uint8([[pixel]]), cv2.COLOR_RGB2HSV)[0][0]
def sort_pixels_by_hue(image, iteration, total_iterations):
"""Sort a portion of the image based on pixel hue."""
height, width, _ = image.shape
portion_height = height // total_iterations
start_row = iteration * portion_height
end_row = min((iteration + 1) * portion_height, height)
for row in range(start_row, end_row):
# Convert each pixel in the row to HSV and sort by hue
sorted_row = sorted(image[row, :, :], key=lambda pixel: rgb_to_hsv(pixel)[0])
image[row, :, :] = sorted_row
return image
def create_hue_sorted_video(image, total_iterations, output_file, fps):
height, width, _ = image.shape
# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, fps, (width, height))
for iteration in range(total_iterations):
# Apply hue-based sorting to a portion of the image
sort_pixels_by_hue(image, iteration, total_iterations)
# Write the frame
out.write(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
out.release()
# New parameters for the hue-based sorting video
fps = 120
duration = 30 # seconds
total_iterations_for_hue_video = fps * duration
output_hue_video_file = '/g/Code/neat-starpages/starpages/hue_sorted_simulation.mp4'
# Generate a new random image
image_for_hue_video = generate_random_image(width, height)
# Create the hue-sorted video
create_hue_sorted_video(image_for_hue_video, total_iterations_for_hue_video, output_hue_video_file, fps)
output_hue_video_file