Imread does not read the file [closed]
OpenCV version: 4.0.1
IDE: Visual Studio 2017
I took code from https://docs.opencv.org/4.0.1/db/deb/...
Here is the project Debug folder, it has the logo file.png and test.jpg
The application is compiled in debug folder.
But when I try to run the application and display one of the images in the application folder, the application writes that imread cannot open the image.
EDIT:
This code shows whether the file is available before imread starts opening it.
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
String imageName("C:\\Users\\Emilia\\Desktop\\VS\\imageFolder\\image.jpg"); // by default
ifstream input(imageName);
if (input)
{
if (input)
{
cout << "Ok! file exists...\n";
}
else
{
cout << "Oops! file not exists...\n";
return -1;
}
}
input.close(); //close file
Mat image;
image = imread(imageName, 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;
}
Here's what the program shows
Please help.
Hi, There is no HappyFish.jpg image inside your folder. Can you verify it?
Why should I HappyFish.jpg? I pass the path with an argument. I tried changing ../data / HappyFish.jpg to test.jpg, it does not help, the library works badly in all aspects.
Can you add your code in question? I have tried the same code with the same image. It is working for me.
and please, no screenshots of code or error msgs here... ;(
@mrjbom, common fact -- noobs NEVER find their images with a 1st attempt. ;)
( they also NEVER expect this to fail ;( )
verify, where your image REALLY is, and try with an absolute path, like
c:/some/folder/my.png
@berak, lol, here is a screenshot of the path to the photo.
screenshot
My code
@mrjbom, again, while you got all my sympathy, -- please do us all a favour, and replace ALL of those with a TEXT version, so it can be properly indexed for search ( to help other noobs ! ), we can simply quote you correctly, etc.
screenshots are for morons, ONLY, try NOT to be one, please.
@berak Okay, how can I bypass the comment length limit? I can't give you everything in TEXT format, if you review a LIMITED number of characters. How?
no, please edit your question and put it all there (we'll help you with the formatting, promised ;)
@berak, i changed the question, look please.
yea it's getting better ;)
@mjrbom -- actually checking possible failure reasons puts you way above the noob majority ;)
we'll have to find out: why did it still fail `?
possible reasons:
@berak, suppose the file path is incorrect, then why if (input) shows that it is correct and the file is there.
With. gif file is the same problem.
imread() cannot handle .gif files (still a patent problem), and it won't look at the file extension, but the first "magic bytes"
again, if your
ifstream
effort proves, the sillly thing does exist, -- imho, you got something, which is "not a valid image file" here.@berak, I moved the image image.jpg in the root of disk D, having the path "D:\image.jpg", in the program I used "D:\image.jpg". if (image) says that the file is in place, however imread is not able to open it.
NEVER use single backslashes, even if it's windows
I use this code: String fileName = "D:\\balik.jpg"
can you upload your image ? (edit again, and use the "upoad image" buttton from the menu)
@berek, I installed version 3.4.5 and now it works, thanks.
I have a small question, I would be glad if you could help me. When I create a window so I get 2 Windows, one gray, and the second with a picture, how to remove this gray window?
@mrjbom: most probably, the names you gave it, did not match (so it opened a NEW windw, for the one it didn't know, with an entirel empty image)
Please note that
if(input)
won't check if the file exists (only that theinput
variable was created)!Replace it with
if(input.is_open())
!!!!