Cannot run Opencv with C++ syntax - c++

Im using Opencv 2.3.1 on Visual studio 2010 (vc10)
I have configured opencv based on many tutorials and can compile & run C-syntax program like:
#include "StdAfx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main ()
{
IplImage* img = cvLoadImage("D:\cat_helmet.jpg", CV_LOAD_IMAGE_UNCHANGED);
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
However, I cannot run the C++ syntax program like
#include "StdAfx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
Mat image;
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
if(! image.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
imshow( "Display window", image );
waitKey(0);
return 0;
}
I got the error messages (in the function calls: namedWindow, imread, imshow)
First-chance exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
Unhandled exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
How can I fix this?

You say that you have followed a multitude of guides and tutorials. I've had great success with this one
http://www.anlak.com/using-opencv-2-3-1-with-visual-studio-2010-tutorial/
The thing is that this guy walks you through the 'park' and helps you unravel two major issues whilst setting up OpenCV 2.3.1; one of which is placement of .dll files in your project folder. The other is a missing .dll 'tbb_debug.dll' (the absense of this .dll is considered a bug in OpenCV 2.3.1).
He also provides some decent code-snippets for your to try out (in c++ syntax).
Good luck.

The above mentioned answers doesn't make sense. I am also facing the same problem. The main reason for this exception is that you are trying to display image (read by imread) which is empty. The main problem in the program is the line
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
I think imread function is not behaving the way it is expected. One more thing, while going through the references i came across a following link:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)
Here, imread is used via call by reference method. I am not a C++ expert, but i feel it could be the problem.
int main()
{
std::string imgPath("splash.bmp"); //Add your file name
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
This code worked for me. Also, I put the file next to executable to decrease complexity.

Related

My image is not opening in open cv, tried a lots of solution but still the same problem

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;
}

C++ openCV waitKey(0) not working?

#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.

OpenCV C++ imshow does not work

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].

Assertion failed with cv::merge

I am currently doing some C++ image processing with openCV. I developed the application on a Mac with Xcode 6.3.2 and it works perfectly in both debug and release. In order to have a Windows executable program, I am now working on Windows with Visual Studio Express 2013. The program is running well on debug mode but crashes in release mode on this part of the code :
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/photo/photo.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
vector<Mat> stacked_images;
Mat medianr_eq, mediang_eq, medianb_eq, objrgb;
medianr_eq = imread("C:\\Path\\medianr_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
mediang_eq = imread("C:\\Path\\mediang_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
medianb_eq = imread("C:\\Path\\medianb_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
objrgb = Mat(medianr_eq.size(), CV_16UC3);
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(stacked_images, objrgb);
}
The error I get is :
OpenCV Error : Assertion failed <mv && n > 0> in cv::merge, file C:\builds\master_PackSlave_Win64-vc12-shared\opencv\modules\core\src\convert.cpp, line 941
I can't see where I could have done something wrong... Indeed, it is pretty basic OpenCV !
The images I used are downloadable with this link : https://transfert.u-psud.fr/gs67
For astronomy lovers it is the Stephan's Quintet, taken with Calar Alto Observatory's 1.23m telescope where I currently am an intern.
Thank you in advance for your help,
Arnaud.
I recently had the exact same error, appearing with a valid openCV code running perfectly fine in debug mode but not in release mode.
Looking into the openCV source code, one can find that the function called has this code (modules/core/src/convert.cpp, line 341):
void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
{
CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
ocl_merge(_mv, _dst))
std::vector<Mat> mv;
_mv.getMatVector(mv);
merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
}
the last line here calls the function 'void cv::merge(const Mat mv, size_t n, OutputArray _dst)*', which has the infamous CV_Assert( mv && n > 0 ); instruction in its first line, causing the crash at runtime.
This error tells us that the vector of Mat is either a null pointer/reference or empty, which it clearly isn't in your code. I strongly suspect that the error is in the getMatVector function call, not copying the contents of _mv into mv. This leaves an empty array that is passed to the merge function, causing the error raised by CV_Assert.
In my case, the fix was to use directly the prototype of the function defined at line 200 in convert.cpp. For you, that would mean (copying only the last few lines of your code):
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(&stacked_images[0], stacked_images.size(), objrgb);
This solution worked in my case, and my code is now happily running in debug and release mode !
PS: I know it has been a while, but thought I would post the answer anyway in case somebody running into the same problem would arrive on this SO question.
PS2: Here is a full example code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
namedWindow( "Display window", WINDOW_AUTOSIZE );
Mat result;
Mat R = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat G = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat B = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
// Changing channel values for final result that should look largely blue
R = 0.1*R;
G = 0.1*G;
B = 1.5*B;
std::vector<cv::Mat> array_to_merge ;
array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);
// This line triggers a runtime error in Release mode
//cv::merge(array_to_merge,result);
// This line works both in Release and Debug mode
cv::merge(&array_to_merge[0], array_to_merge.size(), result);
cv::imshow( "Display window", result );
cv::waitKey(0);
}
Although you could write it in a lot fewer lines , this code is valid.

OpenCV crash after SURF Detection

I wrote a simple program in OpenCV that detects SURF feature in a given image and diplays the detected features in a namedWindow.
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
using namespace cv;
int main(int argc,char** argv)
{
if(argc!=3)//Check cmd number of argumets
{
std::cout<<"Usage: "<<argv[0]<<" <image-file> <method>"<<std::endl;
return -1;
}
//LOAD THE SOURCE IMAGE
Mat Img = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)//Check correct image load
{
std::cout<<"Cannot read image file. Check file path!"<<std::endl;
return -1;
}
//COMPUTE FEATURES
SurfFeatureDetector detector;
std::vector<KeyPoint> features;
detector.detect(Img,features);
//SHOW RESULT
Mat ImgF;
drawKeypoints(Img,features,ImgF);
namedWindow("Features", CV_GUI_NORMAL);
imshow("Features",ImgF);
waitKey();
return 0;
}
Everything is OK, the programs do what it have to do. The problem is when pressing a key to terminate the program a crash error occurs.
It doesn't crash for me... but in order for me to compile your code, I had to add
#include <opencv2/nonfree/features2d.hpp>
because SURF was moved to the nonfree module at some point.
So, I would have to recommend trying the newest version (2.4.6 as of today).