【发布时间】:2020-03-14 17:28:48
【问题描述】:
我有一个基本上需要两个图像的代码,大图像和小图像。小图像被缩小为一行图像,然后从大图像的每一行中减去。结果应该是具有不同值的新大图像。
两个图像都是 ndarray(超过 2 个维度)。 当我在一行上运行此代码时,它可以工作,但是当我尝试使用 for 循环以便在图像中的所有行上运行它时,它永远不会停止。
图片细节: - 大图目前有 11 行 1024 列。 - 缩小的小图只有 1 行 1024 列。
这是代码:
import spectral.io.envi as envi
import matplotlib.pyplot as plt
import os
from spectral import *
import numpy as np
#Create the image path
#the path
img_path = r'N:\path\Image_Python\13-8-2019\emptyname_2019-08-13_11-05-46\capture'
resized_path=r'N:\path\Image_Python'
#the specific file
img_dark= 'DARKREF_emptyname_2019-08-13_11-05-46.hdr'
resized_file='resize3.hdr'
#load images
img_dark= envi.open(os.path.join(img_path,img_dark)).load()
resized= envi.open(os.path.join(resized_path,resized_file)).load()
wavelength=[float(i) for i in resized.metadata['wavelength']]
#reduce image into 1 row
dark_1024=img_dark.mean(axis=0)
#the follow command works and was compared with the image in ENVI
#resized[0] suppoose to be row no. 0 in image resized
#so the problem is in the for loop
resized[0]-dark_1024
#Here I have tried to run at the beginning my computation but then it took too much so I tried to run #this count in order to see how many rows it iterate through
#I have tried this also with a== 3,000,000 and it got there
a=0
for i in resized[0,1]:
a=a+1
print(a)
if a==8000:
break
我的最终目标是能够使用 for 循环为我的 n 维图像中的每一行运行“resize-dark_1024”进程
说明: 每当我跑步时:
调整大小[i]-dark_1024[i]
当 i 是一个数字时。例如 i=3, i-4...
有效
编辑 2:如果我使用 dark_1024 运行它,它有 1 行 1024 像素:
a=0
for i in dark_1024:
a=a+1
print(a)
if a==8000:
break
最多计数到 1024:
【问题讨论】:
-
即使
if a=8000无法正常工作它会打印8001, ...? -
我怀疑 resized[0,1] 并不意味着你认为它的意思。你希望这会产生什么?
-
@SimonHibbs 我认为它会产生 11 的行数
-
@Tserenjamts 它不打印 8001 但它只是检查它是否在某个时候停止
-
我也尝试过:for i in resized, for i in resized[0] 但结果相同
标签: python for-loop numpy-ndarray spectral spectral-python