-5

I'm starting to learn OpenCV in C++. I have a decent working knowledge of the python version of OpenCV, and I was wondering what the following would translate to in python? I'm trying to understand the math of the cv2.warpPerspective() function. My tiny python brain can't wrap its head around it.

w = w != 0.0f ? 1.f / w : 0.0f

So far all I get from this is that if 'w' is not equal to '0' then it will equal '1/w' otherwise 'w' will equal '0'

Is that right?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Jacob W. Dallas
  • 383
  • 3
  • 12
  • 1
    `?:` is a ternary conditional operator meaning if cond (`w != 0`) then return 2nd. arg. (`1.f / w`) else 3rd. arg (`0.0f`). Please, google for C++ condition operator to find more. I believe in Python it would be `1.0 / w if w != 0.0 else 0.0`. – Scheff's Cat Feb 01 '19 at 12:09
  • 2
    *if 'w' is not equal to '0' then it will equal '1/w' otherwise 'w' will equal '0'* — well, it seems you already have your answer. What is your actual question? – Norrius Feb 01 '19 at 12:09
  • 1
    It's equivalent to `if (w != 0.0f) { w = 1.f / w; } else { w = 0.0f; }` – Some programmer dude Feb 01 '19 at 12:10
  • Thank you for the responses, I was checking out https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/c---numbers-and-operators and other sources, but I wasn't sure how all of the pieces fit together for this one line. – Jacob W. Dallas Feb 01 '19 at 12:11
  • Found [SO: Does Python have a ternary conditional operator?](https://stackoverflow.com/a/394814/7478597). (Yepp, I remembered right.) – Scheff's Cat Feb 01 '19 at 12:12
  • @Someprogrammerdude the `else { w = 0.0f; }` part is redundant – Stack Danny Feb 01 '19 at 12:17
  • @StackDanny Yes, but it's still part of the conditional expression and so need to be part of the equivalent statement. Otherwise the statement would not be matching the expression. – Some programmer dude Feb 01 '19 at 12:19
  • @Someprogrammerdude you're right. I assume this is one of the cases where the ternary operator is a bit overkill and a single `if` does just fine. But I suppose the compiler will optimize the ternary away anyway. – Stack Danny Feb 01 '19 at 12:23
  • 1
    @StackDanny Not to mention that the `if` statement is much clearer than the expression. Clearer code means easier to understand it and what it does, and also easier to maintain. The conditional expression is too often misused as a premature optimization in the belief that the compiler won't otherwise generate optimal code. – Some programmer dude Feb 01 '19 at 12:26

1 Answers1

3

yes that is correct. If they would just write

w = 1.0 / w;

division by zero might occur. So adding the ternary operator here is an OK way to handle this. A more intuitive alternative would be:

if (w != 0.0f){
   w = 1.0f / w;
}
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • I'll accept this when the timer is up. – Jacob W. Dallas Feb 01 '19 at 12:12
  • thank you. When asking a question like this next time please consider spending a few more minutes researching so that questions like this can be avoided. Afterall they do not add any value to this site. – Stack Danny Feb 01 '19 at 12:15