gpt4 book ai didi

python - 如何检测并绘制 asc 文件的强度

转载 作者:行者123 更新时间:2023-12-01 04:45:04 32 4
gpt4 key购买 nike

我有火焰照片 - 数据为包含像素矩阵的 asc 文件。每个像素中都是光强度值。

我的绘图代码:

import os
import numpy as np
import matplotlib.pyplot as plt
# Open a file
path = "input/"
dirs = os.listdir( path )
number_of_files = 0

# This would print all the files and directories
for file in dirs:
if file.endswith(".asc"):
img = np.genfromtxt (path+file)

file = os.path.splitext(file)[0]
#pasukamas vaizdas
img = img.transpose ()
# Just a figure and one subplot
fig, ax = plt.subplots(1,figsize=(20,20))

ax.set_title(file, fontsize=60, y=1.03)
plt.imshow (img, interpolation='nearest', origin='lower')

plt.colorbar ()
plt.savefig ('output/' + file + '.png', bbox_inches = 'tight')

number_of_files = number_of_files + 1
plt.close(fig)
print (number_of_files)

结果: plotted image

如何绘制 3 个范围内的图像:

  1. 从最大强度到 36000(红色区域)
  2. 从最大强度到 27000(黄色区域)
  3. 从最大强度到 12000(蓝色区域)

并检测大多数底部和顶部像素?还有最左和最右的像素?然后用线连接顶部和底部像素。左右像素需要相同的东西。如何显示线条上的像素距离?

结果必须是这样的 wanted result

最佳答案

您似乎想要图像的阈值版本,然后在阈值上进行区域标记,然后再进行一些奇特的测量。

<小时/>

为了方便起见,我将形成图像序列的 3D ndarray ,以便可以使用 numpy 一次性完成任何操作:

fileList = filter(lambda s: s.endswith(".asc"), os.listdir(path))

# open first image so we have its dimensions to initialize the array
firstImage = np.genfromtxt (path+fileList[0])

imageArray = np.zeros((len(filelist),) + firstImage.shape)

现在我们分配值

imageArray[0,:,:] = firstImage
for i,file in enumerate(filelist[1:]):
# skip the first item because we already have it
imageArray[i+1,:,:] = np.genfromtxt (path+file)
<小时/>

好的,现在我们有了图像的 3D 数组,所以让我们获取范围图像

boolMaskRedzone = imageArray > 36000 
boolMaskYellowzone = imageArray > 27000
boolMaskYellowzone = imageArray > 12000

这些现在是与图像大小相同但由 bool 值组成的蒙版。让我们来摆弄它:

redParts = image*boolMaskRedZone # images with 0 for thresholded values
plt.imshow(redParts[0,:,:],cmap="hot")

再次注意,redParts 和其他所有内容仍然是 3D,因此我们制作了数组的 2D View 以用于绘图目的。

<小时/>

现在是简单/有趣的部分:标签!我们可以使用scipy.ndimage.measurements.label()

from scipy.ndimage import label
labelsRed, nbLabelsRed = label(boolMaskRedzone)

labelsRed 现在是一个以整数作为标签索引的数组。

理想情况下,我们应该有 nbLabelsRed == 1,如果没有,“岛屿”可以用以下方法关闭

from scipy.ndimage import morphology
closedLabels = morphology.binary_closing(labelsRed)
# fiddle with the optional iterations parameter if needed

我们可以通过使用 np.where 为我们提供像素位置,然后计算项目数量来计算标签面积 = 阈值区域:

x,y,z = np.where(labelsRed == 1) # x, y ,z are arrays
area = len(x) # the number of pixels that are brighter than red

至于计算顶部/底部最像素,如果您希望该线是对角线,那么它可能会变得很棘手,但如果您只想顶部/底部(与图像轴对齐),您可以在 mask 变为时进行 numpy 检查对于每个轴都是如此,这显然是在数组和偏移版本之间进行差异(推导),然后沿每个轴的第一个非零元素

differenceArray = boolMaskRedZone - np.roll(boolMaskRedZone,1,axis=1)
# now check along each column when the array first becomes True
uselessImageIndex,xTopMostPixel,yTopMostPixel= numpy.where(differenceArray == True)
# found all the crossings and put them in 2 arrays
<小时/>

为了精确的对角线测量,您可能需要查看专门的图像测量库,例如 scikit-image ,他们可能有你想要的

如果你真的想自己做,我会推荐一些基于查找对象中心、然后计算对角线位置并测量最大长度的方法,但是如果你找到 45 度线会发生什么?是“从上到下”还是“从左到右”?或者你想要接近水平线的最长线?或者您想要双正交测量? (选择最大的线作为第一次测量,第二条直径线是与第一条线成 90 度的线的长度)

假设你在图像中有你的点,绘图只是一个带有 plt.plot =) 的线图

我必须承认我没有对推导部分进行太多思考,但我认为一旦你拥有了标签,你就会成为一个快乐的露营者 =)

编辑:当然,所有这些操作都可以通过迭代数组来直接计算,但我只发布了一种使用 numpy 的数组操作效率来生成单行代码的方法。您确实可以通过执行

来完成每个操作
for x in range(image.shape[0]):
for y in range(image.shape[1]):
if(image[x,y] > 36000):
mask[x,y]=True

但是与 numpy 的编译函数和硬件加速函数相比,这种循环嵌套非常慢(请参阅 http://www.astro.washington.edu/users/vanderplas/Astr599/notebooks/11_EfficientNumpy 了解 python/numpy 上的速度演示)

编辑 2:对于我的另一个项目,我一直在研究 scipy 的 ndimage 函数,并且有一些适合您的东西:ndimage.center_of_mass() .

此函数查找(可能已标记的)数组中的质心。通过找到标记数组的质心,您就可以找到对角线的轴中心,其余的只是小菜一碟^^

关于python - 如何检测并绘制 asc 文件的强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637117/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com