首页 > 编程问答 >温度矩阵为 phash?

温度矩阵为 phash?

时间:2024-07-30 17:13:26浏览次数:6  
标签:python image-processing hash hashmap hashcode

我有以下问题:我想在 python 中比较热图像(数据以一个大矩阵/每个像素一个值的形式出现)并确定它们是否相等。我读了很多关于 phash 的内容,但都是在图片的背景下。由于我的原始数据是矩阵,我想知道这是否是一种有意义的方法?

我读了很多关于 phash 的内容,但无法弄清楚/找到关于它如何与矩阵数据库一起工作的文献...

的大小我的输入是 50x3000,因为它是我无法共享的敏感内部数据,但数字是范围从 700,0 到 1100,0 摄氏度的小数

我的 Python 代码:

import os
import json
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import imagehash

folder_pathname = XXX
images_folder = os.path.join(folder_pathname, "images")

# read initially one json for first comparison
abs_json = os.path.join(folder_pathname, "00_Ref_image.json")
with open(abs_json, 'r', encoding="utf8") as f:
    camera_data = json.load(f)

# Set global image size
width = camera_data["videoWidth"]
height = camera_data["videoHeight"]

# convert reference image to 2D array
R_image = np.array(camera_data["image"])
R_image.shape = (height, width)

# convert the NumPy array to a PIL image
R_pil_image = Image.fromarray(R_image.astype(np.uint8))

# calculate histogram for reference image
HIST_BIN = 70
HIST_RANGE = (870, 1200)
R_hist = np.histogram(R_image, bins=HIST_BIN)

# Calculate the hash value for the reference image
R_hash = imagehash.dhash(R_pil_image)
print(f"Reference Image Hash: {R_hash}")

# Display the reference image histogram
plt.figure(figsize=(6, 4))
plt.bar(range(HIST_BIN), R_hist[0])
plt.title("Reference Image Histogram")
plt.xlabel("Bin")
plt.ylabel("Count")
plt.show()

# for all files in image folder
for json_file in os.listdir(images_folder):
    abs_json = os.path.join(images_folder, json_file)
    with open(abs_json, 'r', encoding="utf8") as f:
        camera_data = json.load(f)
        M_image = np.array(camera_data["image"])

    # convert the NumPy array to a PIL image
    M_pil_image = Image.fromarray(M_image.astype(np.uint8))
    M_hist = np.histogram(M_image, bins=HIST_BIN)

    # Calculate the hash value for the current image
    M_hash = imagehash.average_hash(M_pil_image)
    print(f"{json_file} Image Hash: {M_hash}")

    # Calculate the similarity percentage
    similarity = 100 - ((R_hash - M_hash) / 64 * 100)

    # Display the current image histogram and similarity
    plt.figure(figsize=(6, 4))
    plt.bar(range(HIST_BIN), M_hist[0])
    plt.title(f"{json_file} Image Histogram - {similarity:.2f}% similar")
    plt.xlabel("Bin")
    plt.ylabel("Count")
    plt.show()

你当然可以使用 pHash 来比较你的热图像,即使是原始矩阵数据也是如此。但这里有一个关键点:pHash(以及其他图像哈希算法)设计用于识别视觉相似性,即使在图像经过压缩、调整大小或略微更改颜色时也是如此。

在你的情况下,你正在处理代表温度值的原始数值数据。这些值的微小差异在视觉上可能并不重要,但在表示温度分布方面可能很重要。让我们分解一下如何仍然使用 pHash 以及探索其他合适的选项:

使用 pHash(需要调整):

  1. 归一化: 将你的温度矩阵归一化为 0-255 范围,以有效地表示 8 位灰度图像的像素强度。这可以通过以下方法完成: ```python from sklearn.preprocessing import minmax_scale

normalized_matrix = minmax_scale(R_image, feature_range=(0, 255)).astype(np.uint8) 2. **生成图像:**从归一化的矩阵数据创建 PIL 图像: python pil_image = Image.fromarray(normalized_matrix) `` 3. **应用 pHash:**现在你可以像以前那样使用 imagehash.dhash(pil_image)` 计算 pHash。

要记住的重要事项:

  • 灵敏度: pHash 旨在对小的视觉差异具有鲁棒性。如果你的温度变化很小,但很重要,pHash 可能无法捕捉到它们。调整 pHash 函数中的阈值或探索其他更敏感的哈希函数。
  • 解释: pHash 相似性分数将反映图像结构的总体相似性,这可能与你正在寻找的确切温度分布差异不完全匹配。

替代方法:

  1. 直方图比较: 你已经在计算直方图。考虑使用 OpenCV 的 cv2.compareHist() HISTCMP_CORREL HISTCMP_BHATTACHARYYA 等方法来直接比较直方图以获得数值相似性分数。

  2. 结构相似性指数 (SSIM): SSIM 是一种感知图像质量指标,它也可以直接在你的矩阵上使用,而无需哈希处理。SSIM 提供了一个在 -1 和 1 之间的相似性分数,其中 1 表示完全相同。

```python from skimage.metrics import structural_similarity as ssim

ssim_score = ssim(R_image, M_image) ```

  1. 均方误差 (MSE) 或峰值信噪比 (PSNR): 这些是常用的图像质量指标,可以适应于比较温度矩阵。较低的 MSE 或较高的 PSNR 值表示更高的相似性。

```python mse_score = np.mean((R_image - M_image)**2)

psnr_score = cv2.PSNR(R_image, M_image) ```

选择正确的方法:

最佳方法取决于你的具体需求:

  • 对小的视觉变化的鲁棒性: 如果需要对小的视觉差异(如噪声或伪影)具有鲁棒性,pHash 是一个不错的选择。
  • 对温度分布的敏感性: 如果需要对温度分布的微小变化敏感,那么直方图比较、SSIM 或 MSE/PSNR 可能是更好的选择。
  • 解释的简便性: SSIM 和 MSE/PSNR 提供更容易解释的数值相似性分数。

通过尝试这些方法,你可以确定最适合你的热图像比较需求的方法。

标签:python,image-processing,hash,hashmap,hashcode
From: 78805957

相关文章

  • 为什么 string.maketrans 在 Python 3.1 中不起作用?
    我是Python新手。怎么了这个在Python3.1中不起作用?fromstringimportmaketrans#Requiredtocallmaketransfunction.intab="aeiou"outtab="12345"trantab=maketrans(intab,outtab)str="thisisstringexample....wow!......
  • Python - Creating alternative initializers using class Methods
    Classmethodsallowustodefinealternativeinitializers(alsoknownasfactorymethods)inaclass.Thesemethodshelpuscreateinstanceobjectsfromdifferenttypesofinputdata.Letusunderstandthiswiththehelpofanexample.Again,wetakethe......
  • 如何让 Python 请求信任自签名 SSL 证书?
    importrequestsdata={'foo':'bar'}url='https://foo.com/bar'r=requests.post(url,data=data)如果URL使用自签名证书,则会失败requests.exceptions.SSLError:[Errno1]_ssl.c:507:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIF......
  • python 偏函数
    如下代码loop=tornado.ioloop.IOLoop.current()ctx=contextvars.copy_context()func_call=functools.partial(ctx.run,func,*args,**kwargs)returnawaitloop.run_in_executor(executor,func_call)偏函数一个函数作为模板,通过提供部分参数来产生一个新的函数。......
  • Chapter 18 Python异常
    欢迎大家订阅【Python从入门到精通】专栏,一起探索Python的无限可能!文章目录前言一、什么是异常二、捕获异常三、异常的传递前言在Python中,异常是一种特定的对象,能够在程序运行过程中被抛出和处理。有效地管理异常不仅可以增强程序的稳定性,还可以提高用户体验,使程......
  • Python正则表达式匹配数字的第一次重复
    示例:For0123123123,1应匹配,因为第二个1出现在任何其他数字重复之前。For01234554321,5应该匹配,因为第二个5出现在任何其他数字的重复之前。我尝试过的一些正则表达式:......
  • 当 python 极坐标中某些列条目为空时,如何分解 List[_] 列?
    给定如下所示的Polarsdf,如何在两列上调用explode(),同时将空条目扩展到正确的长度以与其行匹配?shape:(3,2)┌───────────┬─────────────────────┐│x┆y││---┆---......
  • 使用python从网站自动下载pdf时出错
    我想从一个名为epadossier.nl的网站自动批量下载pdf我用谷歌搜索了这个并找到了一段代码并修复了一个小错误。所以现在我得到了这个`importrequestsurl="https://www.epadossier.nl/adres/plaats/straat/num"response=requests.get(url)ifresponse.status_cod......
  • 避免字符串连接的嵌套循环的 Pythonic 方法
    我想找到所有5位数字的字符串,其中前三位数字在我的第一个列表中,第二个槽第四个数字在我的第二个列表中,第三到第五个数字在我的最后一个列表中:l0=["123","567","451"]l1=["234","239","881"]l2=["348","551","399"......
  • Python 环境配置(二)安装jupyter、matplotlib、numpy库
    Python环境配置(二)安装jupyter、matplotlib、numpy库一、numpypipinstallnumpy二、matplotlibpipinstallmatplotlib三、jupyter1、anaconda自带Jupyter2、pycharm插件只有Pycharm的Professional版才支持JupyterNotebook,请注意版本3、新建文件#%......