Bus errorとSegmentation faultに困ったら見るブログ

物理の研究者による日々の研究生活のメモ書きです ( python/emacs/html/Japascript/シェルスクリプト/TeX/Mac/C言語/Linux/git/tmux/R/ポケモンGO)

飽和(サチュレーション)した場合の2次元ガウスビームとそのヒストグラム【python3, matplotlib】

スライドで使いたかったので、飽和(サチュレーション)した場合の2次元ガウスビームとそのヒストグラムをchatGPTと相談しながら作りました

プロット例


コード例

import numpy as np
import matplotlib.pyplot as plt

# 2Dガウシアンビーム画像の生成
x = np.linspace(-1.5, 1.5, 256)
X, Y = np.meshgrid(x, x)
I = np.exp(-2 * (X**2 + Y**2))  # Gaussian beam intensity profile

# ヒストグラム
plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.imshow(I, cmap='gray')
plt.title("Gaussian Beam Image")

plt.subplot(1, 2, 2)
plt.hist(I.ravel(), bins=50, color='gray')
#plt.title("Histogram of Intensity Values")
plt.title('Histogram')
plt.xlabel("Intensity")
#plt.ylabel('Histogram')

plt.tight_layout()
plt.show()

プロット例


コード例

import numpy as np
import matplotlib.pyplot as plt

# 画像サイズ
N = 256
x = np.linspace(-1.5, 1.5, N)
X, Y = np.meshgrid(x, x)
R2 = X**2 + Y**2

# ガウスパラメータ
sigma = 1.0
I0    = 400.0  # 中心が飽和するように255を超える

# 真の強度分布
I_true = I0 * np.exp(-2 * R2 / sigma**2)

# センサー応答(ノイズ + オフセット + 飽和)
black_level = 5.0
read_noise = np.random.normal(0, 1.5, size=I_true.shape)
I_sensor = I_true + black_level + read_noise

# クリップしてuint8に変換(飽和処理)
I_clip = np.clip(I_sensor, 0, 255)
img = I_clip.astype(np.uint8)

# 可視化(カラーバー付き)
plt.figure(figsize=(12, 5))

# 左:画像表示 + カラーバー
plt.subplot(1, 2, 1)
im = plt.imshow(img, cmap='gray', vmin=0, vmax=255)
plt.title('Gaussian Beam with Saturation')
#plt.axis('off')
plt.colorbar(im, fraction=0.046, pad=0.04, label='Intensity')

# 右:ヒストグラム
plt.subplot(1, 2, 2)
plt.hist(img.ravel(), bins=50, range=(0, 255), color='gray')
plt.title('Histogram')
plt.xlabel('Intensity')
#plt.ylabel('Histogram')
plt.tight_layout()
plt.show()

ランキング参加中です

↓クリックしていただけると嬉しいです〜