1

I imported an image with three bands. And I entered each band into a numpy array.

Now I try to modify the value of band 1, conditional on band 3.

However, my image has many zero values and must be computed with the exception of zero to speed up the operation.

I think it is faster to find values after excluding the 0 value.

Below is the code I used to do.

cols = 0 
rows = 0
[cols,rows] = test.shape
i= 0
i2 = 0

while i < cols:
    k = 0
    k2 =0
    while k <rows:
        if 0.15>test[i,k]>0.05089 and  30> test3[i,k]>29.8  :
            test[i,k] = 1
....
10
  • 2
    You may want to consider giving an example. Right now your use of pronouns makes it very difficult to understand what you want. Commented May 6, 2018 at 14:23
  • For example, I want to find a value of 1 in the array and double it. Commented May 6, 2018 at 14:24
  • Please edit your question and provide some code. Commented May 6, 2018 at 14:25
  • Corrected the contents. If you do not understand, please comment again. Commented May 6, 2018 at 14:32
  • Your example is bad and you clearly haven't tried running it yourself. You can't start a variable name with a number, and you can just do img *= 2. Please show something that can actually be run. Consider an example involving addition. Commented May 6, 2018 at 14:35

1 Answer 1

2

Well it looks like what you want is to select a "mask" and assign to it. Your example is a bit strange and incomplete, but you could achieve what I think you're intending to achieve by replacing the loop with:

test[(0.15>test) & (test>0.05089) & (30>test3) & (test3>29.8)] = 1

What's going on here:

  • (0.15>test) create a boolean array the same size as test with all elements < 0.15 set to True and the rest False.
  • The & operators do an elementwise logical AND with the other boolean arrays to make a new boolean array (also the same size as test and test3)
  • test[XXX] = 1 means "take all elements of XXX which are true, and set the corresponding elements of test to 1" (it's assumed that XXX has the same shape as test (or can be broadcasted to the same shape))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your interest in this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.