使用字符串定义Numpy数组切片 [英] Using a string to define Numpy array slice
问题描述
我有一个图像数组,其X值Y形状为2048x2088. x轴有两个20个像素区域,一个在开始,一个在结尾,用于校准主图像区域.要访问这些区域,我可以像这样对数组进行切片:
I have an image array that has an X times Y shape of 2048x2088. The x-axis has two 20 pixel regions, one at the start and one at the end, which are used to calibrate the main image area. To access these regions I can slice the array like so:
prescan_area = img[:, :20]
data_area = img[:, 20:2068]
overscan_area = img[:, 2068:]
我的问题是如何在配置文件中定义这些区域,以便将该切片用于其他可能具有不同预扫描和过扫描区域并因此需要不同切片的摄像机的一般化.
My question is how to define these areas in a configuration file in order the generalise this slice for other cameras which may have different prescan and overscan areas and therefore require a different slice.
理想情况下,类似于下面的字符串的内容将允许在特定于相机的配置文件中进行简单表示,但是我不确定如何将这些字符串转换为数组切片.
Ideally, something like the strings below would allow a simple representation in the camera specific configuration file, but I am not sure how to translate these strings into array slices.
prescan_area_def = "[:, :20]"
image_area_def = "[:, 20:2068]"
overscan_area_def = "[:, 2068:]"
也许我很想念某些东西?
Maybe there is something obvious that I am missing?
谢谢!
推荐答案
您可以执行以下操作:
var1="img"
prescan_area_def = "[:, :20]"
并使用eval
prescan_area=eval(var1+prescan_area_def)
这篇关于使用字符串定义Numpy数组切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!