图像直方图 python

import cv2
import numpy as np
import matplotlib.pyplot as plt

FileName = r"D:\cycFeng\Data\2021-11-30\02Guass.mp4"
cap = cv2.VideoCapture(FileName)  # 原视频位置
while (True):
    ret, frame = cap.read()
    if not ret: #or count >= EndNum:
        print('not res , not image')
        break
    gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
  
    index = len(np.where(gray == 255)[0])
    print(index)
    
    img2 = cv2.resize(frame, (int(w / 2), int(h / 2)))
    cv2.imshow('a',img2)
    cv2.waitKey(1)
    
    # 直方图
    plt.hist(gray.flatten() , 256)
    plt.show()
    plt.close('all')
  
cap.release()
### 使用Python处理HSI图像直方图 对于HSI(高光谱图像)的直方图处理,在Python中有多种库可以实现这一目标。通常情况下,会涉及到`numpy`、`matplotlib.pyplot`以及专门用于图像处理的库如`opencv-python`。 当处理HSI图像时,首先需要读取并转换图像到适合的颜色空间。如果原始图像是BGR格式,则应将其转换为HSV颜色空间以便更好地分离色调、饱和度和强度信息[^1]: ```python import cv2 as cv from matplotlib import pyplot as plt img = cv.imread('hsi_image.png') hsv_img = cv.cvtColor(img, cv.COLOR_BGR2HSV) ``` 接着,为了计算二维直方图,即针对Hue(色调) 和 Saturation(饱和度),可利用`numpy.histogram2d()`函数来获取分布情况,并设置合适的bin范围以适应HSI特性: ```python import numpy as np h, s, i = cv.split(hsv_img) hist, xedges, yedges = np.histogram2d(h.flatten(), s.flatten(), bins=(72, 64), range=[[0, 180], [0, 256]]) plt.imshow(hist, interpolation='nearest', origin='lower') plt.title("2D Histogram of HSI Image") plt.xlabel("Saturation Bins") plt.ylabel("Hue Bins") plt.show() ``` 此外,考虑到HSI图像可能具有较高的维度特征,除了简单的可视化之外,还可以应用直方图均衡化技术增强对比度或进行其他预处理操作。这可以通过OpenCV中的`cv.equalizeHist()`方法完成,不过需要注意的是该方法默认适用于单通道灰度图像;因此,对于多维数据集来说,需分别对各分量执行此过程或者寻找更高级别的解决方案[^2]. 最后值得注意的一点是,实际工作中可能会遇到不同类型的HSI文件格式,比如ENVI .hdr等专用格式,这时就需要借助额外工具包如`spectral`来进行加载和支持更多功能的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值