在 OpenCV 中查找图像方差时的意外输出 -Python

Unexpected output when finding variance of an image in OpenCV -Python

我的程序在网格化图像的每个 window 处找到图像的方差值。问题是当我打印的值与输出图像中显示的值不匹配时。我在下面包含了一个示例图片。

这是我的代码:

#import packages
import numpy as np
import cv2
import dateutil
import llist
from matplotlib import pyplot as plt
import argparse

#Read in image as grey-scale
img = cv2.imread('images/0021.jpg', 0)

#Set scale of grid 
scale = 6

#Get x and y components of image
y_len,x_len = img.shape

variance = []
for y in range(scale):
    for x in range(scale):
        #Crop image 9*9 windows
        cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale,(x*x_len)/scale:((x+1)*x_len)/scale]

        (mean,stdv) = cv2.meanStdDev(cropped_img)
        var = stdv*stdv
        cropped_img[:] = var

        #Print mean_values array
        variance.append([var])
variance=np.asarray(variance)
np.set_printoptions(suppress=True, precision=3)
print variance.reshape(1,scale,scale)

cv2.imshow('output_var',img)
#cv2.imwrite('images/output_var_300.jpg',img,[int(cv2.IMWRITE_JPEG_QUALITY), 90])
cv2.waitKey(0)
cv2.destroyAllWindows()

这是上面代码的输出图像:

据我所知,下面的值与上面的图片不符。有人知道这里发生了什么吗?

print variance.reshape(1,scale,scale)
#[[[    17.208     43.201    215.305   1101.816   1591.606   2453.611]
#  [    46.664    121.162    326.59     809.223   1021.599   5330.989]
#  [    47.754     64.69     705.875   1625.177   3564.494  10148.449]
#  [    19.153    201.864    289.258    632.737   5285.449   4257.597]
#  [    37.621    159.51     271.725    282.291   2239.097    759.007]
#  [    26.108     98.456     32.958    505.609    575.916     70.741]]]

提前致谢。

编辑: 这里有一个更真实的输出图像,供有兴趣的人使用:

我们以variance的第二行为例。由于每个通道的颜色值在 0-255 范围内,我们可以尝试包装您的值以适应该范围:

>>> row = [46.664, 121.162, 326.59, 809.223, 1021.599, 5330.989]
>>> wrapped = [x % 256 for x in row]
>>> wrapped
[46.66, 121.16, 70.58, 41.22, 253.59, 210.98]

瞧,现在说得通了。