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

时间:2015-04-27 11:09:13

标签: python opencv image-processing computer-vision variance

我的程序在网格图像的每个窗口中找到图像的varaince值。问题是当我打印出与输出图像中显示的值不匹配的值时。我在下面列出了一个示例图片。

这是我的代码:

#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()

以下是上述代码的输出图像: Scale = 6

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

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]]]

提前谢谢。

编辑:对于有兴趣的人来说,这是一个更逼真的输出图像: scale = 300

1 个答案:

答案 0 :(得分:1)

让我们以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]

瞧,现在有道理了。