簡體   English   中英

如何計算python中同一圖像內兩個不同區域的平均像素強度差?

[英]How to calculate the average pixel intensity difference for two different regions within the same image in python?

我有這些醫療原始數據圖像,它們有.rawb 而不是.jpg。 我需要打開並讀取 3D 的數據。 我的問題是在將這個文件打開到 python 之后,我必須比較這個圖像中兩個不同區域的像素強度(平均平均值)。

對於第二部分(即,一旦您將圖像讀入 numpy 數組),您可以 go 關於它如下:

import numpy as np

# simulate an image
n, m, o = 100, 100, 100
img = np.random.random((n, m, o)) ** 8

# define the two regions by the indices of two corners each
idx1 = ((20, 30, 25), (40, 35, 50))
idx2 = ((60, 55, 75), (65, 80, 85))

# get the pixel values in the specified regions
region1 = img[idx1[0][0]:idx1[1][0], idx1[0][1]:idx1[1][1], idx1[0][2]:idx1[1][2]]
region2 = img[idx2[0][0]:idx2[1][0], idx2[0][1]:idx2[1][1], idx2[0][2]:idx2[1][2]]

# calculate the averages
avg1 = np.mean(region1)
avg2 = np.mean(region2)
avg1, avg2

# calculate differences
abs_diff = avg2 - avg1
rel_diff = 1 - avg1 / avg2
print(f'abs. diff.: {abs_diff:.4f}')
print(f'rel. diff.: {rel_diff:.4f}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM