-1

What is the best way to code this condition in C++?

I tried to code it, but it alredy looks too complicated and has errors when compiling

                     if(alpha <= hsv_image[2].at<float>(i,j)/hsv_bkg[2].at<float>(i,j))
                                {
                         D = min(...);
                                    if( abs(hsv_image[1].at<uchar>(i,j)-hsv_bkg[1].at<uchar>(i,j))<=T_value && D<=T_value){
                                        ...

                                    }
                                }
                }
  • 3
    `...` is not valid syntax. Please show a [mre] with code that reproduces your problem and the exact error message – Alan Birtles Mar 07 '20 at 13:09
  • 1
    What is the `∧` symbol in the picture representing? Is it really xor or is it a [logical conjunction](https://en.wikipedia.org/wiki/Logical_conjunction)? – Ted Lyngmo Mar 07 '20 at 13:18
  • 1
    this is AND operator –  Mar 07 '20 at 13:23
  • Also A is && of two inequalities – stark Mar 07 '20 at 13:28
  • 2
    The general way (not only in programming) to deal with complexity is *divide et impera,* or ["divide and conquer".](https://www.open.edu/openlearn/science-maths-technology/approaches-software-development/content-section-1.5) In programming it's one of the central tenets. In this particular case: Assign the various expressions to aptly named variables and perform the top-most computations and the comparisons with those. Add more intermediate layers when necessary. Pay attention to variable names: They are perhaps the single most important contribution to understanding a program. – Peter - Reinstate Monica Mar 07 '20 at 13:32
  • A while ago I gave an example for naming and decomposition into smaller, more manageable pieces [here.](https://stackoverflow.com/a/33241669/3150802) – Peter - Reinstate Monica Mar 07 '20 at 13:36
  • Since this is a OpenCV code, I added OpenCV tag. – Afshin Mar 07 '20 at 13:44

2 Answers2

1

From what I can read out of it, you could calculate the three inner conditions first:

auto val1 = IVt(x,y) / BVt(x,y);
bool cond1 = alpha <= val1 && val1 <= beta;

auto val2 = ISt(x,y) - BSt(x,y);
bool cond2 = val2 <= Ts;

bool cond3 = Dh <= Th;

And then the result should be:

bool result = cond1 && cond2 && cond3;
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

From what I see, you are using OpenCV. I'm right now learning it myself and I'm not expert, but I see that you are trying to do operations pixel by pixel. This causes a HUGE performace issue in OpenCV. You need to operate on whole picture at once.

For example, your equation can be written in OpenCV as something like this(maybe it has bugs, it came quickly off top of my head and I'm learning myself):

Mat mask1, mask2, sub, res;
divide(IV, BV, mask1);
inRange(mask1, Scalar(alpha), Scalar(beta), mask2);
abs(IS - BS, sub);
auto D = min(...);
bitwise_and(sub, D, res, mask2);
Afshin
  • 8,839
  • 1
  • 18
  • 53