判斷一幅圖像是否是紅外還是可見光,直接在rgb顏色空間不好弄,需要轉化到hsv空間進行判別,具體就是統計s通道的值是否爲0或接近0,這樣一來可以直接加和s通道的值,然後取個均值,最後給個閾值,當這個均值小於給定閾值時,就可以認爲這幅圖像就是紅外的,具體python程序如下:
import os,sys
import numpy as np
def judgeimingmode(impath):
infraredthresh=10
im = cv2.imread(impath)
if im is None:
print(impath)
return None
imshape = im.shape
hsvim = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
schannelmeanval = np.sum(hsvim[:, :, 1]) / (imshape[0] * imshape[1])
if schannelmeanval < infraredthresh:
imagingmode = 'hongwai'
else:
imagingmode = 'kejianguang'
# print('{} imagingmode={}'.format(impath, imagingmode))
return imagingmode