本来只想探讨Pillow histogram在人脸辨识的运用,却扯出了一堆疑问….

本来只想探讨Pillow histogram在人脸辨识的运用,却扯出了一堆疑问….
"图片差异比对"真的是有智慧的吗? Is骨龄Bone Age 判读intelligent ?

某些脸部辨识程序码会运用Pillow Image.histogram颜色直方图的差异,difference数字愈大,表示两张图片愈不相同。现在先研究一下:
Question:

  • Histogram()是什麽?
  • 图片尺寸大小会影响吗? 类型?
  • 放大缩小会影响吗?
    如果将同一张图A,放大(缩小)成B,会有差异吗?
    在人类眼中AB两张图是一样的(内容、构图、颜色…一样),两张图如使用”直方图”判断,会不会说是’不一样’?
    Histogram是什麽?
    The histogram() method provides information on counts of different colors/bands.

histogram() method returns a list of pixel counts for each band present in the image.
The list will have all the counts concatenated for each band.

If an image is of mode "RGB" then for each of band/color a list of pixel counts will be returned, totaling 768.

In other words, for an RGB image, the histogram() method gives information on how many kind of Red, Green and Blue pixels are present in the image for each 256 types of red, 256 types of green and 256 types of blue.
这段话的意思:
Pillow Image的’直方图方法’,传回的是各个颜色的”数量”。
传回一个list(列表),list内各元素的数字就是该颜色在图片中出现的次数。
如果是RGB图片,可传回三个histogram 代表R G B 三层各自256阶出现的次数。
举例来说:
Histogram() 是个list 长度768
print(f'type(picA.histogram()) : {type(picA.histogram())}')
type(picA.histogram()) : <class 'list'>
len(picA.histogram()): 768
把picA拆成三色
r1, g1, b1 = picA.split()
print(type(r1.histogram()))
把红色前面5个色阶的出现次数,印出来看看
print(r1.histogram()[:5])
[15, 2, 11, 13, 26] Red 色阶0出现15次,色阶1出现2次,色阶2出现11次….

【如何测试本程序】 Source code download

1.选一张图,当原尺寸 arrowA.jpg,另制作一张缩小成一半大小arrowB.jpg
2.可以切换两种方式(原尺寸、相同尺寸),试看看效果如何
标注程序码,切换试试看

# 可以切换两种方式(原尺寸、相同尺寸),试看看效果如何
# 调整成相同尺寸
picA, picB = imgResize(pic1,pic2)

# 依原始尺寸,不调整
#picA = pic1
#picB = pic2

3.了解观察histogram

Histogram() 是个list 长度768

print(f'type(picA.histogram()) : {type(picA.histogram())}')
print(f'len(picA.histogram()): {len(picA.histogram())}')

把红色前5个色阶的出现次数,印出来看看

print(r1.histogram()[:5])
print(len(r1.histogram()))
4.我们来看看picA的histogram 长得像什麽
借用一张图 https://ithelp.ithome.com.tw/upload/images/20210826/20111373nJVp4cSmH9.jpg

#--- plot bar chart of picA.histogram
x = np.arange(0,256)
# 画出 picA 三色 histogram
plt.title('picA histogram')   
plt.bar(x, r1list,color='red')      #红
plt.bar(x,g1list,color='green')     #绿
plt.bar(x,b1list,color='blue')      #蓝
plt.show()

发现此图出现次数多的都偏重在RGB的前面100个色阶,代表这张图偏深色调。
https://ithelp.ithome.com.tw/upload/images/20210826/20111373UIOi8KlpGQ.jpg
三色分开来看
https://ithelp.ithome.com.tw/upload/images/20210826/201113732jRtr60Ihp.jpg
https://ithelp.ithome.com.tw/upload/images/20210826/20111373diTpEow5Ei.jpg
https://ithelp.ithome.com.tw/upload/images/20210826/20111373msVvxWHxnu.jpg
5. 比较两张图片的histogram差异Histogram difference method:
两种计算方式:
第一种【直接相减】

#--- 两图RGB出现次数"直接相减"的简单评估方法
#--- 计算 某一色阶出现次数/此图该色阶出现次数平均值 之 比例% 
#--- red scale 
r1Avg = np.mean(r1list)
print(f'r1Avg {r1Avg}')
rr1 = []
for i in range(len(r1list)):
    rr1.append(100*r1list[i] / r1Avg)
#print('rr1[:10]',rr1[:10])
rr2 = []
r2Avg = np.mean(r2list)
print(f'r2Avg {r2Avg}')
for j in range(len(r2list)):
    rr2.append(100*r2list[j] / r2Avg)
rrD = []                          # 两图红色差异   
for j in range(len(r2list)):
    rrD.append(abs(rr1[j]-rr2[j]))
#print('rrD different % : ',rrD[:10])

#--- 以下略...

第二种【平方根复杂计算式】
颜色在图片中出现次数的不同,差异临界点100以内的,认定是”无差异的”。

#--- 平方根的复杂计算式
def DiffSqrt(pic1,pic2):
    h1 = pic1.histogram()
    h2 = pic2.histogram()
    diff = math.sqrt(reduce(operator.add,
                     list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1))
    '''上一行意思: h1 h2相减-->平方-->平均-->再....-->再开平方根...
    '''
    # threshold 100,如果diff<=100,认定无差异
    if(diff <= 100):
        tmp = '两张图片 无差异'
    else:
        tmp = '两张图片 不相同'
    print(f'{tmp}  diff = :  {diff}')      

结果:
''' 两种方法测试结论:
内容相同的一张图,一张原尺寸 & 一张缩小成一半
切换【原尺寸】或【缩成相同尺寸】两种状况下
直接相减法 : 结论相同
平方根算法:结论不同
'''
衍生出来的疑问是:

  • 以这种方式(颜色差异),如果只是图片,或许还可以接受。
    但是却没有考虑其它因素(特徵、构图…),这样能运用在人脸的辨识吗?
  • 如果,虚拟产生一张图片其中的histogram都符合目标图片,也就骗过了程序?
    ( 改天再写一个试看看 )
  • 这样是真正的有智慧吗? Intelligent?

心存这样的疑惑,我上网去测试
2017年RSNA Radiology Society of North America举办的Bone Age Challenge

冠军者网站

如果,拿一张主办单位提供的资料库X光片测试,就会给出一个完美的骨龄判读结果。
(注:男女孩骨头发育时程不同,所以切换选男女)。
https://ithelp.ithome.com.tw/upload/images/20210826/20111373rzoH1764E7.jpg
出於疑惑心态,我给它一张Lenna.jpg ,它也能判读,也给我一个骨龄结果。傻眼?
Why is that? 它内部真正的比对算法是?
也是RGB? 不过它已先转成GrayScale了,而且提供training的也是PNG档
https://ithelp.ithome.com.tw/upload/images/20210826/20111373YE74TuVK94.jpg

关於Bone Age 後续研究中….
脸部辨识也可以研究一下,最近出现的Intel OpenVINO ….


<<:  WIN10无法开机停在转圈的解决方式

>>:  Rails幼幼班--Rails安装Tailwindcss

分散式资料库:New SQL

分散式资料库可以依据资料模型及系统架构分类; OLAP(On-Line Analytical Pro...

Ruby解题分享--Remove Duplicates from Sorted List

哎~~心法没有,拳法太弱.... Remove Duplicates from Sorted Li...

[Day10] 团队管理:人才定位与任务给予

掌握人才状况 以表现与潜能为人才定位 我们审慎面试人进来後,对於如何有效沟通与解决问题的mental...

[DAY 2] _ 做一块自己的开发板(stm32f030)

第二天我来说一下如何做1块属於自己的开发板,分享我在做STM32的开发板经验,我不会说我的做法是最好...

第 12 天 小有成果保持练习( leetcode 043 )

https://leetcode.com/problems/multiply-strings/ M...