Python独習!

習得したPython知識をペイフォワード

ラインプロファイルのスプライン補間

シーンとしては画像解析。
エッヂ広がり関数(edge spread function:ESF)を微分して、ライン広がり関数(line spread function:LSF)を求めて、その半値幅で画像のボケ度合いを定量化する。
今回は、ESFもしくはLSFをデータ補間して画素数の少なさからくる“ラフさ”をなくすところ。

取っ掛かり

ベースは某エンジニアのお仕事以外のメモさんのプログラムを参考に。
water2litter.net


スプライン補間はHiroto Aさんのプログラムを参考に。
org-technology.com

モジュールのインストール

コマンドプロンプトで pip install ***** コマンドを使ってモジュールのインストールする。*****には以下が入る。

  • numpy
  • scipy
  • matplotlib

結果

スプライン補間の結果はこんな感じ。
上:全体 下:一部拡大(青が補間データ、赤が生データ)
f:id:greenhornprofessional:20191224234539p:plain
f:id:greenhornprofessional:20191224234542p:plain

プログラム

01_ReadCsvSpline_002.py

import csv
import numpy as np
from scipy import signal, interpolate
from matplotlib import pylab as plt

fn = '01_test.csv' #.pyファイルと同じ階層に置く
cal = 1 #x軸データの縮尺調整用のパラメータ。他サンプルとの比較に使う。

#csvを読み込んでdata_arrayに格納
with open(fn, mode='r', newline='') as f_in:
    reader = csv.reader(f_in)
    data_array = [row for row in reader]

#data_arrayの中身を確認する
print(data_array)

#data_arrayをx,yに分けるための配列を定義する
plot_x = []
plot_y = []

#data_arrayの要素を一個ずつ取り出して、各配列に格納する
for i in data_array:
    plot_x.append(float(i[0])*cal) #要素は文字列として扱われているのでfloatに変換する
    plot_y.append(float(i[1]))

#linspaceにx軸データ範囲が必要なのでxの最小値/最大値を調べる
xs = min(plot_x)
xm = max(plot_x)
print("x軸の最小値は",xs)
print("x軸の最大値は",xm)

#lilnspaceでデータ補間数を決める
tt = np.linspace(xs, xm, 1000)

#2次スプライン補間
f = interpolate.interp1d(plot_x, plot_y, kind="quadratic")
y = f(tt)

#データプロット
plt.plot(plot_x, plot_y,"r")
plt.plot(tt, y)
plt.show()

/* -----codeの行番号----- */