Why doesn't this OpenCV code do anything? - c++

I've been trying to use OpenCV with C++ but even though my code compiles (Visual Studio 2010 ), it doesn't ever do anything:
#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cvwimage.h"
#include "cxcore.h"
#include "cxmisc.h"
#include "ml.h"
using namespace cv;
using namespace std;
int main()
{
namedWindow("yolo", WINDOW_AUTOSIZE );
waitKey(1);
cout << "Why won't this show up?" << endl;
}
It compiles OK, without errors but the program doesn't do anything - when I open it in console, it doesn't return the 'Why won't this show up?" text - there is nothing returned.
Regardless of which tutorial piece of code I am trying to use, it never works and never does anything.
What is going on?
Best regards
EDIT: When I set the wait time to 0 (forever) it still doesn't work.

The window does get created, however, because you have the waitKey function set to 1 millisecond it only exists for a very short period of time. Try using:
waitKey(0);

waitKey(1); is the culprit. the minimum time is 1 millis (that#s what you got here)
either make it :
waitKey(0); // forever
or increase the time to something sensible
waitKey(5000); // 5 secs

It needs to be,
#include <cv.h>
#include <highgui.h>
cv::namedWindow("test_1", CV_WINDOW_AUTOSIZE );
cvWaitKey();
std::cout << "This will work because I just tested it." << endl;
I use CMake to link the libraries. The CMake code is,
FIND_PACKAGE( OpenCV REQUIRED )
TARGET_LINK_LIBRARIES( myProject ${OpenCV_LIBS} )

Related

How do I make a portable program with OpenCV2 on C++?

I have installed OpenCV on Windows 10 and using C++ in Visual Studio.
Now I'm trying to make a program that makes several photos then saves them.
Problem:
I made a copy of project on USB flash (go to another PC) and when I try to start it from .exe file, I get this error:
How can start it without installing 1Gb of FULL OpenCV libraries?
I tried to start from release version.
My C++ source:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h> // For Sleep
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap(0);
// Get the frame
Mat save_img; cap >> save_img;
if (save_img.empty())
{
std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
}
// Save the frame into a file
imwrite("test.jpg", save_img); // A JPG FILE IS BEING SAVED
return 0;
}
My settings:
P.S. I want to make light program for making photo on Windows. And how to make real portable programs.

Visual Studio 2015 OpenCV Assertion failed (size.width>0 && size.height>0) in cv::imshow windows.cpp

I've been learning a bit of c++ lately and i keep getting the same error for some reason.
Apparently, imshow isn't able to find the file.
My codes are
#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include<iostream>
#include <conio.h>
using namespace std;
using namespace cv;
int main()
{
cv::Mat img;
img = imread("1.png");
if (img.empty())
{
cout << "can't find da file\n\n" ;
}
cv::namedWindow("test");
cv::imshow("test",img);
waitKey(0);
}
I've tried putting it in the project folder along with the ".exe",
I tried listing the whole directory of the image using "/" and "\"
I've also tried not using any extensions in the code but to no avail.
Error Signs
Thanks for helping you guys :)
I always advise the same thing for these cases. It just needs a little lateral thinking. Change your code to this
int main()
{
ofstream test("test.txt");
test << "I'm here!!\n";
}
Now run this program, it will create a file called test.txt. Find out where that file is on your system and that's where you should put your 1.png file. Then you can go back to your original program confident that the file is in the right place and any remaining issues are a different problem.

Eclipse OpenCV Undefined reference and Binary not found

This is the code that I am trying out for a basic OpenCV program. I am using OpenCV 2.4.6.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lenna.png", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
Also I am using MinGW and am including the required libraries using this guide. On building all the OpenCV functions are declared as undefined references and the compilation generates the error message saying Launch failed. Binary not launched.
I checked the answer given here but on applying the changes I still cannot get it to work. Where am I going wrong?
I am using OpenCV 2.4.6 and a 64-bit windows 7.
It sounds like the compiler/linker can't find OpenCV.
Where is it installed ? Are you sure the headers and dynamic libraries are available to Eclipse ?
Are you sure you are linking against it ?

VideoWriter not working on Win7-64Bit VM

I coded the below which tries to hold a picture and count number hundred and finally grab all them to a video with following codec MP42(MPEG4-V2)
#include <stdAfx.h>
#include <iostream>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int n=0;
char a;
int main(int, char** argv)
{
Mat image= imread("C:\\Users\\Metin\\Desktop\\images.jpg");
Mat newimage;
VideoWriter vide ("Hopdedik.avi",CV_FOURCC('M','P','4','2'),30.0,image.size());
vide.open("Hopdedik.avi",CV_FOURCC('M','P','4','2'),30.0,image.size());
for(int n=0; n<100;n++){
cout<<n<<'\n';
std::string s= to_string(static_cast<long long>(n));
image.copyTo(newimage);
putText(newimage,s,Point(100,40),cv::FONT_HERSHEY_COMPLEX,2,cv::COLORMAP_COOL,2,2,false);
imshow("asd",newimage);
if(!vide.isOpened())cout<<"can not opened";
vide.write(newimage);
waitKey(10);
}
vide.release();
return 0;
}
I started debugging on Release mode and copied the .exe files to Virtualbox Win7-64 bit. Right after I put necessary .dll files exe works but the code can't properly create the video.
I have tried to install following two codecs:
http://www.fourcc.org/downloads/angel-potion-mpeg-4--8/
and Indeo which is: http://www.afreecodec.com/codec-by-code/iv45-160.html
But it did not work.
Once I faced a similar problem where opencv couldn't read a video. There was no warning at all. The reason was that an automated string maker resulted in "opencv_mmpeg.dll" which was missing from the system. So, I debugged up to cap_ffmpeg.cpp and found this:
#if defined WIN32 || defined _WIN32
const char* module_name = "opencv_ffmpeg"
CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
"_64"
#endif
".dll";
So, copy opencv_ffmpeg_64.dll to your folder and you might have some results. Else, you need to debug deeply. Start by inserting into your project the file opencv/modules/highgui/src/cap.cpp (and the headers needed to be compiled) so you can debug line by line. I suppose many other files will be needed to go further down into the code.

C++ Auto Format

I am trying to create a program that auto formats my f:\ (USB Drive), and I have this code so far:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
int main ()
{
system ("format f:");
system("pause");
return 0;
}
Now, when I run that, it comes up, with me needing to press Y, then Enter. How can I simulate that?
As Tyler said in a comment, a batch file would be much easier, but this might work:
system( "echo y | format f:" )