Strange behavior with OpenCV - c++

When I compile my Windows application (called CrosslinesDetection) in Visual C++ 2005 including OpenCV 1.1, a computer vision library, I do not get any compile or link errors, but when I am running it, it gets to a point and freezes, and Windows says the following:
"Unhandled exception at 0x7c915223 in CrosslinesDetection.exe:
0xC0000005: Access violation writing location 0x00030ffc."
The program is a common C++ Windows Applikation with two lines of OpenCV code:
IplImage *img = cvCreateImage( cvSize( 1024, 768 ), IPL_DEPTH_8U, 1 );
cvReleaseImage( &img );
The strange behavior is now, if I
- include the OpenCV lines, the program throws the exception
- exclude the OpenCV lines, the program works properly.
I used the OpenCV libraries in another project without any problems. In particular, I made a C# project for the GUI and a C++ project compiled as DLL in the background. If I create such a solution for the above lines, I have not problems during execution.
Has anybody an idea, what might cause this error?
Thanks for any help, Stefan
Thanks for the comments.
Meanwhile, I use a minimal project for testing.
I still do not fully understand the problem, but meanwhile, I figured out, that the excpetion occurs, when I include a third party library (from uEye).
If I use a single function from OpenCV and from the uEye library in the project, then an exception occurs. If I use either a single function from OpenCV or from uEye, no exception is thrown. So, these to libraries seem to be somewhat incompatible, or yet there is another problem. However, I don't know how to detect it.
No not at this point. In the minimal example the functions are unrelated. One functions initializes the camera and the other function intializes an image structure.
But maybe the error is elsewhere...
I created in Visual Studio a C++ Windows Form Application. I added to the form a button as well as corresponding function in Form1.h file:
private: System::Void Form1_Click(System::Object^ sender, System::EventArgs^ e) {
OpenCamera();
IplImage * img = cvCreateImageHeader( cvSize( 1024, 768 ), IPL_DEPTH_8U, 1);
cvReleaseImage( &img );
CloseCamera();
}
And I added the headeras at the begin of Form1.h:
#include "CameraControl.h"
#include "cv.h"
Then, I had to switch off the precompiled header option and I had to change from /clr:pure to /clr option to successfully compile and link the project.
But, then I run the program the above mentioned exception is thrown...
I am wondering, if I misuse the C++ Windows Form Application and I should not inlcude my pure C++ code or if there is really a problem with OpenCV or the uEye library.

I would suggest to test this same code in a native project, without managed code. Either MFC or a Win32 console application.

Isn't it
cvReleaseImage( img );
? (ampersand removed)
Also you should check the proper calling convention.

I just ran and compiled those exact lines with OpenCV 1.0 and Visual Studio 2008, with no errors. Maybe try creating an empty project which does nothing else but includes the cxcore.h header and then runs those two lines.
Also just to the other poster: no the ampersand is part of the specification, he's correct there. Sorry I would post this as a comment but can't yet.

Related

C++: Why does libtiff break the console-output?

So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.

Debug Assertion Failed _pFirstBlock == pHead using Opencv

I am using following code for background subtraction. I am giving it path of video, video runs successfully but at the end it gives Debug Assertion Failed error.
I am using following code in Microsoft Visual Studio to solve a problem of Computer Vision with opencv.
#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
#include<vector>
#include "opencv2/video/background_segm.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat frame;
Mat back;
Mat fore;
VideoCapture cap;
cap.open("H:/competition.avi");
BackgroundSubtractorMOG2 bg(100,16,true);
bg.set("nmixtures",3);
vector<vector<Point> > contours;
namedWindow("Frame");
namedWindow("Background");
for(;;)
{
cap >> frame;
if(!frame.empty())
{
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
erode(fore,fore,Mat());
dilate(fore,fore,Mat());
findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
drawContours(frame,contours,-1,Scalar(0,0,255),2);
imshow("Frame",frame);
imshow("Background",back);
if(waitKey(30) >= 0) break;
}
else
break;
}
return 0;
}
I just came across this issue and after serious web trawling, found the answer, at least it worked in my case...
you need to go to your visual studio project settings, to c/c++, to code generation and change the runtime library to Multi-threaded Debug DLL (/MDd).
It seems this error is from a single threaded DLL trying to run in a multi thread project, or something of that nature.
good luck!
For unknown reasons, some versions of opencv (2.x at least) have a CMake variable "BUILD_WITH_STATIC_CRT" that by default gets set to on, thus causing issues like that. Disable that flag, then the solution should get generated with /MDd defined.
Secondarily, open your exe file in dependency walker. Look for multiple versions of MS C++ runtime libraries. For example, you may have a version of QT built against msvcp110.dll (visual studio 2012) but your current project uses msvcp120.dll (visual studio 2013).
Alright. First thing first: Hit Retry, assuming that you are debugging (F5), and have not launched (Run) the program by hitting (Ctrl+F5). As soon as you'd hit Retry, you will see call stack in debugger.
The call stack will give you possible hint where that invalid/double free/delete is happening. That would be your starting point to analyse the issue. See if some memory is double freed, allocated using different heap (for example, with malloc, and being deleted). Or, if memory allocated by VC9 (for example), is being freed by a DLL written in VC8.
I had the same error,
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c Line 1424
Expression:_pFirstBlock == pHead
when using debug mode on vs12 when testing opencv code for augmented reality, for reference the code I used was from here.
The Solution that worked for me: The problem went away for me after I updated the visual studio settings for release mode, even though I was only using debug. Other opencv code runs no problem in debug mode, so I had not bothered to fully configured the release settings.
Anyway specifically in release the parts I had to update were in Properties -> C++ -> Additional Include Directories; and Properties -> Linker -> Input -> Additional Dependencies. After that the code ran error free in debug mode and release mode. If you don't know what settings to use, they are listed in the setup instruction pages on the opencv website, vs12 instructions are here
I meet the same problem.
I find the resolution through this URL.
Debug Assertion Failed Expression: _pFirstBlock == pHead using OpenCV and C++ trying to call SurfFeatureDetector
The reason for this error is the configuration problem,vs2012 is matched with vc11 folder.
This may help you.

Loading texture SFML causes program to crash

I'm trying to load an sf::Texture, but every time I run the program, my computer starts beeping (no joke), and the command prompt outputs a bunch of characters that look like ancient Egyptian hieroglyphics.
Relevant code:
#pragma once
#include "SFML/Graphics.hpp"
class TextureLoader {
public:
sf::Texture runway;
~TextureLoader(){}
TextureLoader() {
runway.loadFromFile("Runway.png");
}
};
Runway.png does exist, because I've tested creating an ifstream and it loads the image fine.
The debugger complains of an "Access violation reading location", but the location changes each time and I've never been able to find the memory space it points to in the debugger. Every object appears to be fine except for the texture.
You are mixing debug/release version of SFML libraries with your program's debug/release version. If your program is debug, use debug libraries of SFML, if your program is in release version, use release version of SFML.
People who are still figuring out how to set the project settings, please do as follows (note the "-d" for debug):

Can not read image with opencv2.3 imread

Hello I am trying to read an image by using imread function of opencv as in the link (http://opencv.itseez.com/doc/tutorials/introduction/display_image/display_image.html#display-image). I have VS2010 with 64 bit windows 7. Each time I try I get error message "no image data", however the image I want to read is in the same folder with codes. Can someone please help me how to read an image with imread function? My code is as below:
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat image;
image = imread("al.jpg");
if(argc != 2 || !image.data )
{
printf("no image data \n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
It you can load image using
IplImage *img=cvLoadImage("Image_Name);
Then you can convert this into cv::Mat using,
Mat mat(img);
I've had the same problem on WinXP with VS2005 and OpenCV 2.3. It seems that the C++ interface of OpenCV is not working well for Windows. I also had problems with imread(), which returned NULL data.
I solved the problem using the C interface of OpenCV instead. For more info, check Reading and Writing Images and Video.
I have met the same problems as yours. The C++ interface for OpenCV2.3 didn't work and some functions have to convert to C version.
But at last, I found what the ** problem is.
That is because the project properties when you compiled your OpenCV source code with CMake, is not the same as your current project properties.(May be your OpenCV's dll and libs are download from the internet, and these may be compiled with other wierd properties.)
So I check my CMake generated OpenCV source code project, I found the project property , C/C++-->Code Generation, the Runtime Library is Multi-threaded Debug DLL(/MDd).
Then, I change the corresponding property in my current project, and my project work well. At the same time, the other C++ interface for OpenCV bugs is solved well.
Take out the argc != 2 test. You aren't using the arguments passed to your program so it's superfluous. And if argc is, in fact, not eaual to 2, your program is terminating before even getting to the !image.data test.
Edit: I just looked at the code sample you linked to. It loads an image whose name is passed to the program on the command line. That's why the argc != 2 test is there. You definitely want to take that out because you are most likely not passing a file name on the command line so your argc is 1, thus the test will always fail.
OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras).
I experience that same problem, This may come from VS project compilation in VS 10.
In visual studio 2010:
Go to project --> properties --> Character set --> Use multi byte instead of unicode.
Set the Common Language Runtime Support (/clr) in Visual C++.
It works.

OpenCV 2.3 and Visual Studio 2010

I am having so much trouble installing openCV 2.3 with visual studio 2010. Crash after crash, installation after installation and after several weeks I've had no luck.
Unfortunately there are no installation documents for openCV 2.3 and the directory structure and file locations are different from openCV 2.2 which makes the current tutorials almost useless. :(
Has anyone out there had any success with openCV 2.3? Can someone please try it and let me know if its an openCV build issue or my setup?
Or maybe someone can suggest an alternative to openCV. What my end goal is, is to get Pixel info, use inpaint functions, and basic image processing for After Effects and Maya.
EDIT: Sorry I thought I posed the error! This is what happens when I run the code:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat image;
image = imread( "c:/image1.png", 1 );
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
imshow( "Gray image", image );
waitKey(0);
return 0;
}
It runs until imshow. If I comment out imshow it runs fine with no errors.
Here is the errors when I add IMSHOW:
Unhandled exception at 0x76bfb727 in openCV_test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0015ec20. and it gives me the option to break or continue.
This is what the output window shows:
First-chance exception at 0x76bfb727 in openCV_test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0015ec20..
Unhandled exception at 0x76bfb727 in openCV_test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0015ec20..
It then goes on to open SYSTEM.CPP and give me this:
- exc {msg="......\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type
" code=-206 err="Unrecognized or unsupported array type" ...} const cv::Exception &
+ std::exception {_Mywhat=0x00000000 _Mydofree=false } std::exception
+ msg "......\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type
" std::basic_string,std::allocator >
code -206 int
+ err "Unrecognized or unsupported array type" std::basic_string,std::allocator >
+ func "" std::basic_string,std::allocator >
+ file "......\modules\core\src\array.cpp" std::basic_string,std::allocator >
line 2482 int
Thanks!
I have OpenCV2.3 and had no problem to install it with Visual Studio v9.0.
What you should do to compile and run correctly your project in all the Visual Studio versions :
Properties of your project (right click on it)
C/C++
General
Include directory add the < your directory >\OpenCV2.3\include\opencv2, < your directory >\OpenCV2.3\include\opencv and < your directory >\OpenCV2.3\include
Linker
General
Add lib directory < your directory >\OpenCV2.3\lib
Input
Add all the libs like opencv_core230d.lib opencv_highgui230d.lib and so on...
Then don't forget to add the dll to your system path.
Configuration panel > System > Advanced > Environment variables > Path
you can add a user environment path which will override the other one,
just click New (if you have never added directory to your system path before) and write the path eg : < your directory >\OpenCV2.3\bin
Without more information, I hope it could help...
Julien,
Sometimes, the compiler cannot find the dll even if you have the correct path settings. At that point try copying the dll files from the openCV folder to ur current project (not solution) folder.
Rather than copy pasting I'll just link: http://theroundedengineer.blogspot.com/2011/07/opencv-23-for-vs-2008-from-source.html
Hopefully that helps. Granted I'm far from an expert on the differences between VS 2008 and VS 2010.
I got exactly the same problem. I couldn't read any image. I checked the data flag and image size. The flag indicated error and the size was always (0, 0). Although I do not know the actual cause, I somehow could avoid the problem and run my program fine.
At first, I compiled OpenCV myself. Even though all the dlls seemed to be created correctly and there was no build error, I noticed that the header folders are not correctly organized. Therefore, I switched to the superpack binary and tried static linking. There were a lot of undefined symbols at the beginning, so I basically just put everything into VS link options. The number of libraries I had to input to VS was quite absurd, but it is a good way to test if the superpack works.
These are what I use.
opencv_core230d.lib;opencv_calib3d230d.lib;opencv_contrib230d.lib;opencv_features2d230d.lib;opencv_highgui230d.lib;opencv_legacy230d.lib;opencv_ml230d.lib;opencv_imgproc230d.lib;opencv_video230d.lib;libjasperd.lib;libjpegd.lib;libpngd.lib;libtiffd.lib;zlibd.lib;
I also need to input Comctl32.lib to resolve linking error.
I tested my program with static libraries of superpack. Things work fine now.
The headers I used are
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
I do not try using dlls yet, but I can confirm that VS 2010 + OpenCV 2.3 64 bit from the superpack work. I hope you will have some luck with the superpack too.
(Hmm, I can't remember if I put opencv.hpp in the opencv2 folder myself, or if it was there right from the beginning. I did a lot of things to make it work and was a bit confused. But, I believe you can figure this out yourself if there is anything wrong about the header.)
Hope this helps,
Pinyo
You should try making a cycle structure:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(){
Mat image;
namedWindow( "Gray image", 2 );
while(1){
image = imread( "d://Gaa6P.png", 1 );
imshow( "Gray image", image );
waitKey(0);
}
return 0;
}
Check your firewall. turn off firewall totally. Your antivirus may block the process too.
I had the same problem: cv::exception for memory and this was it's cause.