简体   繁体   English

Python 中的循环永无止境

[英]Never ending for loop in Python

I have a code that basically takes two images,big image and small image.我有一个基本上需要两个图像的代码,大图像和小图像。 the small image is being reduced into one row image and then is being subtracted from each row of the big image.小图像被缩小为一行图像,然后从大图像的每一行中减去。 The result should be new big- image with different values.结果应该是具有不同值的新大图像。

both images are ndarray (more then 2 dimensions ).两个图像都是 ndarray (超过 2 个维度)。 When I run this code on one row, it works, but when I try to use for loop in order to run it on all the rows in the image, it never stops.当我在一行上运行此代码时,它可以工作,但是当我尝试使用 for 循环以便在图像中的所有行上运行它时,它永远不会停止。

details of the image: -The big image has currently 11 rows with 1024 columns.图片的详细信息: - 大图目前有 11 行 1024 列。 -The small reduced image has 1 row only with 1024 columns. - 小型缩小图像只有 1 行,1024 列。

This is the code:这是代码:

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

My end goal is to be able to run the process "resize-dark_1024" for each row in my n-dimensional image using for loop我的最终目标是能够使用 for 循环为我的 n 维图像中的每一行运行进程“resize-dark_1024”

clarification: Whenever I run:澄清:每当我跑步时:

resized[i]-dark_1024[i]调整大小[i]-dark_1024[i]

when i is a number.当我是一个数字。 for example i=3, i-4...例如 i=3, i-4...

it works有用

edit 2: If I run this with the dark_1024,which has 1 row with 1024 pixels:编辑2:如果我用dark_1024运行它,它有1行1024像素:

a=0
for i in dark_1024:
    a=a+1
    print(a)
    if a==8000:
        break

it counts up to 1024:它计数到 1024:

An easy way to accomplish what you want is by using numpy's broadcasting capability.使用 numpy 的广播功能来完成你想要的一个简单的方法。 For example, I'll create a dummy dark array.例如,我将创建一个虚拟dark阵列。

In [1]: import spectral as spy

In [2]: import numpy as np

In [3]: img = spy.open_image('92AV3C.lan').load()

In [4]: dark = np.random.rand(*img.shape[1:]).astype(img.dtype)

In [5]: print(img.shape, dark.shape)
(145, 145, 220) (145, 220)

To be able to subtract dark from all rows of img , we just need to create a dummy index for the first dimension so numpy can broadcast the operation.为了能够从img的所有行中减去dark ,我们只需要为第一个维度创建一个虚拟索引,以便 numpy 可以广播该操作。

In [6]: y = img - dark[None, :, :]

And just to verify that it worked, make sure that the difference between y and img is equal to dark across multiple rows.为了验证它是否有效,请确保yimg之间的差异在多行中等于dark

In [7]: dark[:2, :2]
Out[7]: 
array([[0.38583156, 0.08694188],
       [0.79687476, 0.24988273]], dtype=float32)

In [8]: img[:2, :2, :2] - y[:2, :2, :2]
Out[8]: 
array([[[0.3857422 , 0.08691406],
        [0.796875  , 0.25      ]],

       [[0.3857422 , 0.08691406],
        [0.796875  , 0.25      ]]], dtype=float32)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM