该程序的主要功能作用
- 该程序可以将DECT相关的数据进行直方图数据统计
- 所用到的数据可以为:
- “100kv CT Number”,
- “140kv CT Number”,
- “Density (g/cc)”,
- “PEF”,
- “Ze”
- 数据类型为tiff图片格式 (输入的数据)
- 输出的数据为图片,即将直方图以图片的格式(.png)保存在同级目录中
程序界面
操作步骤
- 选择数据类型
- 打开图片所在文件夹
3.等待运行
- 查看结果
运算主程序
import os
from matplotlib.pyplot import savefig
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
from scipy import stats
class Table(object):
def __init__(self, table_type_value, table_data_path, data_range_value):
self.table_type_value = table_type_value
self.table_type_key = {1: "100kv CT Number", 2: "140kv CT Number", 3: "Density (g/cc)", 4: "PEF", 5: "Ze"}
self.table_type = {"100kv CT Number": "#3333FF", "140kv CT Number": "#FF6600", "Density (g/cc)": "#00FF00",
"PEF": "#FF00FF", "Ze": "#660000"}
self.table_data_path = table_data_path
self.data_range_key = {1: (1000, 12000), 2: (1000, 6000), 3: (1, 6), 4: (1, 17), 5: (1, 25)}
self.xticks_range = {(1000, 12000): range(1000, 12000, 1000), (1000, 6000): range(1000, 9000, 1000),
(1, 6): range(1, 6, 1), (1, 17): range(1, 17, 1), (1, 25): range(1, 25, 1)}
self.data_range_value = data_range_value
self.data_range = self.data_range_key[self.data_range_value]
self.Histogram_bins = 50
def get_table(self, rootpath):
self.image = Image.open(self.table_data_path)
self.array = np.array(self.image).flatten()
self.table_data = [i for i in self.array if i > 0]
Mode_value = round(stats.mode(self.table_data)[0][0], 2)
Mode_count = stats.mode(self.table_data)[1][0]
Count = np.size(self.table_data)
Min = round(np.min(self.table_data), 2)
Max = round(np.max(self.table_data), 2)
Mean = round(np.mean(self.table_data), 2)
StdDev = round(np.std(self.table_data), 2)
plt.subplot(1, 2, 1)
plt.figure(figsize=(6, 4), dpi=80)
n, bins, patches = plt.hist(self.table_data, self.Histogram_bins, density=False, color="%s" %
self.table_type[self.table_type_key[self.table_type_value]])
plt.xticks(self.xticks_range[self.data_range_key[self.data_range_value]])
plt.grid(alpha=0.4)
n_max = np.max([n[i] for i in range(len(n))])
plt.text(Mean*1.1, n_max*4/5, "Min:%s Max:%s\nMean:%s StdDev:%s\nCount:%s Mode:%s(%s)" %
(Min, Max, Mean, StdDev, Count, Mode_value, Mode_count))
plt.title("%s" % self.table_data_path.split("\\")[-1].split(".")[0])
plt.xlabel("%s" % self.table_type_key[self.table_type_value])
plt.ylabel("Count")
savefig('%s' % os.path.join(rootpath, self.table_data_path.split("\\")[-1].split(".")[0]))
# plt.show()
if __name__ == '__main__':
rootpath = r"F:\cyb_workdata\31_301_sample_Distribution_histogram\tif_data\05_ze"
l = os.listdir(rootpath)
# print(l)
for i in range(len(l)):
if ".tif" in l[i]:
table = Table(5, os.path.join(rootpath, l[i]), 5)
pic = table.get_table()
else:
print("没有找到对应的tiff文件")
界面主程序
import webbrowser
from tkinter import *
from hist import *
import easygui as g
import tkinter.messagebox
import tkinter.ttk
class MyGUI(object):
def __init__(self, init_window_name):
self.init_window_name = init_window_name
def set_init_window(self):
self.init_window_name.title("单张切片数据的直方图绘制")
self.init_window_name.geometry('320x250+700+100')
self.init_window_name.attributes("-alpha", 0.9)
first_button = Button(self.init_window_name, text="选择图片所在文件夹", command=self.select_radiobutton)
first_button.grid(row=2, column=4, rowspan=1, columnspan=3, sticky=E, pady=20, padx=60)
label = Label(self.init_window_name, text="请选择数据的类型:")
label.grid(row=2, column=0, columnspan=2, sticky=W)
self.v = tkinter.IntVar()
radio_button1 = Radiobutton(self.init_window_name, text="low CT Number", fg='blue', variable=self.v, value=1)
radio_button1.grid(row=3, column=1, sticky=W)
radio_button2 = Radiobutton(self.init_window_name, text="high CT Number", fg='blue', variable=self.v, value=2)
radio_button2.grid(row=4, column=1, sticky=W)
radio_button3 = Radiobutton(self.init_window_name, text="Density", fg='blue', variable=self.v, value=3)
radio_button3.grid(row=5, column=1, sticky=W)
radio_button4 = Radiobutton(self.init_window_name, text="PEF", fg='blue', variable=self.v, value=4)
radio_button4.grid(row=6, column=1, sticky=W)
radio_button5 = Radiobutton(self.init_window_name, text="Ze", fg='blue', variable=self.v, value=5)
radio_button5.grid(row=7, column=1, sticky=W)
self.progressbar = tkinter.ttk.Progressbar(self.init_window_name)
self.progressbar.place(relx=0.01, rely=0.88, height=20, width=180)
second_button = Button(self.init_window_name, text="查看使用说明", command=self.open_help_web)
second_button.grid(row=8, column=4, rowspan=1, columnspan=3, sticky=W, pady=10, padx=60, ipadx=18)
def select_radiobutton(self):
if self.v.get() == 0:
tkinter.messagebox.showerror('提示', '请选择数据类型')
if self.v.get() != 0:
self.rootpath = g.diropenbox()
l = os.listdir(self.rootpath)
self.progressbar['maximum'] = len(l)
self.progressbar['value'] = 0
for i in range(len(l)):
self.progressbar['value'] += 1
self.init_window_name.update()
if ".tif" in l[i]:
table = Table(self.v.get(), os.path.join(self.rootpath, l[i]), self.v.get())
pic = table.get_table(self.rootpath)
if ".png" in l[i]:
os.remove(os.path.join(self.rootpath, l[i]))
if self.progressbar['value'] == len(l):
tkinter.messagebox.showinfo('提示', '已完成!!!')
self.progressbar['value'] = 0
def open_help_web(self):
webbrowser.open_new_tab("http://124.70.7.51/source/tool.html")
if __name__ == '__main__':
w = Tk()
window = MyGUI(w)
window.set_init_window()
w.mainloop()