OpenCV imread could not open or find image - c++

I'm doing a project with OpenCV. I am trying to run a simple code:
#include<opencv2/core.hpp>
#include<opencv2/videoio.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
cv::Mat img2 = cv::imread("test.jpg", 1);
if (!img2.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
cv::waitKey(5000);
return -1;
}
else {
cout << "Working" << endl;
cv::waitKey(5000);
}
return 0;
}
My project is a lot more bigger then this, however something simple like this isn't working for me. I have tried many things such as full path names // and \\, even tried an IplImage and convert it into Mat still no luck.
I have tried many different file types as well. If it helps I have coded it to stop the application after hitting any button, that does not work as it isn't taking any input from my keyboard.
How can I determine what is wrong?

I had this exact issue including the part about it only working in release mode. I found out I was using the release libraries for both release and debug. Because I had followed the tutorial on the OpenCV website for visual studio (incorrectly albeit), I changed the .lib folder to the correct d.lib and that fixed it.

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.

lodepng, stb_image. Nothing works in my system for image loading in C++

I'm trying to load pngs or bmps into my programs. But none of the libraries I found around the web works. I have no idea why but I always get "incorrect PNG signature, it's no PNG or corrupted" for EVERY png when using lodepng. "unknown pixel format" for EVERY bmp when using SDL_loadBMP. "unknown image type" for every png when using stb_image.
I can't load anything. Maybe there is something wrong with my system ? I'm using OSX Yosemite. Here is the code.
#include <iostream>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include <lodepng.h>
#include <stb_image.h>
using namespace std;
int main (){
string name = "res/img_test.png";
const char * cstr = name.c_str();
//lodepng
unsigned char *buffer;
unsigned int w,h;
int result = lodepng_decode32_file(&buffer, &w, &h, cstr);
cout << lodepng_error_text(result) << endl;
//stb_image
int x, y, comp;
FILE *f = fopen(cstr, "rb");
unsigned char *res;
res = stbi_load_from_file(f,&x,&y,&comp,0);
fclose(f);
cout << stbi_failure_reason() << endl;
return 0;
}
I'm using latest cmake to build this with gcc. Any recommendatation is appreciated but consider this. I've tried many files (generated by me or grabbed from internet). Tested same files with other users of the these libraries. Their code worked and mine didn't.
Edit:
Here's the source with complete cmake project >> github.com/onatbas/png_load_test.git
I solved it! Thank you everyone for trying to help.
It wasn't a code issue, afterall. It is a configuration issue. My cmake script damages the pngs and bmps when trying to copy them into destination folder. The code is fine.

Opencv << operator error for cout and writing to FileStorage

I am having problems getting the << operator to work for cout and writing to a FileStorage object in Visual studio c++ 2010 express. I am using the vs10 libraries, included the ***d.lib files and am running my project in debug mode. Other opencv functions (ORB, imshow) seem to work
When I use the << operator for printing a matrix with cout I get an access violation error.
The second issue I am having is when I try to write a matrix to a FileStorage object. I can write 1 matrix and read it from the file without it crashing. When I however try to write 2 matrices I get an "Exception at memory location" error on the second write. The console error says that No element name has been given.
The code I used for testing:
cv::Mat test(3,3,CV_8UC1);
cv::FileStorage file("fudge.xml", cv::FileStorage::WRITE);
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
test.at<unsigned char>(x,y) = x+y;
}
}
std::cout<<test<<std::endl;
file << "rotMat1" << test;
file << "rotMat2" << test;
Code Update
this is my entire code
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat test(3,3,CV_8UC1);
cv::Mat test2(3,3,CV_8UC1);
cv::FileStorage file("fudge.xml", cv::FileStorage::WRITE);
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
test.at<unsigned char>(x,y) = x+y;
test2.at<unsigned char>(2-x,2-y) = x+y;
}
}
std::cout<<test<<std::endl; //error 1
file << "AAAAA" << test;
file << "BBBBB" << test2; //error 2
return 0;
}
both errors still occur
UPDATE, I fixed it (kind of)
I found the problem but now there is a new one. So the problem was that because I have projects in both VS2010 and VS2012. Because of this I added both vs10/bin and vs11/bin to the path variables. It turns out that this results in visual studio using vs11/bin (maybe because that was declared first). After removing the vs11/bin decleration it worked in vs2010 but now vs2012 is broken. Because this isn't part of the original problem I will set it as answered.
The problem was that because I have projects using opencv in both visual studio 2010 and 2012 I added both /dir paths to the environment path variable. This resulted in visual studio always using the 2012/dir (probably because this was declared first). After removing the 2012/dir it worked fine.
you need to add using namespace cv;
follow this exemple : http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#xml-yaml-file-storages-writing-to-a-file-storage
I don't think the namespace is the problem.
Two things that I could think off that cause the problem:-
did you release your file object?
In the XML file, both your entries would have the same field. I'm not sure if this is supported by XML filestorage in Opencv. Did you try writing twice with different Mat objects?

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 ?