# -*- coding: utf-8 -*-
import cv2, matplotlib
import numpy as np
import matplotlib.pyplot as plt
pixel=cv2.imread('2.jpg')
print 'pixel=',pixel
average_img = np.average(pixel, axis=0)
print 'average_img=',average_img
average_img_weights = np.average(pixel, axis=0,weights=[1./4,2,2]) # pixel 3X3
print 'average_img_weights=',average_img_weights
average_color = np.average(average_img, axis=0)
average_color = np.uint8(average_color)
print 'average_color=',average_color
average_color_img = np.array([[average_color]*500]*500, np.uint8)
cv2.imshow('average',average_color_img)
cv2.imwrite('avarage.jpg',average_color_img)
cv2.waitKey(0)
函数原型为:average(a, axis =None, weights=None) : 依给定轴axis计算数组a相关元素的加权平均值
np.average() 对于3X3的矩阵是各个行列相应位置元素平均值计算;见下图
截图一块小图片,其像素描述:
pixel= [
[[ 6 15 29] [ 13 22 36] [104 114 131]]
[[ 12 21 35] [ 42 51 65] [164 174 191]]
[[ 0 0 11] [ 14 23 37] [142 152 169]]
]
执行python 代码可知
average_img= [[ 6. 12. 25. ]
[ 23. 32. 46. ]
[ 136.66666667 146.66666667 163.66666667]]
average_img_weights= [[ 6. 10.76470588 23.35294118]
[ 27.11764706 36.11764706 50.11764706]
[ 150.11764706 160.11764706 177.11764706]]
average_color= [55 63 78]
计算过程可见 pixel ==>>average_img 未加权值参数weights
6=(6+12+0)/3
23=(13+42+14)/3
........
计算过程可见 pixel ==>>average_img 加权值参数weights
暂未看懂。。。。。 看懂小伙伴给我留言
关于axis描述:
标签:颜色,img,color,average,openCV,weights,图像,np,pixel From: https://blog.51cto.com/u_12504263/5718747