opencv +python 提取roi目标区域全部像素的值 得出上下限 均匀值

cv2.imread后返回的值是一个矩阵,而我需要全部像素的值,不需要矩阵排布

所以:

利用

h, w, _ = img3.shape

得到区域的长宽,也就是像素的行数和列数

再用 for 循环,逐个打印

for a in range(h):
    for b in range(w):
        print(img3[a,b])
        jihe.append(list(img3[a,b]))
        num +=1

就可以得到整齐的一字排开的像素值

[ 98 125 255]
[ 97 124 255]
[ 96 123 254]
[ 94 121 252]
[ 93 120 251]
[ 92 119 250]
[ 91 118 249]
[ 90 117 248]
[ 98 125 255]
[ 97 124 255]
[ 95 122 253]
[ 93 120 251]
[ 92 119 250]
[ 91 118 249]
[ 89 116 247]
[ 87 114 245]
[ 97 124 255]
[ 96 123 254]
[ 94 121 252]
[ 94 121 252]
[ 93 120 251]
[ 92 119 250]
[ 90 117 248]
[ 88 115 246]
[ 93 120 251]
[ 92 119 250]
[ 92 119 250]
[ 93 120 251]
[ 94 121 252]
[ 93 120 251]
[ 91 118 249]
[ 90 117 248]
[ 93 120 251]
[ 93 120 251]
[ 95 122 253]
[ 96 123 254]
[ 97 124 255]
[ 95 122 253]
[ 95 122 253]
[ 95 122 253]
[ 95 122 255]
[ 94 121 254]
[ 95 122 255]
[ 97 124 255]
[ 97 124 255]
[ 96 123 255]
[ 95 122 255]
[ 95 122 255]
[ 92 119 252]
[ 90 117 250]
[ 89 116 249]
[ 92 119 252]
[ 93 120 253]
[ 92 119 252]
[ 90 117 250]
[ 90 117 250]
[ 92 119 252]
[ 88 115 248]
[ 88 115 248]
[ 91 118 251]
[ 94 121 254]
[ 93 120 253]
[ 91 118 251]
[ 90 117 250]
[ 92 119 252]
[ 90 117 250]
[ 89 116 249]
[ 91 118 251]
[ 92 119 252]
[ 92 119 252]
[ 92 119 252]
[ 93 120 253]
[ 89 116 249]
[ 89 116 249]
[ 90 117 250]
[ 93 120 253]
[ 94 121 254]
[ 93 120 253]
[ 93 120 253]
[ 93 120 253]
[ 84 110 246]
[ 86 112 248]
[ 88 114 250]
[ 90 116 252]
[ 90 116 252]
[ 90 116 252]
[ 90 116 252]
[ 91 117 253]
[ 83 109 245]
[ 85 111 247]
[ 87 113 249]
[ 86 112 248]
[ 85 111 247]
[ 85 111 247]
[ 87 113 249]
[ 89 115 251]
[ 86 112 248]
[ 87 113 249]
[ 87 113 249]
[ 86 112 248]
[ 83 109 245]
[ 83 109 245]
[ 86 112 248]
[ 88 114 250]
[ 85 111 247]

再求RGB均值

sumx = sumy = sumz = 0
for i in range(num):
[x, y, z] = jihe[i]
sumx = sumx + x
sumy = sumy + y
sumz = sumz + z

r=int(sumx/num)
g=int(sumy/num)
b=int(sumz/num)
print(r, g, b)
print(num)
print(jihe)
print('集合长度%d' % (len(jihe)))

colors_change = np.uint8([[[b,g,r]]])
hsv_change = cv2.cvtColor(colors_change,cv2.COLOR_BGR2HSV)
print(hsv_change)

得出HSV平均值

这里的HSV是opencv下的,与别处不同

For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. Different softwares use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges.

H:0-179

S: 0-255

V:0-255

但其他软件经常见H也是两百多的

posted @ 2018-03-26 16:40  Mrfri  阅读(12517)  评论(0编辑  收藏  举报