I am new to OpenCV. I appreciate if somebody answers this question. I try to read an image and display it. Below is a copy of the code I copied from documentation. However, a window just pops up without the actual image:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("myimage.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (img.empty())
{
cout << "Error : Image cannot be loaded..!!" << endl;
return -1;
}
else
{
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
waitKey(5000);
}
return 0;
}
I have copied over your code, and changed the image to my local one, and it displays correctly.
Looks like the program cannot read the image for some reason.
Why don't you try with the full path to the image?
The code is pretty correct, make sure you got myimage.jpg in the same folder with your binary.
Try with full path to an image or provide a path to your image as argv[1].
Related
i have tried out almost every format of writing path of that specific image in my code(/,//), even tried out by putting the image into project folder with some respective changes. but still there is an assertion error(without if condition).I tried my best to solve this but still the image not opening. I am a newbie using OpenCV c++ . Please help me I am tired with this error.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("test.png");
if (!img.data)
{
std::cout << "Image not loaded";
return -1;
}
imshow("Display Image", img);
waitKey(5000);
return 0;
}
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int isSquare(String fileName);
int main() {
String imgName = "C:/A.jpg";
isSquare(imgName);
}
int isSquare(String fileName) {
Mat img;
img = cv::imread(fileName, IMREAD_COLOR);
if (img.empty()) {
cout << "Could not open or find the image" << endl;
return -1;
}
//namedWindow("display", WINDOW_AUTOSIZE);
imshow("display", img);
waitKey(0);
cout << "hi";
destroyWindow("display");
return 0;
}
Hi, I'm currently messing around with openCV 3.30, C++. Now I'm trying to open an image, but the display window just kept disappear when I execute above code. I commented namedWindow("display", WINDOW_AUTOSIZE); because openCV document says cv:imshow() will automatically create one, and if I un-comment that line I got one gray window, one image window like this.
I don't want to got that gray window, and key input for waitKey(0) works only when I focus on gray window, not on image window.
So I made that line as a comment. but if I do that, when I execute that code the image window disappears instantly as if I don't have waitKey(0) code. Clearly waitKey(0) is not working because cout<<"hi"; after waitKey(0) was executed.
Am I missing something? Is the document wrong and using namedWindow necessary? All I wanted was to get rid of that gray window... any words of wisdom is appreciated, thanks.
I am using CMake and OpenCV with C++ and am just trying to run a simple program:
#include "opencv/highgui.h"
#include "opencv/highgui.hpp"
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/cxcore.hpp"
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("test.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (!image.data) //check whether the image is loaded or not
{
cout << "Image cannot be loaded." << endl;
}
else
{
cout<<"Image can be loaded."<<endl;
cout<<"Size is "<<image.size().height<<","<<image.size().width<<endl;
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
}
}
When I cmake, I get no errors, and when I run the program by doing ./test, I get "Image can be loaded" along with the correct size of the image.
Why won't my program create a new window which displays the image?
Thank you!
use cv::waitKey() after imshow. This is needed to proceed openCV rendering.
use waitKey(0) to pause until a key is pressed or waitKey(1) to pause as short as possible.
For further reference.
I'm new to OpenCV.
I've given a link to the function imread as follows:
Mat logo = imread("http://files.kurento.org/img/mario-wings.png");
I've checked and the image exists on the given path. imread() still fails to read it.
Any mistake that I've made?
-Thanks
In fact imread is not able to read image data via http.
But it's possible using VideoCapture.
See this little snippet:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
cv::VideoCapture vc;
vc.open("http://files.kurento.org/img/mario-wings.png");
if(vc.isOpened() && vc.grab()) {
cv::Mat logo;
vc.retrieve(logo);
cv::namedWindow("t");
cv::imshow("t", logo);
cv::waitKey(0);
vc.release();
}
return 0;
}
I am trying to convert color images to gray-scale using OpenCV 2.4.11 C++ for Visual Studio 2012. I have used the following code to convert the image to grayscale. However, I am unable to do so because I am not able to read the image.
The message I get is "Error reading image" because img is empty. I have stored the required image in the Debug folder beside the exe file. I have also mentioned the image name as a command argument in the Debug section of the property pages. I am also trying to store the grayscale image in the disk. Thanks in advance. The code is as follows:
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
using namespace cv;
int main(int argc, const char**argv)
{
Mat img=imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(img.empty())
{
std::cout<< " --(!) Error reading image " << std::endl;
system("pause");
return -2;
}
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
imwrite("image_bw.jpg", img);
waitKey(0);
destroyWindow("MyWindow");
return 0;
}
You should try using the absolute path to the image not the relative path. The other steps are fine, the image is read properly and the image displaying and saving commands are given properly there is a path problem.