Why cvWaitKey(0) doesn't work? - c++

I'm not sure why, but for a mysterious reason my c++ application doesn't wait anymore when it reaches cvWaitKey(0) it just passes this line, like this function doesn't do anything!
I also tried cvWaitKey(100000) it doesn't work either...
void main() {
cvWaitKey(0);
return;
}
My project is a little complex, I'm using Visual Studio 2010 and It includes opencv ffmpeg pthread winsocks and some other libraries.
Can you guess why this happens?

Have you called cvNamedWindow yet? It will not work without cvNamedWindow.

I've had the issue myself a few times, but I can only speculate on what causes this. I can offer a work-around though:
while(1){
int key=cvWaitKey(10);
if(key==27) break;
}
This will block until ESC is pressed.

Related

break when watched expression becomes true

I'm using visual studio code with the c++ extension and gnu debugger on ubuntu 14.04.
I'm setting a watch expression in my program, which works as expected, but I'd like to trigger the debugger to break when that condition becomes true. How can you do that with visual studio code? The documentation is pretty thin on this subject.
Why don't you add a block that you can break at?
For example, make a condition:
while(true)
{
//stuff is happening
if(condition_happened)
{
std::cout<<"things happened"<<std::endl; //break here
}
}

C++ output screen disappears

Why does my C++ output screen disappear immediately? I'm a beginner in cpp. Can anyone help me to find the problem please?
You should either launch your application inside of a terminal, or add a line of code that waits for the input in order for the window to not close. E.g. add in the end of the function main a line:
std::cin.get();
And also add at beginning of the file the include that holds that function.
#include <iostream>
This is hard to answer since there can be many things that can cause your output box to close immediately. First try having a cout statement and then a cin statement. Something like:
cout<<"Hello"<<endl;
cin>>input>>endl;
Also make sure to have the necessary include statement at the top and whatever you want to return at the bottom.
#include<iostream>
return 0;
As you have said that you are beginner in C++, you should keep in mind ,three major things while coding in C++. You've mentioned that your screen disappears , then following things you should try.
1). in C++ conventionally main returns value of type int.And the format of your program should be like...
int main()
{
-------
//body of your program
-------
return 0;
}
If the function returns 0, that means it ran successfully.
2). You have to inlcude #include<iostream> on the top of your program.
3).check whether the IDE you are using is compatibale to your operating system or not.
Hope this will help you.

How to prevent visual studio 2013 console window to close right after running a program? [duplicate]

This question already has answers here:
Keeping console window open when debugging
(3 answers)
Closed 8 years ago.
Hi I am using visual studio express 2013.I have never used vs before and so just to test it out I ran a simple c++ program where the user enters 2 integers and their sum is then displayed. The problem is that the console window appears and takes the inputs, but then immediately closes once the output is displayed. Please note that this happens right after all the inputs are taken and when the output is shown. Is there anyway to fix this? I have looked all over and cant find a solution. I have tried including a bunch of things such as the getch() function at the end of my program, and pressing ctrl F5 to debugg my programs,but nothing seems to work. Please help!!!
I have been using this,
int getchar ( void );
Get character from stdin
Returns the next character from the standard input (stdin).
OR
use this from process.h
system("PAUSE");
This approach is for beginners. It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.
One decent approach is Debug.WriteLine
// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;
int main()
{
Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
Trace::AutoFlush = true;
Trace::Indent();
Trace::WriteLine( "Entering Main" );
Console::WriteLine( "Hello World." );
Trace::WriteLine( "Exiting Main" );
Trace::Unindent();
Debug::WriteLine("test");
}
In debug mode, Before your main return you could put a breakpoint and you will be able to see your console and the result you are waiting for.
Put
system("PAUSE");
wherever you need the program execution window to pause. If you're using int main(), usually it'll be right before you return 0

Error: cannot find World!.map: No such file or directory. Using Atmel studio 6 and CEENBot API

So I am making my Hello world program, and it is like this:
#include "lib-includes/capi324v221.h"
void CBOT_main(void)
{
LCD_open();
if (!LCD_OPEN) {
while (true); // loop forever
}
else {
LCD_printf("Hello, World!");
while(true); // loop forever
}
}
Can anyone help with this? I am sorry if it is a little vague; it is all the info I know for the most part.
First of all what are those while loops doing there? My first reaction is to get rid of those.
I have no personal experience with programming CEENBots\your IDE, but based on the error description in the question title I suspect your source file has a ! in it which messes up the build process (it complains about a World!.map file, best to keep special characters out of filenames).
edit:
To extend upon what Öö Tiib states below, get rid of the space and comma too :)

Using cvQueryFrame and boost::thread together

I need to call cvQueryFrame (to capture a frame from a webcam with opencv) instead a thread created with boost. Here is a little example code:
void testCVfunc(){
IplImage* frame;
CvCapture *capture;
capture = cvCreateCameraCapture(CV_CAP_ANY);
if(!capture){
exit(1);
}
frame = cvQueryFrame(capture);
cvNamedWindow("testCV", 1);
while(frame = cvQueryFrame(capture)){
if(!frame){
exit(2);
}
cvShowImage("testCV", frame);
cvWaitKey(1);
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
int main(){
//Method 1: without boost::thread, works fine
testCVfunc();
//Method 2: with boost::thread, show black screen
char entree;
boost::thread threadTestCV = boost::thread(&testCVfunc);
std::cin >> entree;
}
As the comments say, testCVfunc does its job if I don't call it from a boost::thread, but I get a black screen if I use boost::thread.
I don't get the problem, maybe someone does?
Thank you for your help.
I've seen some problems when OpenCV is being executed from a secondary thread and it's difficult to pinpoint the origin of the problem when the behavior is not consistent on all platforms.
For instance, your source code worked perfectly with OpenCV 2.3.0 on Mac OS X 10.7.2. I don't know what platform you are using, but the fact that it worked on my computer indicates that OpenCV has some implementation issues with the platform you are using.
Now, if you can't move OpenCV's code to the primary thread, then you might want to start thinking about creating a 2nd program to handle all OpenCV related tasks, and use some sort of IPC mechanism to allow this program to communicate with your main application.
I solved the problem by calling
cvCreateCameraCapture(CV_CAP_ANY);
in the main thread, even if it doesn't really answer the question:
why is this not working? question.
Hope this can help someone else.
Try calling cv::startWindowThread(); in the main app and then creating a window within your thread. This worked for me.