gpt4 book ai didi

python - 在 OpenCV -Python 中查找图像的方差

转载 作者:行者123 更新时间:2023-11-28 02:30:53 25 4
gpt4 key购买 nike

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

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

变量(X) = 冠状病毒(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 , 但它似乎不太有效。

最佳答案

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

import numpy as np

var = np.var(img)

关于python - 在 OpenCV -Python 中查找图像的方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com