目前正在尝试根据本文Performance metrics for image contrast计算直方图散度以获取图像对比度指标。该文件的屏幕截图显示了更容易,更快阅读的算法:
或引用本文:
Histogram spread is the ratio of the quartile distance to the
range of the histogram. Quartile distance is denoted as the
difference between the 3 rd quartile and the 1 st quartile. Here 3 rd
quartile and 1 st quartile means the histogram bins at which
cumulative histogram have 75% and 25% of the maximum
value respectively (see Fig.1)
我在实验中使用了这两个图像,它们都是医学图像(乳房X线照片)1.
2.
这是我使用的代码:
import cv2
import numpy as np
def calculate_histogram_spread(gray_img):
#compute image histogram and its cdf
histo,_ = np.histogram(gray_img.flatten(),256,[0,256])
cdf = np.cumsum(histo)
#calculate the Q3 (75%percentile) and Q1 (25%percentile)
Q3 = np.percentile(cdf,75)
Q1 = np.percentile(cdf,25)
#find the which bin value
Q3_bin = np.searchsorted(cdf,Q3)
Q1_bin = np.searchsorted(cdf,Q1)
IQR = Q3_bin - Q1_bin
divider = np.max(gray_img) - np.min(gray_img)
HS = IQR/divider
return Q3_bin,Q1_bin,HS
if __name__ == '__main__':
sample1 = '/home/mario/Desktop/hs1.jpg'
sample2 = '/home/mario/Desktop/hs2.jpg'
img1 = cv2.imread(sample1,0)
img2 = cv2.imread(sample2,0)
print('Sample1')
Q3_bin,Q1_bin,HS = calculate_histogram_spread(img1)
print('Q3_bin={},Q1_bin={},HS={}'.format(Q3_bin,Q1_bin,HS))
print('Sample2')
Q3_bin,Q1_bin,HS = calculate_histogram_spread(img2)
print('Q3_bin={},Q1_bin={},HS={}'.format(Q3_bin,Q1_bin,HS))
这是我的最终结果:Sample1
Q3_bin=192,Q1_bin=64,HS=0.5019607843137255
Sample2
Q3_bin=192,Q1_bin=64,HS=0.5019607843137255
我的问题是,为什么两个图像具有相同的精确输出,而视觉上图像具有不同的对比度(我认为),我是否犯了任何错误?有谁能够帮助我。我还没有测试相同尺寸/分辨率的图像。
最佳答案
在Python / OpenCV中,这似乎为我提供了不同的对比度值。
输入1:
输入2:
import cv2
import numpy as np
# load images as grayscale
img = cv2.imread("breast1.jpg", 0)
#img = cv2.imread("breast2.jpg", 0)
hh, ww = img.shape[:2]
# compute total pixels
tot = hh * ww
# compute histogram
hist = np.histogram(img,bins=256,range=[0,255])[0]
# compute cumulative histogram
cum = np.cumsum(hist)
# normalize histogram to range 0 to 100
cum = 100 * cum / tot
# get bins of percentile at 25 and 75 percent in cum histogram
i = 0
while cum[i] < 25:
i = i+1
B1 = i
i = 0
while cum[i] < 75:
i = i+1
B3 = i
print('25 and 75 percentile bins:',B1,B3)
# compute min and max graylevel (which are also the min and max bins)
min = np.amin(img)
max = np.amax(img)
print('min:',min, 'max:',max)
# compute contrast
contrast = (B3-B1)/(max-min)
print('contrast:',contrast)
图片1的结果:
25 and 75 percentile bins: 0 147
min: 0 max: 255
contrast: 0.5764705882352941
图片2的结果:
25 and 75 percentile bins: 58 162
min: 0 max: 255
contrast: 0.40784313725490196
关于python - 为图像对比度度量实现直方图扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63437029/