Ask Your Question
1

ELSE runs even after IF runs!

asked 2013-08-26 01:05:29 -0600

sinaras gravatar image

updated 2015-08-24 13:14:16 -0600

Hey Guys & Gals! I have an odd problem with OpenCV.

I am trying to implement a calibration program based on calibration example of opencv but with more features. The problem is, when in class "Settings" the program reaches the following line in Debug mode, it runs perfectly:

Mat nextImage()
{
Mat result;
if( inputCapture.isOpened() )
{
   Mat view0;
   inputCapture >> view0;
   view0.copyTo(result);
}
else if( atImageList < (int)imageList.size() )
   result = imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR);
        return result;
}

Assuming I am taking images from my WebCam, when I am trying to trace the code in Release mode, after IF validates (isOpened()==true) and filling the Result with view0, OpenCV runs the ELSE part too!!!, and thus destoying the result and creating an empty view!. As far as I know if IF validates, ELSE shouldn't validate! :)) but I've no idea why this happens! Remember that my code runs perfectly in Debug mode!

OpenCV:2.4.6 / Visual Studio 2012 / Windows 7 SP1 64bit / Active Configuration: 64bit

edit retag flag offensive close merge delete

Comments

First of all, small remark. We suggest not to use hashtags for the tags in the future. They generate a lot of double tags, rendering the search and filter options useless. Keep this in mind at your next post, adjusted it for you now. Like @Michael Burdinov said, keep in mind that in visual studio you need seperate settings for debug/release. Also, make sure that you have your resources at a fixed path for starters, then work on relative paths if that works.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-26 01:46:29 -0600 )edit

Sorry, I just added what the editor suggested, nothing from myself. I'll consider this in future. Thanks for noting.

sinaras gravatar imagesinaras ( 2013-08-26 02:25:18 -0600 )edit

Like I said, it is just a small remark. We are in contact with the forum administrator to actually remove the hashtags, meaning tons of topics will loose their tags in the process. Making people using the correct ones for now, will keep it more clear.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-26 02:32:47 -0600 )edit

6 answers

Sort by ยป oldest newest most voted
2

answered 2013-08-26 07:03:31 -0600

Rui Marques gravatar image

updated 2013-08-26 08:30:34 -0600

I think your problem is as simple as a missing bracket... Here:

else if( atImageList < (int)imageList.size() ) **{**

UPD: I just noticed your usage of brackets to change the scope of the variables. I guess your code has some weird syntactic glitch or I am misunderstanding C++. To make sure, i would set all the brackets like this:

Mat nextImage(); // missing ; here?
{
Mat result;
if( inputCapture.isOpened() )
{
  Mat view0;
  inputCapture >> view0;
  view0.copyTo(result);
} 
else if( atImageList < (int)imageList.size() ) 
{
   result = imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR);        
}
return result;
}
edit flag offensive delete link more

Comments

1

Then there would be no return in the if part. The return result needs to be outside of the if else construct.

Moster gravatar imageMoster ( 2013-08-26 08:00:22 -0600 )edit

Yes I will fix that.

Rui Marques gravatar imageRui Marques ( 2013-08-26 08:29:18 -0600 )edit
2

answered 2013-08-26 01:36:51 -0600

Michael Burdinov gravatar image

OpenCV is just library, i.e. collection of functions. It can not "run else together with if" or do anything like that. If your program got inside 'else' that means condition in 'if' was false and program didn't got inside it. Are you sure you set all the required input arguments inside Visual Studio? Don't forget that setting for Debug and Release modes may be entirely different (input arguments, working directory and so on).

edit flag offensive delete link more

Comments

That's right. But remember condition for IF is true (which is isOpen()) and that's why it runs the lines inside its bracket. But it also runs the line for ELSE. this means that the return value from isOpen from highgui library is somehow corrupted and thus makes Visual Studio validate it as true and false at the same time! I posted it here because I think this is related to highgui library. My settings for Debug and Release are 100% correct and work perfectly for other programs I write (which do not access IO devices)

sinaras gravatar imagesinaras ( 2013-08-26 02:28:25 -0600 )edit

@sinaras If your code goes on the else statement, that means the if() returned false. That is guaranteed by the C++ language, no need to find crazy explanations for the opposite. Just go find why that isOpened() returns false. Most probably, in Debug mode, it cannot find the video you are trying to open, and Michael said.

sammy gravatar imagesammy ( 2013-08-26 06:22:52 -0600 )edit

@sammy please note that the statements in IF and ELSE are both run!

sinaras gravatar imagesinaras ( 2013-08-27 18:13:02 -0600 )edit

@sinaras, what sammy and the others are trying to say is that having IF and ELSE both running is something completely impossible. Why are you so sure that they both were called? In what way did you check that?

Michael Burdinov gravatar imageMichael Burdinov ( 2013-08-28 00:59:51 -0600 )edit
1

answered 2013-09-02 08:49:56 -0600

sinaras gravatar image

Please see this 1 minute video... It explains my problem... http://www.youtube.com/watch?v=nJWDN01-dAI

edit flag offensive delete link more

Comments

Can you try to add a return function in the if, and see if you get to the else part in release mode?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-02 09:03:24 -0600 )edit

Also why is there in debug two variables in your stack trace, view0 and result, and in release only the view0 variable? :) Just noticed after watching the video 10 times or so.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-02 09:10:19 -0600 )edit

Also check this C# problem, with predefined debug and release stuff which leads to similar problems. Did you do any of this? http://stackoverflow.com/questions/2104099/c-sharp-if-then-directives-for-debug-vs-release

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-02 09:15:07 -0600 )edit
1

Don't debug in release mode and expect it to be 100% accurate! Optimization can confuse the debugger, leading to things you see in your video. Disable optimization in release mode and see if it still happens.

Notas gravatar imageNotas ( 2013-09-02 11:13:13 -0600 )edit
1

As @Notas said you can't trust debugger when running in Release mode. Optimization changes your code. Some parts of code/variables may be removed, merged, splitted, changed and so on. Debugger may be confused by those changes. I saw multiple times that debugger shows wrong values of variables when running in Release mode. Using 'cout' is the safest way to know if some part of code really happened or not.

And just to complete your debugging test: use 'step in' instead of 'step'. If both IF and ELSE statement are accessed debugger will get both inside 'copyTo' function and 'imread' function. If not, you will know that everything is OK.

Michael Burdinov gravatar imageMichael Burdinov ( 2013-09-02 13:43:48 -0600 )edit
1

@StevenPuttemans: Thanks for watching the video thoroughly. You're right, that's why I suspected opencv core libraries since it cannot initialize a Mat object for the result variables.

sinaras gravatar imagesinaras ( 2013-09-03 16:02:44 -0600 )edit
0

answered 2013-09-02 09:20:45 -0600

berak gravatar image

if complexity is your problem, cut it into pieces !

Mat nextImage() {
    // 1st try:
    if( inputCapture.isOpened() ) {
        Mat result, view0;
        inputCapture >> view0;
        view0.copyTo(result);
        return result;
    }
    // 2nd try:
    if( atImageList < (int)imageList.size() ) {
        return imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR);
    }
    // fallback
    return Mat();
}
edit flag offensive delete link more
0

answered 2013-09-03 16:13:09 -0600

sinaras gravatar image

PROBLEM SOLVED! Thanks to all you friends for helping me out. I changed my compiler options as suggested by @Notas, and everything is now OK :) Property Pages -> C++ -> Optimization. set optimization to Disabled(/Od). My code now runs perfectly in release mode and I don't feel any changes in speed. This little change cost me 3 weeks of code search and compiler testing. Thanks all again :)

edit flag offensive delete link more

Comments

However this is a bad practice! Release code isn't meant for debugging purposes. When building in release now you will not get optimizations and thus not profit of the actual speedups you can get. You should keep debugging in the debug mode, then build a release client once it works to have optimal performence.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-04 01:35:28 -0600 )edit

@StevenPuttemans: I exactly did this. The reason for debugging a release code is that the release code doesn't execute properly while the debug code does. Funny thing is, this problem is now solved in debugging mode of visual studio for release, but occurs every 3 out of 10 executions of the code. I still suspect the core release library that is responsible for creation of Mat objects.

sinaras gravatar imagesinaras ( 2013-09-04 04:25:37 -0600 )edit

You still do NOT get the fact that there doesn't exist a debuggingmode for release. It is possible, but it makes no sense and it is not intended for doing so ... If you want for whatever reason possible to be possible you need to add explicit commands like imshows or couts to make sure that each part of the code is executed or not in the release 'debugging'. However, this is not how you should do it. Mat creation works perfectly, this has nothing to do with the openCV code. You will get similar behaviour with pure C++ code.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-04 05:00:11 -0600 )edit

I get it. and most of the time I get check my program via cout, .... I'm aware that code/binary aligning is sometimes replaced and not accurate. Using cout, I found out where the problem is in my final excutable (release) and then focused on these particular part of the code. The debugging example is just to show what my problem is (I'm used to this type of debugging since I am a PHP programmer :) )

sinaras gravatar imagesinaras ( 2013-09-04 08:24:32 -0600 )edit
0

answered 2013-08-26 05:51:28 -0600

updated 2013-08-26 08:30:12 -0600

Solution that was suggested in the comment and seemed to fix the problem. Removed some remarks that were actually wrong, sorry for that.

By introducing some extra brackets, you can solve your problem.

 if (first condition) {
    //do bla bla bla
 }
 else{
    // Call this bla bla bla if first one fails
    if (condition 2){
       // If condition is also correct, do this
    }
 }
edit flag offensive delete link more

Comments

The AND should be replaced by the double 'ampersant' sign but the forum doesn't allow me to use it.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-26 05:57:59 -0600 )edit

Thanks Steven, but the second structure couldn't fix that either. The funny thing is that if I replace the following line in my code: result = imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR); with this: cout<<"blah blah";

then the second IF wouldn't validate! and the program would run perfectly! in your code (on top of this comment), after the first IF validates, the trace will go to ELSE and without checking the condition 2 for IF would directly run it's code (i.e. result = imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR);) !!! This is the weirdest thing I have seen until now. I changed it with this: imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR).copyTo( result);

Still runs! but place cout<<"Blah Blah" and comment the above line, and the cout<< wouldn't run! :(

sinaras gravatar imagesinaras ( 2013-08-26 06:17:48 -0600 )edit

Ye, actually sinaras is right. As soon as the first condition is valid (which seems to be according to him), the other condition(s) are not getting checked anymore. At least this is how every c++ compiler should handle it. Im not really familiar with VS, so maybe there is some setting that breaks this.

Moster gravatar imageMoster ( 2013-08-26 06:24:07 -0600 )edit

The issue seems to be that you try to trace/debug on release mode. This is not really functional: http://stackoverflow.com/questions/16305637/why-is-my-program-going-into-both-an-if-statement-and-its-corresponding-else-sta

Moster gravatar imageMoster ( 2013-08-26 06:31:21 -0600 )edit

Thanks Moster. But the problem is, this happens even after I build my project in RELEASE mode and not just when tracing it. That is, my program never captures a frame from camera (it actually captures, but the image is corrupted during this process).

sinaras gravatar imagesinaras ( 2013-08-26 07:32:03 -0600 )edit

No Steven, the 'else if' statement is always executed only if the condition in the 'if' is wrong.

yvo2m gravatar imageyvo2m ( 2013-08-26 07:36:30 -0600 )edit

I have corrected my answer. I admit that I was wrong in my assumption.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-26 08:56:56 -0600 )edit

Question Tools

Stats

Asked: 2013-08-26 01:05:29 -0600

Seen: 1,505 times

Last updated: Sep 03 '13