X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 15 Jan 2010 09:28 AM by  anon
Band Math Conditional Statements
 4 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
15 Jan 2010 09:28 AM
    An earlier post (by area_51) was very helpful in describing the use of conditional statements in band math. I have a follow-on question that I hope someone can address. Using this earlier post as an example, I expanded this to produce -999 as a background, and to calculate a more complex expression for my non-background values: exp = '(b1 le 0) * (-999) + (b1 gt 0 )*(float(b2) + float(b1))' This works great -  I can add/multiply/subtract those bands in the expression for my non-background pixels, and all original background zeros become -999.  However, if I DIVIDE the bands in the expression, all of my background values become -NaN. I understand that I would get NaN's for a divide by zero, but zeros should not be encountered since those cases fall under the first condition. Any suggestions would be greatly appreciated.

    Deleted User



    New Member


    Posts:
    New Member


    --
    18 Jan 2010 11:01 AM
    Can you tell us exactly where you're putting the division in the expression?  Maybe just paste in the expression with the division, that'd be helpful.  And what exactly are you trying to do by putting in the division?  I think we need to know those things before we can help.

    Deleted User



    New Member


    Posts:
    New Member


    --
    18 Jan 2010 04:00 PM
    Sure thing.  I am trying to apply an equation to each of my valid pixels in a georegistered image (which has background values of zeros along the edges)..  In the simplest case, this could just be an nir/red index.  If I can get that to work, then my more complex equation should work. The reason that I am concerned about the background values, is that I want to then mosaic a number of scenes together (this is low atltitude aircraft imagery, so lots of scenes).  Right now I'm trying to get the background values to change to -999, so that I can confirm that the conditional statement is working (hard to tell if I tell it to make them zeros, because 0+0=0) This equation works fine as shown below, and also works if I b1 and b2 are multiplied or subtracted. The operation is performed on all image values, and background values all become -999. exp = '(b1 le 0) * (-999) + (b1 gt 0 )*(float(b2) + float(b1))' However, the following equation results in all background values = -NaN exp = '(b1 le 0) * (-999) + (b1 gt 0 )*(float(b2) / float(b1))' another  'fix' to this processing bottleneck could be to batch up the removal of the -NaNs, but right now I can only do that interactively - In other parts of my processing stream, I batch up the masking process, but I haven't been able to figure out how to mask out NaNs in batch mode. Thanks for your help -M  

    Deleted User



    New Member


    Posts:
    New Member


    --
    18 Jan 2010 10:01 PM
    Ok, here's where I think you're getting confused.  The leftmost term is what's turning the background values to -999 - this expression:  (b1 le 0)*(-999).  But the thing is, the *result* of that expression has -999's in it, not the *input* values to it.  So b1 itself still has zeroes as background.  So later in the rightmost part when you do (float(b2)/float(b1)), b1 still has zeroes as background and there's your divide by zero error.  So, while this is probably going to make for a very complicated expression, you need to validate the input images(b1, b2, etc.) each time you use them (or at least every time you're going to be dividing with them).  If you just want to divide two bands and have the resulting image have -999 as a background, you might try something like this (this is very off the top of my head, so no promises that it actually works, but it should get you started):   (  (b2 le 0)*(-999) + float(b2)   )   /  (  (b1 le 0) + float(b1)  )   So in the numerator, (b2 le 0) is the mask that gives you a 1 on a background pixel, 0 for non-background, then you multiply the 1's and 0's by -999, so that changes background pixels to -999, and all the rest are still zero.  Add that to b2, and the resulting numerator is either -999 at background, or b2 where not background.  Then in the denominator, the mask this time remains as all 0's or 1's, then is added to b1 in the same manner as the numerator.  And when you finally do the ratio, anything not a background value is going to have a resulting pixel value of  b2/b1, and the background pixels will have values of -999/1 = -999. I think should work, or at least get you out of the gate.  One more thing, you seem to be using MATH_DOIT.  If it were me writing code to do all this, if my band math expression got too complicated, i'd be tempted to just go ahead and write the code that does the math myself, skipping MATH_DOIT, just so it'd be easier to come back to later. Hope that helps, Jeff

    Deleted User



    New Member


    Posts:
    New Member


    --
    23 Nov 2010 04:03 AM
    Good day! I would like to ask one more question on this topic! I need to find all pixels with a zero value and replace them with the value 'NaN' to create 'mean value' composite. Thank you very much!!! I am looking forward to the reply! With kind regards, Zoya
    You are not authorized to post a reply.