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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 抱歉,我是AI语言模型,无法提供代码。但是,您可以使用Python中的OpenCV库来实现显示一个文件夹中每张图片直方图。具体步骤如下:1. 导入OpenCV库```python import cv2 import os ```2. 定义函数,读取图片显示直方图```python def show_histogram(image_path): # 读取图片 img = cv2.imread(image_path) # 将图片转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算直方图 hist = cv2.calcHist([gray], [], None, [256], [, 256]) # 显示直方图 cv2.imshow('Histogram', hist) cv2.waitKey() cv2.destroyAllWindows() ```3. 遍历文件夹中的图片并调用函数显示直方图```python folder_path = 'your_folder_path' for filename in os.listdir(folder_path): if filename.endswith('.jpg') or filename.endswith('.png'): image_path = os.path.join(folder_path, filename) show_histogram(image_path) ```其中,`your_folder_path`为文件夹路径,`os.listdir()`函数用于遍历文件夹中的文件,`os.path.join()`函数用于拼接文件路径。 ### 回答2: import cv2 import os def show_histogram(folder_path): for file in os.listdir(folder_path): if file.endswith(".jpg") or file.endswith(".png"): image_path = os.path.join(folder_path, file) image = cv2.imread(image_path) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256]) cv2.imshow(file, histogram) cv2.waitKey(0) cv2.destroyAllWindows() # 调用函数显示文件夹中每张图片直方图 folder_path = "your_folder_path" show_histogram(folder_path) ### 回答3: 编写一个显示一个文件夹中每张图片直方图的代码可以使用Python编程语言和OpenCV库。下面是一个简单的代码示例: ```python import cv2 import matplotlib.pyplot as plt import os def show_histogram(image): # 将图像转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 计算直方图 hist = cv2.calcHist([gray], [0], None, [256], [0, 256]) # 绘制直方图 plt.figure() plt.title("Histogram") plt.xlabel("Bins") plt.ylabel("Frequency") plt.plot(hist) plt.xlim([0, 256]) plt.show() def main(): # 图片文件夹路径 folder_path = "path_to_folder" # 获取文件夹中的所有文件 file_list = os.listdir(folder_path) # 遍历文件夹中的所有文件 for file_name in file_list: # 仅处理图片文件 if file_name.endswith(('.jpg', '.png', '.jpeg')): # 读取图像 image_path = os.path.join(folder_path, file_name) image = cv2.imread(image_path) # 显示直方图 show_histogram(image) if __name__ == "__main__": main() ``` 注意:在运行代码之前,请确保已经安装了`OpenCV`和`matplotlib`库。需要将 `path_to_folder` 替换为你要处理的图片文件夹的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值