I'm beginner to openCV. I dowloaded opencv2.4.5 and visual studio express 2012 then i followed this link http://opencv-srf.blogspot.in/2013/05/installing-configuring-opencv-with-vs.html for setup everything in environment variable etc. Then i followed below link http://opencv-srf.blogspot.in/2013/06/load-display-image.html to create sample application. I included proper #include path. But i'm getting error.
#include "stdafx.h"
#include <C:\opencv\build\include\opencv\cv.h>
#include <C:\opencv\build\include\opencv\cxcore.h>
#include <C:\opencv\build\include\opencv\highgui.h>
#include "C:\opencv\build\include\opencv2\highgui\highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
please do not use an absolute path for the includes, this is totally non-portable.
it should look like this:
// the usual suspects:
#include "opencv2\core\core.hpp" // Mat is defined here.
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
also, to make this work, your "additional include folders" should point to "opencv/build/include"
and avoid the old c-api headers, like cv.h, highgui.h, cxcore.h
Related
I wrote some lines of code to simply read and write an image and it was working a few days ago, but now Visual Studio gives these errors: "1>Done building project "Lab1_PI.vcxproj" -- FAILED." and it says it was "unable to start the program" and that the system cannot find the file specified.
I also tried to add add _CRT_SECURE_NO_DEPRECATE and _CRT_SECURE_NO_WARNINGS via right-click project->C/C+±>Preprocessor->Preprocessor Definitions, but nothing changed.
Also, here's my code, but it is correct since it was working a few days ago:
#include <iostream>
#include <string>
#include "opencv2\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main(int arc, const char** argv) {
string imgPath = argv[1];
string img2Path = argv[2];
cv::Mat img = imread(imgPath);
if (!img.data) {
cout << "error:no image" << endl;
return -1;
}
imshow("flower image (color)", img);
Mat img2;
cvtColor(img, img2, COLOR_RGB2GRAY);
imwrite(img2Path, img2);
imshow("flower image (grey)", img2);
waitKey(0);
}
I would be really grateful if someone could help me fix this problem because it stops me from using Visual Studio and I really need it for a project!
I have been using openCv for a while, but have just moved to windows 10.
Now, existing applications will compile, but I cannot make a new one.
In a new project (visual studio 2015, release 64)
I am adding all the libs:
opencv_calib3d310.lib
opencv_core310.lib
opencv_features2d310.lib
opencv_flann310.lib
opencv_highgui310.lib
opencv_imgcodecs310.lib
opencv_imgproc310.lib
opencv_ml310.lib
opencv_objdetect310.lib
opencv_photo310.lib
opencv_shape310.lib
opencv_stitching310.lib
opencv_superres310.lib
opencv_ts310.lib
opencv_video310.lib
opencv_videoio310.lib
opencv_videostab310.lib
setting:
D:\opencv-master\build64\lib\Release;%(AdditionalLibraryDirectories)
and
D:\opencv-master\modules\highgui\include
D:\opencv-master\modules\imgcodecs\include
D:\opencv-master\modules\core\include
D:\opencv-master\modules\videoio\include
D:\opencv-master\modules\imgproc\include
%(AdditionalIncludeDirectories)
and adding the most basic:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "stdafx.h"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window", image); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
In visual studio, Intellisense is fine, no red underlines, everything looks good. When I try to compile tho...
'cv': a namespace with this name does not exist
'Mat': undeclared identifier
'image': undeclared identifier
...and many more. It is like it cannot find the libs, but i am linking them correctly, I am sure of it.
Can anyone assist me here?
I found that this question has been asked many times here, but I haven't found any solution or work-around to this problem. Here's my code (copied from here: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, char** argv)
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I compiled this using Visual Studio 2008 and 2010, and got different results (both don't work). The program compiled using VS 2008 has run-time error at imread(), and the other displays the message "Could not open or find the image".
Anybody can help me with this?
The problem here is your main() function._tmain does not exist in C++. main does.
_tmain is a Microsoft extension. Here is a nice explanation of these two methods.
Further more if you want to add default argument in Visual studio please follow these steps.
Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
Hope this solves your problem!
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
if( argc != 2)
{
cout <<"No Commandline Aurgument Found!: Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
set argv[1] to be a known image page "C:\test.jpg"
Ok, Ive read all the comments and I'm going to answer the main question along with the sub questions.
Why Doesn't my Code Work in VS2008?
The reason your code doesn't work in VS2008 is because you are using the compiled libraries for 2010, at least I think this is a pretty accurate assumption. If you want to be completely accurate then build the libraries, for the compiler you are using.
What is tmain & what is main
This stack overflow question answers the subject a lot better than I ever could, but effectively it is a windows specific main and does not actually exist in C++. It get removes by the compiler on compile time and converter to main.
Can you please try this: Use known-to-work image and absolute path until it works so you can be sure that there is no problem with image or relative path.
download http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png to C:\Lenna.png
rename your main function to something else and try this: if it does not work, please tell me the displayed name of the output window.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
Mat image;
image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.cols ) // Check for invalid input
{
cout << "Could not open or find the image. Press any key to exit." << std::endl ;
cv::waitKey(0)
return -1;
}
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Please try this and if it does not work, please tell me the displayed name of the output window.
I have try to configure visual studio 2013 to program using opencv. My OS is 64 bit and I have followed the below steps.I have extracted the opencv to C:.
Create a console application.
In properties set C/C++ section ->addtional include directories set to C:\opencv\build\include.
In properties set Linker section ->general->Additional Libarary directories set to C:\opencv\build\include\x64\vc12\lib
Then In Linker->input->Additional Dependencies I add following
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_ml249d.lib
opencv_nonfree249d.lib
opencv_ocl249d
opencv_objdetect249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_videostab249d.lib
these are the names of the file in my C:\opencv\build\x64\vc12\lib folder(there are two versions opencv_calib3d249d and opencv_calib3d249(without d at end))
and then I add following code to my cpp file and try to run.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
Mat img = imread("galpatha.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
system("pause");
return 0;
}
it gives me the error
Error 1 error LNK1104: cannot open file 'opencv_calib3d249d.lib' c:\Users\Kasun\documents\visual studio 2013\Projects\OpenCV_test\OpenCV_test\LINK OpenCV_test
I am trying to use graph library using opencv.
This is the code that I have written , and am building this on Visual studio 2010.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "GraphUtils.h"
using namespace cv;
using namespace std;
int main()
{
float floatArray[4]= {1,1,2,2};
showFloatGraph("Rotation Angle", floatArray, 4 );
return 0;
}
There is no compilation error.
However, I am getting following linking error:
Error 1 error LNK2019: unresolved external symbol _showFloatGraph
referenced in function _main C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\opencv1\helloworld.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\Debug\opencv1.exe
I have put the two files (GraphUtils.h and GraphUtils.c) with my .cpp file.
Why I am getting these compilation errors whenever I am using third party libraries (.c and .h) files, since I get this linking error also when using other library. I asked the question here
In Properties=>Linker=>input=>Additional dependencies , I have added :
opencv_core231d.lib;opencv_highgui231d.lib;opencv_imgproc231d.lib;opencv_features2d231d.lib;opencv_calib3d231d.lib;%(AdditionalDependencies)
And in fact my basic opencv program for image display works fine.
Do I need to do something special when using these libraries. As of now I just copy paste the .c and .h files in the folder where my .cpp file is present. Do I need to do something more ?
Update:
I found that the the file GraphUtils.c has the following code:
// OpenCV
#include <cv.h>
#include <cxcore.h>
#ifdef USE_HIGHGUI
#include <highgui.h>
#endif
I found that the cv.h, cxcore.h and highgui.h are actually present in opencv 2.3.1/opencv folder and so I changed it like this:
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#ifdef USE_HIGHGUI
#include <opencv/highgui.h>
#endif
Unfortunately this gave rise to more linking errors.
Update
The following program works fine.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include<conio.h>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("xyz.bmp", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
getch();
return 0;
}
But this program always gives error; no header file recognized. so no function would work:
#include "cv.h" //main OpenCV header
#include "highgui.h" //GUI header
int main() {
// declare a new IplImage pointer
IplImage* myimage;
// load an image
myimage = cvLoadImage("sayyidsmile.jpg",1); //change the file name with your own image
//create a new window & display the image
cvNamedWindow("Smile", 1);
cvMoveWindow("Smile", 100, 100);
cvShowImage("Smile", myimage);
//wait for key to close the window
cvWaitKey(0);
cvDestroyWindow( "Smile" );
cvReleaseImage( &myimage );
return 0;
}
Details on Env. Variables
I followed this article to set EVs.
Currently my variables are like this:
Name Value
OpenCV: C:\Users\Yuvue\Desktop\OpenCV2.3.1
INCLUDE: %OPENCV%\build\include
LIB: %OPENCV%\build\x86\vc10\lib
Path: %OPENCV%\build\x86\vc10\bin
Under Properties => VC++ Directories=> Library Directories, I have added the following:
$(OPENCV)\build\x86\vc10\lib
The second program compiles when I explicitly gives path of cv.h in the include. But if I do so with any of the third party libraries when they have used #include, it starts giving linking error (my original question).
Please help me sort this!!