我正在尝试使用Python和skimage将纹理图像实现为described in this tutorial。
问题是将一个7x7窗口移动到一个大光栅上,并用从7x7窗口计算的纹理替换每个像素的中心。我用下面的代码做到了这一点,但我看到没有其他的方法,只不过是循环通过每个像素,这是非常缓慢的。
一个软件包会在几秒钟内完成,所以肯定有其他方法。。。有?
这里的代码工作,但非常缓慢。。。
import matplotlib.pyplot as plt
import gdal, gdalconst
import numpy as np
from skimage.feature import greycomatrix, greycoprops
filename = "//mnt//glaciology//RS2_20140101.jpg"
outfilename = "//home//max//Documents//GLCM_contrast.tif"
sarfile = gdal.Open(filename, gdalconst.GA_ReadOnly)
sarraster = sarfile.ReadAsArray()
#sarraster is satellite image, testraster will receive texture
testraster = np.copy(sarraster)
testraster[:] = 0
for i in range(testraster.shape[0] ):
print i,
for j in range(testraster.shape[1] ):
#windows needs to fit completely in image
if i <3 or j <3:
continue
if i > (testraster.shape[0] - 4) or j > (testraster.shape[0] - 4):
continue
#Calculate GLCM on a 7x7 window
glcm_window = sarraster[i-3: i+4, j-3 : j+4]
glcm = greycomatrix(glcm_window, [1], [0], symmetric = True, normed = True )
#Calculate contrast and replace center pixel
contrast = greycoprops(glcm, 'contrast')
testraster[i,j]= contrast
sarplot = plt.imshow(testraster, cmap = 'gray')
结果:
我有同样的问题,不同的数据。下面是我编写的使用并行处理和滑动窗口方法的脚本:
此脚本计算定义的窗口大小的GLCM属性,相邻窗口之间没有重叠。
相关问题 更多 >
编程相关推荐