【问题标题】:Finding Variance of an image in OpenCV -Python在 OpenCV -Python 中查找图像的方差
【发布时间】:2015-05-16 22:15:22
【问题描述】:

我试图在 OpenCV -Python 中找到灰度图像的方差。我先把读入的图片分割成子图,我要计算这些子图的方差(cropped_img)。

我不确定如何在 python 中计算方差,我假设我可以计算协方差矩阵以使用规则找到方差:

Var(X) = Cov(X,X)

问题是我不知道如何使用cv2.calcCovarMatrix(),而且我在 python 中找不到任何示例。

我确实在 C++ 中找到了这个示例,但我从未使用过该语言并且我正在努力将其转换为 python:calcCovarMatrix in multichannel image and unresolved assertion error

这是我的代码:

#import packages
import numpy as np
import cv2

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

#Set scale of grid 
scale = 9

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

covar = []
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]

        #Here is where I need to calc variance
        cv2.calcCovarMatrix(cropped_img, covar, meanBGR, cv2.cv.CV_COVAR_NORMAL)
        #???
        cropped_img[:] = covar

cv2.imshow('output_var',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

如果有人有任何想法,或者如果您有更好的方法来计算方差,那么我将非常感激。

谢谢。

编辑:我也在 c 中找到了这个例子; mean and variance of image in single pass,不过好像效率不高。

【问题讨论】:

  • 如果您正在寻找标准开发,它是one-liner
  • @berak 这正是我想要的,干杯:)

标签: python c++ opencv image-processing computer-vision


【解决方案1】:

要在python中获取灰度图像的方差,可以使用numpy。

import numpy as np

var = np.var(img)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多