针对3D图像的,因为所有3D图像的2D切片都在一个文件夹里,所以要进行直方图各个像素值数量的累加。
import sys
import cv2
import numpy as np
import os, glob
from skimage import data,io
import matplotlib.pyplot as plt
np.set_printoptions(threshold=sys.maxsize)#全部打印
midfixed_img_path = 'D:/No.2project/MM_VM/result/RV_RL_png/fixed2moving_T22T1/Transverse/3/'
moving_img_path = 'D:/No.2project/MM_VM/result/RV_RL_png/moving_T1/Transverse/3/'
fixed_img_path = 'D:/No.2project/MM_VM/result/RV_RL_png/fixed_T2/Transverse/3/'
midfixed_img_names = os.listdir(midfixed_img_path)
moving_img_names = os.listdir(midfixed_img_path)
fixed_img_names = os.listdir(midfixed_img_path)
hist_midfixed = np.array([[0]*1]*256)
hist_moving = np.array([[0]*1]*256)
hist_fixed = np.array([[0]*1]*256)
for midfixed, moving, fixed in zip(midfixed_img_names, moving_img_names, fixed_img_names):
midfixed = midfixed_img_path + midfixed
moving = moving_img_path + moving
fixed = fixed_img_path + fixed
# 1.加载图像
midfixed = cv2.imread(midfixed, cv2.IMREAD_COLOR)
moving = cv2.imread(moving, cv2.IMREAD_COLOR)
fixed = cv2.imread(fixed, cv2.IMREAD_COLOR)
# 2.获取直方图的值
hist_midfixed = cv2.calcHist([midfixed], [0], None, [256], [1, 255]) + hist_midfixed
hist_moving = cv2.calcHist([moving], [0], None, [256], [1, 255]) + hist_moving
hist_fixed = cv2.calcHist([fixed], [0], None, [256], [1, 255]) + hist_fixed
# 3.显示直方图
plt.plot(hist_midfixed, color="r")
plt.plot(hist_moving, color="g")
plt.plot(hist_fixed, color="b")
plt.show()
saveFile = "D:/No.2project/MM_VM/result/RV_RL_png/fixed2moving_T22T1/Transverse/3_0-255/75.png" # 保存文件的路径
cv2.imwrite(saveFile, img_src) # 保存图像文件