计算方差图像python [英] Calculating variance image python

查看:495
本文介绍了计算方差图像python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来计算使用Python / NumPy / Scipy的图像上的运行方差过滤器?通过运行方差图像,我的意思是计算图像中每个子窗口I的总和((I - mean(I))^ 2)/ nPixels。

由于图像相当大(12000x12000像素),我想要避免格式之间转换数组的开销,只是为了能够使用不同的库,然后转换回来。



<我想我可以通过使用类似的手段来手工完成这个工作。

  kernel = np.ones((winSize,winSize ))/ winSize ** 2 
image_mean = scipy.ndimage.convolve(image,kernel)
diff =(image-image_mean)** 2
#计算winSize *图片
#子样本结果

但是有更好的东西像stdfilt-功能从Matlab。

任何人都可以指向我有一个函数库的方向,并支持numpy数组,或提示在NumPy / SciPy?

解决更简单的解决方案,也更快:使用SciPy的 ndimage.uniform_filter

  import numpy as np 
from scipy import ndimage
rows,cols = 500,500
win_rows,win_cols = 5,5

img = np。 random.rand(rows,cols)
win_mean = ndimage.uniform_filter(img,(win_rows,win_cols))
win_sqr_mean = ndimage.uniform_filter(img ** 2,(win_rows,win_cols))
win_var = win_sqr_mean - win_mean ** 2

而不是可读性。
generic_filter 比步幅慢20倍...


Is there an easy way to calculate a running variance filter on an image using Python/NumPy/Scipy? By running variance image I mean the result of calculating sum((I - mean(I))^2)/nPixels for each sub-window I in the image.

Since the images are quite large (12000x12000 pixels), I want to avoid the overhead of converting the arrays between formats just to be able to use a different library and then convert back.

I guess I could do this manually by finding the mean using something like

kernel = np.ones((winSize, winSize))/winSize**2
image_mean = scipy.ndimage.convolve(image, kernel)
diff = (image - image_mean)**2
# Calculate sum over winSize*winSize sub-images
# Subsample result

but it would be much nicer to have something like the stdfilt-function from Matlab.

Can anyone point me in the direction of a library that has this functionality AND supports numpy arrays, or hint at/provide a way to do this in NumPy/SciPy?

解决方案

Simpler solution and also faster: use SciPy's ndimage.uniform_filter

import numpy as np
from scipy import ndimage 
rows, cols = 500, 500
win_rows, win_cols = 5, 5

img = np.random.rand(rows, cols)
win_mean = ndimage.uniform_filter(img, (win_rows, win_cols))
win_sqr_mean = ndimage.uniform_filter(img**2, (win_rows, win_cols))
win_var = win_sqr_mean - win_mean**2

The "stride trick" is beautiful trick, but 4 slower and not that readable. the generic_filter is 20 times slower than the strides...

这篇关于计算方差图像python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆