简体   繁体   English

获取光栅图像中所有像素的像素坐标

[英]get pixel coordinates of all pixels in a raster image

I have a raster image of shape 9000x10000 that has RGB bands. 我有一个具有RGB波段的形状为9000x10000的光栅图像。 I use the below code to get the XY coordinates of all pixels in the image. 我使用下面的代码来获取图像中所有像素的XY坐标。 But it is very slow. 但这很慢。 Is there a faster way to do it? 有更快的方法吗?

filename='file.dat'
inDs = gdal.Open(filename)
outDs = gdal.Translate('{}.xyz'.format(filename), inDs, format='XYZ', creationOptions=["ADD_HEADER_LINE=YES"])

I want to save the XY coordinates and the pixel values in a dataframe. 我想将XY坐标和像素值保存在数据框中。

If your raster file has a GeoTransform attribute, you can try this: 如果您的栅格文件具有GeoTransform属性,则可以尝试以下操作:

import gdal
import pandas as pd

def ix2xy(r,c,gt):
    '''Gets x,y from row and column'''
    x = gt[0] + r * gt[1]
    y = gt[3] + c * gt[5]
    return(x,y)

This little function gets the X/Y coordinates from the GeoTransform attribute which is a tuple with (xorigin, xres, 0, yorigin, 0, yres) . 这个小函数从GeoTransform属性获取X / Y坐标,该属性是一个具有(xorigin, xres, 0, yorigin, 0, yres)的元组。

ds = gdal.Open('file.dat')
gt = ds.GetGeoTransform()


df = pd.DataFrame.from_records(itertools.product(range(ds.RasterYSize),range(ds.RasterXSize)),columns=['Row','Column'])

ds = None

df['X'], df['Y'] = zip(*df.apply(lambda x: ix2xy(x['Column'],x['Row'],gt),axis=1))

This should give you a tidy dataframe with the columns Row , Column , X and Y . 这应该为您提供一个整齐的数据框,其中包含RowColumnXY Column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从坐标参考系统中的多边形边界获取栅格中的像素坐标 - Get pixel coordinates in raster from polygon boundary in coordinate reference system 如何获取一个像素周围的所有 position 个像素 - how to get all the position of pixels around a pixel Python GDAL:使用一系列像素值计算光栅图像的像素数 - Python GDAL: Count the number of pixels of the raster image using a range of pixel values 如何从图像中获取“1 像素宽”坐标? - How to get "1 pixel wide" coordinates from image? 如何获取二进制图像中 4 个角白色像素的像素位置 - How to get pixel locations of 4 corner white pixels in a binary image 使用 python 查找图像中黑色/灰色像素的所有坐标 - Find all coordinates of black / grey pixels in image using python 如何用matplotlib imread获取所有蓝色像素的坐标? - How to get the coordinates of all blue pixels with matplotlib imread? 从 Python 中的二进制图像获取像素边界坐标(不是边缘) - Get pixel boundary coordinates from binary image in Python (not edges) 对FITS图像进行过采样后,从ra,dec获取像素坐标 - Get pixel coordinates from ra, dec after oversampling FITS image 在pyrender中将场景渲染为图像后如何获取对象的像素坐标? - How to get pixel coordinates of object after rendering the scene as image in pyrender?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM