使用GDAL和Python的最小距离算法 [英] Minimum Distance Algorithm using GDAL and Python

查看:405
本文介绍了使用GDAL和Python的最小距离算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GDAL和Python实现最小距离算法用于图像分类。在计算出样本区域的平均像素值并将其存储到数组列表( sample_array)中之后,我将图像读取到称为值的数组中。使用以下代码,我遍历此数组:

I'm trying to implement the Minimum Distance Algorithm for image classification using GDAL and Python. After calculating the mean pixel-value of the sample areas and storing them into a list of arrays ("sample_array"), I read the image into an array called "values". With the following code I loop through this array:

values = valBD.ReadAsArray()

# loop through pixel columns
for X in range(0,XSize):

    # loop thorugh pixel lines
    for Y in range (0, YSize):

        # initialize variables
        minDist = 9999
        # get minimum distance
        for iSample in range (0, sample_count):
            # dist = calc_distance(values[jPixel, iPixel], sample_array[iSample])

            # computing minimum distance
            iPixelVal = values[Y, X]
            mean = sample_array[iSample]
            dist = math.sqrt((iPixelVal - mean) * (iPixelVal - mean)) # only for testing

            if dist < minDist:
                minDist = dist
                values[Y, X] = iSample

classBD.WriteArray(values, xoff=0, yoff=0)

此过程需要很长时间才能生成大图像。这就是为什么我要问是否有人知道一种更快的方法。我对python中不同变量的访问速度了解不多。也许有人知道我可以使用一个库。
预先感谢,马里奥

This procedure takes very long for big images. That's why I want to ask if somebody knows a faster method. I don't know much about access-speed of different variables in python. Or maybe someone knows a libary I could use. Thanks in advance, Mario

推荐答案

您肯定应该使用NumPy。我使用一些相当大的栅格数据集,NumPy通过它们进行刻录。在我的机器上,使用下面的代码,对于1000 x 1000阵列没有明显的延迟。

You should definitely be using NumPy. I work with some pretty large raster datasets and NumPy burns through them. On my machine, with the code below there's no noticeable delay for a 1000 x 1000 array. An explanation of how this works follows the code.

import numpy as np
from scipy.spatial.distance import cdist

# some starter data
dim = (1000,1000)
values = np.random.randint(0, 10, dim)

# cdist will want 'samples' as a 2-d array
samples = np.array([1, 2, 3]).reshape(-1, 1)

# this could be a one-liner
# 'values' must have the same number of columns as 'samples'
mins = cdist(values.reshape(-1, 1), samples)
outvalues = mins.argmin(axis=1).reshape(dim)

cdist() 计算从中的每个元素到 samples 中的每个元素的距离。这将生成一个1,000,000 x 3的数组,其中每行 n 到原始数组中的像素 n 到每一行的距离样本值 [1、2、3] 中。 argmin(axis = 1)为您提供沿每一行的最小值的索引,这是您想要的。快速重塑可以为您提供图像所需的矩形格式。

cdist() calculates the "distance" from each element in values to each of the elements in samples. This generates a 1,000,000 x 3 array, where each row n has the distance from pixel nin the original array to each of the sample values [1, 2, 3]. argmin(axis=1) gives you the index of the minimum value along each row, which is what you want. A quick reshape gives you the rectangular format you'd expect for an image.

这篇关于使用GDAL和Python的最小距离算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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