I am trying to read an image in mat form in opencv and display it.
the code compiles fine but when run gives runtime error and the image is not displayed.
here is my code:
#include "stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<iostream>
using namespace cv;
using namespace std;
void main()
{
Mat Img;
Img=imread("C:/Documents and Settings/image1.jpg");
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
imshow( "Image", Img );
cvWaitKey(0);
}
Could anyone please tell me where am I going wrong?
Most probably imread() is failing. You will know for certain unless you start checking the success of the functions, such an important thing to do when you code.
There's a couple of reasons why it might fail:
You don't have permission to access the file.
The path is wrong.
In your case, it's probably the first. So change your code to:
Mat Img;
Img = imread("C:\\Documents and Settings\\image1.jpg");
if (Img.empty()) {
std::cout << "!!! Failed to open the file\n";
return;
}
and notice how the path to file is different.
Related
I have this simple code to open an image but it shows blank image in the "Video" window. I have checked release and debug lib's to make sure. It is in release mode and the lib is release lib. The picture is there, and I also tried full path for the image but still doesn't work. Also tried different image formats: jpg, png, bmp. It is OpenCV 3.4.1 release, and VS 2017 (also tried 2015).
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
namedWindow("Video");
Mat frame1 = imread("Penguins.bmp", CV_LOAD_IMAGE_COLOR );
imshow("Video", frame1);
system("Pause");
return 0;
}
Thanks in advance
You need to use the waitKey() function after imshow(). You can pass in a delay parameter to show the image for a certain time (in milliseconds).
Example:
#--- It will display the image for 30 ms
waitKey(30);
#--- It will display the image until a key is pressed
waitKey();
Check THIS PAGE for more.
There is also a demo example shown HERE
I am a beginner about OpenCV. I searched and tried to execute some basic OpenCV codes. When I tried running one of them, I got "R6010 -abort() has been called" error. I also tried to debug codes but this time on line 28 it gave me "Microsoft C++ exception: cv::Exception at memory location 0x000000A7F732F350." error.
// work5animage.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "opencv2\core.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\highgui.hpp"
using namespace cv;
int main( int argc, const char** argv )
{
// Read images
Mat color= imread("resized_love.jpg");
Mat gray= imread("resized_love.jpg", 0);
// Write images
imwrite("lenaGray.jpg", gray);
// Get same pixel with opencv function
int myRow=color.cols-1;
int myCol=color.rows-1;
Vec3b pixel= color.at<Vec3b>(myRow, myCol); //This is where I get the err.
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," <<
(int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
// show images
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
// wait for any key press
waitKey(1000);
return 0;
}
I saw some relavant problems but I couldn't find a convenient answer for me. Thank you for help.
I would suppose jpg doesn't store in the plain 2D format like you want to access it. Convert it to bmp, read that in and try again.
I have created a project once and it was working properly, after reinstalling operation system, I set up the enviroment and all the components and suddenly i am not getting any output at all. My program for testing is simple:
#include <cstdio>
#include "opencv2/opencv.hpp"
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap("videofile.avi");
if (!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Frame", 1);
for (;;)
{
Mat frame;
if (!cap.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
imshow("Frame", frame);
if (waitKey(30) >= 0) break;
}
return 0;
}
I have installed all necessary codecs and updated video card drivers. Still getting the same black screen.
The output looks like this:
EDIT:
The problem appears to be only when playing .avi videos with AVC codec
EDIT:
At the end the bin folder was missing in PATH variable, so after adding new entry - %OPENCV_DIR%\bin, everything is working propelly.
Clumsy solution:
get opencv_ffmpeg300.dll, opencv_ffmpeg.dll, those can be found right in opencv library
- get openh264-1.5.0-win64msvc.dll this one can be found at Openh264
- copy all those into the working directory of the project
In opencv 2.4.6. I am trying to load a mat image file with a simple code given below. But the image is not loaded as I print the image size, it is showing '0'. Can anybody please tell me , what is going wrong?
int main(int argc, char argv[])
{
Mat a=imread("C:/image3.jpg");
cv::Size frame11_size = a.size();
printf("%d",frame11_size.height);
return 0;
}
Update: I solved the problem. The problem was, I was only including all the library,include and additional dependencies in 'debug mode' only. I did not change anything in 'release mode'. When I change the properties in 'release mode' as-well, it worked. thanks all for your kind responses, I am giving '+1' for your answers.
I think there should be single slash on your image path, and always check whether image is successfully loaded.
Mat a=imread("C:/image3.jpg");
if(! a.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
OpenCV can't open jpg files by itself. It depends on third parties to do so. Maybe you are missing certain dlls, or maybe your OpenCV installation don't have the right path to them. To test this assumption store your image in other formats. For example pgm or ppm. Those formats does not perform any encoding and just store image buffer in file as is. As a result OpenCV will not need any external libraries to open image in ppm format.
I have the following code, which runs fine:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace std;
using namespace cv;
void main() {
bool running = true;
cv::OrbFeatureDetector OrbDetector;
while (running) {
IplImage *newFrame = cvLoadImage("x1.jpg");
IplImage *newFrameBW = cvCreateImage(cvGetSize(newFrame), newFrame->depth, 1);
cvConvertImage(newFrame, newFrameBW);
vector<KeyPoint> KeyPoints;
}
}
However, adding the line:
OrbDetector.detect(newFrameBW, KeyPoints);
to the while loop results in the following error:
HEAP[Example 4.exe]: Invalid address specified to RtlFreeHeap( 006B0000, 02474D20 )
Example 4.exe has triggered a breakpoint.
I am sure there is nothing wrong with the code, as I have just seen it run successfully on someone elses machine. Is there anything non code related that could be causing this?
The problem is that the OpenCV version you are using does not deal OK with MCVS 2012. Is not a problem of code, as I had a similar one that include vectors and didn't work.
Take a look to this tutorial that will show you how to rebuild the OpenCV library and your code will work pretty well ;)
Here is the link:
http://answers.opencv.org/question/6495/visual-studio-2012-and-rtlfreeheap-error/