显示一个文件夹下所有图片的直方图之和

针对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)  # 保存图像文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值