Reputation: 95
i am reading the Learning CV book, i came across the first example and encounter this problem
Using OPENCV 3.0.0 and VS 2013, all libraries added and checked.
the code is as follows
#include "opencv2/highgui/highgui.hpp"
int main( int argc, char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
So after compiling or build, I got a window named Example1, and it is grey, no image in the window.
Is this correct? Or what should I expect to get?
Upvotes: 0
Views: 151
Reputation: 41775
You are not loading the image correctly, i.e. argv[1]
has an invalid path. You can check this like:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
//IplImage* img = cvLoadImage("path_to_image");
if (!img)
{
std::cout << "Image not loaded";
return -1;
}
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
You can supply the path also directly in the code like:
IplImage* img = cvLoadImage("path_to_image");
You can refer here to know why your path may be wrong.
You also shouldn't use old C syntax, but use the C++ syntax. Your example will be like:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
int main()
{
Mat3b img = imread("path_to_image");
if (!img.data)
{
std::cout << "Image not loaded";
return -1;
}
imshow("img", img);
waitKey();
return 0;
}
You can refer to this answer to know how to setup Visual Studio correctly.
Upvotes: 1
Reputation: 8038
Its not clear if the image is being loaded. OpenCV will silently fail if it can't find the image.
Try
auto img= cv::imread(name, CV_LOAD_IMAGE_ANYDEPTH);
if (img.data() == nullptr)
{
std::cout << "Failed to load image" << std::endl;
}
Upvotes: 0