|
1
|
#!/usr/bin/env python
|
|
2
|
# -*- coding: utf-8 -*-
|
|
3
|
|
|
4
|
##
|
|
5
|
# @file gamma_curve_plot.py
|
|
6
|
# @brief Generates and visualizes gamma correction curves for comparison.
|
|
7
|
#
|
|
8
|
# @details
|
|
9
|
# This script creates a graph comparing multiple gamma correction curves
|
|
10
|
# against a linear reference (no gamma correction).
|
|
11
|
# The generated plot is intended for documentation and wiki usage
|
|
12
|
# to visually explain how different gamma values affect intensity mapping.
|
|
13
|
#
|
|
14
|
# Gamma correction is calculated using the formula:
|
|
15
|
# @f[
|
|
16
|
# y = x^{(1/\gamma)}
|
|
17
|
# @f]
|
|
18
|
#
|
|
19
|
# The resulting graph is saved as a PNG image file.
|
|
20
|
|
|
21
|
import numpy as np
|
|
22
|
import matplotlib.pyplot as plt
|
|
23
|
|
|
24
|
|
|
25
|
##
|
|
26
|
# @brief Plots gamma correction curves and saves the graph as an image.
|
|
27
|
#
|
|
28
|
# @details
|
|
29
|
# This function generates a normalized input gradient from 0 to 1
|
|
30
|
# and applies multiple gamma correction transformations to it.
|
|
31
|
# The corrected curves are plotted alongside a linear reference curve
|
|
32
|
# to highlight the differences in brightness response.
|
|
33
|
#
|
|
34
|
# The output graph is saved locally and displayed on screen.
|
|
35
|
#
|
|
36
|
# @note
|
|
37
|
# This visualization is intended for explanatory purposes,
|
|
38
|
#
|
|
39
|
# @return None
|
|
40
|
def plot_gamma_curves():
|
|
41
|
##
|
|
42
|
# @brief Generate input gradient values.
|
|
43
|
#
|
|
44
|
# @details
|
|
45
|
# Creates a smooth range of normalized intensity values
|
|
46
|
# from 0.0 to 1.0 for curve plotting.
|
|
47
|
x = np.linspace(0, 1, 500)
|
|
48
|
|
|
49
|
##
|
|
50
|
# @brief List of gamma values to compare.
|
|
51
|
#
|
|
52
|
# @details
|
|
53
|
# These gamma values demonstrate different correction strengths,
|
|
54
|
# from low (brightening) to very high (strong compression).
|
|
55
|
gamma_values = [0.5, 3, 50]
|
|
56
|
|
|
57
|
##
|
|
58
|
# @brief Initialize matplotlib figure.
|
|
59
|
plt.figure(figsize=(10, 6))
|
|
60
|
|
|
61
|
##
|
|
62
|
# @brief Plot linear reference curve (no gamma correction).
|
|
63
|
#
|
|
64
|
# @details
|
|
65
|
# This dashed line represents a direct linear mapping (y = x)
|
|
66
|
# and serves as a baseline for comparison.
|
|
67
|
plt.plot(x, x, 'k--', label='Linear (No Correction)', alpha=0.5)
|
|
68
|
|
|
69
|
##
|
|
70
|
# @brief Plot gamma-corrected curves.
|
|
71
|
#
|
|
72
|
# @details
|
|
73
|
# Applies the gamma correction formula y = x^(1/gamma)
|
|
74
|
# and plots each resulting curve.
|
|
75
|
for gamma in gamma_values:
|
|
76
|
y = np.power(x, 1.0 / gamma)
|
|
77
|
plt.plot(
|
|
78
|
x,
|
|
79
|
y,
|
|
80
|
linewidth=2.5,
|
|
81
|
label=f'Gamma = {gamma} (Exp: 1/{gamma} ≈ {1.0 / gamma:.2f})'
|
|
82
|
)
|
|
83
|
|
|
84
|
##
|
|
85
|
# @brief Configure graph appearance and labels.
|
|
86
|
plt.title('Gamma Correction Curves Comparison', fontsize=14)
|
|
87
|
plt.xlabel('Input Value (Gradient 0 to 1)', fontsize=12)
|
|
88
|
plt.ylabel('Corrected Output Value', fontsize=12)
|
|
89
|
plt.legend()
|
|
90
|
plt.grid(True, alpha=0.3)
|
|
91
|
plt.ylim(0, 1.05)
|
|
92
|
plt.xlim(0, 1.0)
|
|
93
|
|
|
94
|
##
|
|
95
|
# @brief Save the generated graph to a file.
|
|
96
|
#
|
|
97
|
# @details
|
|
98
|
# The image is saved in PNG format for easy embedding in documentation.
|
|
99
|
output_filename = 'gamma_comparison_graph.png'
|
|
100
|
plt.savefig(output_filename)
|
|
101
|
print(f"Graph successfully saved as: {output_filename}")
|
|
102
|
|
|
103
|
##
|
|
104
|
# @brief Display the graph window.
|
|
105
|
plt.show()
|
|
106
|
|
|
107
|
|
|
108
|
##
|
|
109
|
# @brief Program entry point.
|
|
110
|
#
|
|
111
|
# @details
|
|
112
|
# Executes the gamma curve plotting function when the script
|
|
113
|
# is run directly.
|
|
114
|
if __name__ == "__main__":
|
|
115
|
plot_gamma_curves()
|