图像处理基础,等大小图片相似度匹配
基于RGB通道的像素点相似度匹配算法思路
def compare_by_RGB(image_1,image_2): """ 基于通道和的差 :param image_1: :param image_2: :return: """ G_1 = 0 B_1 = 0 R_1 = 0 G_2 = 0 B_2 = 0 R_2 = 0 #第一个图像矩阵通道和 for x in image_1: for y in x: G_1 += y[0] B_1 += y[1] R_1 += y[2] #第二个图像矩阵通道和 for x in image_2: for y in x: G_2 += y[0] B_2 += y[1] R_2 += y[2] #图像矩阵各通道相似度 inc_G = 1 - math.fabs(G_1 - G_2) / G_2 inc_B = 1 - math.fabs(B_1 - B_2) / B_2 inc_R = 1 - math.fabs(R_1 - R_2) / R_2 dec = (inc_G + inc_B + inc_R) / 3 return dec
根据不相似的像素点统计
def compare_by_pixe(pic1, pic2): """ 方法二,基于像素点相似数量统计 :param pic1: :param pic2: :return:totlepix 像素点个数,diffcount 相似度 ,nptg,ptg """ res={} #获取行数列数和通道数 sp1 = pic1.shape sp2 = pic2.shape #获取矩阵大小 res['totlepix'] = pic1.size # pix*3 矩阵大小等于像素点数乘以3 #判断两张图片的行列数是否相同 if sp1 != sp2: ret = "The two picture is in Different range" return ret DiffCount = 0 #遍历矩阵行和列 for index1 in range(sp1[0]): for index2 in range(sp1[1]): (b1, g1, r1) = pic1[index1, index2] (b2, g2, r2) = pic2[index1, index2] if (b1, g1, r1) != (b2, g2, r2): DiffCount = DiffCount + 1 res['totlepix'] = res['totlepix'] / 3 #相似度=不同的像素点数/像素点总数 res['diffcount'] =1- DiffCount/ res['totlepix'] return res
主函数测试
if __name__=="__main__": #相似度阈值 threshold_value=0.75 path="./lab1_data/lab1_video.mp4" cap=cv2.VideoCapture(path) count=0 if cap.isOpened(): #锁,用来判断是否为第一帧 flag=False while 1: ret,frame=cap.read() #缩小图片 image=cv2.resize(frame,(32,32),interpolation=cv2.INTER_CUBIC) if flag==True: res=compare_by_pixe(image, temp) if res["diffcount"]