im new to open cv. im using Open CV 2.4.9 with visual Studio 2010. Im using a basic program to open an image. the problem is that when i put the image file on the main drive (eg: D:\image.png), it works correctly.
but when i move the image to a subdirectory (eg:D:\OpenCV\opencv\build\doc\opencv-logo.png), the output says "image cannot be loaded"
where am i going wrong. please help me out
here is the code that im using:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat im = imread("D:\OpenCV\opencv\build\doc\opencv-logo.png");
if (im.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
imshow("Image", im);
waitKey(0);
}
You need to understand about escape characters
try this
Mat im = imread("D:\\OpenCV\\opencv\\build\\doc\\opencv-logo.png");
You will have to escape the \ character. You have to do this :
Mat im = imread("D:\\OpenCV\\opencv\\build\\doc\\opencv-logo.png")
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 have application made with QTCreator. It uses OpenCV 2.4.11. I have the same application on Visual. Code is the same in both.
OpenCV for Microsoft Visual 2013 was made using this link:
https://www.youtube.com/watch?v=e_TQ9c3n_d8
It is for 2.4.10, but it's the same for 2.4.11.
And I configured QtCreator with this tutorial:
How to link opencv in QtCreator and use Qt library
Now the code:
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <fstream>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2\stitching\stitcher.hpp"
using namespace cv;
using namespace std;
void ReadPhotos();
double begin_t, end_t;
int photo_number = 0;
Mat photos[100];
Mat image;
vector< Mat > ImagesVector;
vector<Mat> roisVector;
int main()
{
cout << "Starting program!" << endl;
ReadPhotos();
Size size(1050, 600);
for (int i = 0; i < photo_number; i++){
//resize(photos[i], photos[i], size);
ImagesVector.push_back(photos[i]);
}
Stitcher stitcher = Stitcher::createDefault(true);
stitcher.setWarper(new SphericalWarper());
stitcher.setFeaturesFinder(new detail::SurfFeaturesFinder(300, 3, 4, 3, 4));
stitcher.setRegistrationResol(0.9);
stitcher.setSeamEstimationResol(0.9);
stitcher.setCompositingResol(1);
stitcher.setPanoConfidenceThresh(1);
stitcher.setWaveCorrection(true);
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
stitcher.setFeaturesMatcher(new detail::BestOf2NearestMatcher(false, 0.3));
stitcher.setBundleAdjuster(new detail::BundleAdjusterRay());
Stitcher::Status status = Stitcher::ERR_NEED_MORE_IMGS;
try{
status = stitcher.stitch(ImagesVector, image);
}
catch (cv::Exception e){}
imwrite("panorama.jpg", image);
waitKey(0);
return 0;
}
void ReadPhotos(){
string sourceIN;
string sourcePhoto;
sourceIN = "paths1.txt";
ifstream FileIN(sourceIN);
if (FileIN.is_open())
{
while (getline(FileIN, sourcePhoto)){
photos[photo_number] = imread(sourcePhoto, 1);
photo_number++;
}
}
else{
cout << "Can't find file" << endl;
}
cout << "Number of photos: " << photo_number << endl;
}
Read photos function takes path to images from txt file and load photos.
In visual studio it's working good, I can stitch 3500x2000 resulution images and it gives nice output panorama. In QtCreator I try to stitch the same images and it gives following errors:
OpenCV Error: Insufficient memory (Failed to allocate 290519044 bytes) in OutOfMemoryError, file C:\OpenCV2411\opencv\sources\modules\core\src\alloc.cpp, line 52
terminate called after throwing an instance of 'cv::Exception'
what(): C:\OpenCV2411\opencv\sources\modules\core\src\alloc.cpp:52: error: (-4) Failed to allocate 290519044 bytes in function OutOfMemoryError
Bad alloc - sorry, I can't make this error again so can't copy exactly.
Anyone has any idea why the same code is not working on QTCreator and is working in Visual Studio 2013? Library is the same. One thing I can think of is that I build libraries for QTCreator myself with Cmake. Maybe it has something with this.
I'm following the tutorial here for setting up OpenCV with Visual Studio (I have 2013 Community edition and OpenCV 2.4.10).
I have the following folder structure:
OpenCVTest
-OpenCVTest.sln
+x64
+Debug
- opencv_core2410d.dll
- opencv_highgui2410d.dll
- OpenCVTest.exe
- OpenCVTest.ilk
- OpenCVTest.pdb
- feck.png
And my source:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#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.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;
}
When I run this in VS or on the command line, I get the following error:
C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\x64\Debug>OpenCVTest.exe feck.png
Could not open or find the image
Anybody know why this might be happening?
Update
I have tried giving the full path:
image = imread("C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\feck.png", IMREAD_COLOR); // Read the file
and placing the image at the same level as the .sln file, still no luck.
Place your image in OpenCVTest folder like this:
C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\OpenCVTest\feck.png
-OpenCVTest.sln
-OpenCVTest <- **HERE**
-ipch
+x64
+Debug
- opencv_core2410d.dll
- opencv_highgui2410d.dll
- OpenCVTest.exe
- OpenCVTest.ilk
- OpenCVTest.pdb
- feck.png
In the tutorial here, under "The local method", it reads:
Then you need to specify the libraries in which the linker should look
into. To do this go to the Linker ‣ Input and under the “Additional
Dependencies” entry add the name of all modules which you want to use:
opencv_core231d.lib
opencv_imgproc231d.lib
opencv_highgui231d.lib
opencv_ml231d.lib
opencv_video231d.lib
opencv_features2d231d.lib
opencv_calib3d231d.lib
opencv_objdetect231d.lib
opencv_contrib231d.lib
opencv_legacy231d.lib
opencv_flann231d.lib
I changed these from opencv_core{version}d.lib to opencv_core{version}.lib (not the debug library) and it seems to work okay now. I can step through the code in Visual Studio and the code appears to work.
The following code fails to open a VideoWriter object:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
VideoWriter oVideo;
oVideo.open ("381.avi", -1, 30, Size(640,480), true);
if (!oVideo.isOpened()) {
cout << "Could not open the output video for write" << endl;
return -1;
}
return 0;
}
I'm running OpenCV 2.4.9 pre-built with Code::Blocks on Ubuntu 12.04. I've written a number of images using imwrite() on the same location without issue, so I doubt it has to do with permissions. Also I tried CV_FOURCC('X','V','I','D') which did not work.
What am I missing here?
Any help is greatly appreciated.
I reinstalled OpenCV using this amazing script: https://help.ubuntu.com/community/OpenCV
Solved.
Make sure specific codec is installed in your machine.