opencv sift - VS2015 debug assertion failed - c++

I am trying to run a small and simple program using opencv in VS2015. The code is the following:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/features2d.hpp>
#include<string>
using namespace cv;
using namespace std;
int main() {
Mat input1=imread("d_1.jpg", 0);
Mat input2 = imread("d_2.jpg", 0);
SiftFeatureDetector detector1,detector2;
vector<KeyPoint> keypoints1,keypoints2;
detector1.detect(input1, keypoints1);
/*detector2.detect(input2, keypoints2);
vector<DMatch> matches1to2;
Mat output1,output2,output;
drawKeypoints(input1, keypoints1, output1);
drawKeypoints(input2, keypoints2, output2);
drawMatches(input1,keypoints1,input2,keypoints2,matches1to2,output);
imwrite("first.jpg", output1);
imwrite("second.jpg", output2);
*/
return 0;
}
I get a Debug Assertion failed Error and the expression mentioned is "reinterpret_cast(_ptr_ptr)[-1]==_BIG_ALLOCATION_SENTINEL"&&0 and the file mentioned is Microsoft visual studio14\vc\include\xmemory0 Line:110.
I looked up other solutions here such as updating the platform to 2010 and checking the configuration to ensure all the included lib files belong to the correct VC version. That just ended up giving me more errors. So I reverted to the original config and ran this code line by line to see which line has the issue cos other wise, the program ran completely, gave output and the error was being reported after the return statement.
The error pops up after the detector.detect statement, I am not sure what the problem is. Any help is appreciated!

Related

FAST function OpenCV Debug Assertion Faild

when I try debug my project, I get Debug Assertion Failed, at the end of the execution up. You can see it below.
I think that the problem is related to FAST function from OpenCV, because when I comment out the line which contains this function, it works fine.
This is my code:
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
string table[2] = { "background_2.jpg","foreground_2.jpg" };
Mat firstImage = imread(table[0], IMREAD_COLOR);
Mat secondImage = imread(table[1], IMREAD_COLOR);
Mat result;
subtract(firstImage, secondImage, result);
Mat resultGray, firstImageGray, secondImageGray;
cvtColor(firstImage, firstImageGray, COLOR_BGR2GRAY);
cvtColor(secondImage, secondImageGray, COLOR_BGR2GRAY);
cvtColor(result, resultGray, COLOR_BGR2GRAY);
Canny(firstImageGray, firstImageGray, 33, 100, 3);
Canny(secondImageGray, secondImageGray, 33, 100, 3);
Canny(resultGray, resultGray, 33, 100, 3);
vector <KeyPoint> keyPoints;
FAST(resultGray, keyPoints, 9, true);//Probably here is the problem.
Mat resultKeyPoints;
drawKeypoints(resultGray, keyPoints, resultKeyPoints, 156);
return 0;
}
I build my project in Visual Studio 2015 Preview.
When I clicked "Retry" to break at that assertion, I got the following exception:
Unhandled exception at 0x00007FF851891B4B (ucrtbased.dll) in
OpenCVProject.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
This is in xmemory0 file from VS in 120 line.
I found the answer. I changed in Property Pages in my project Platform Toolset from Visual Studio 2015 (v140) to Visual Studio 2013 (v120). Now it works fine. It seems that the problem wasn't in FAST function, but in this case, that I use VS 2015 with OpenCV 3.0. #drescherjm thank you for help me do this.

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 assertion failed <size.width>0 && <size.height>0 in cv::imshow

#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include<opencv\cvaux.h>
#include<opencv\cxcore.h>
#include <opencv2\imgproc.hpp>
#include <iostream>
#include<conio.h>
using namespace cv;
using namespace std;
void main()
{
Mat src = imread("greyscale.jpg"); \\image to be edited
imshow("input", src);
Mat dest; \\processed image
threshold(src, dest, 100, 255, THRESH_BINARY);
imshow("result", dest);
waitKey();
}
The above code when executed on Visual Studio 2013 express (configured for opencv 3.0)
does not show any compiling errors.
However the output window displays error
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, fi
le C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\highgui\src\windo
w.cpp, line 271
Press any key to continue . . .
And window pops up saying open cv has crashed.
I'm new to opencv and any help would be much appreciated.Thanks in advance!
I have gone through similar problems but theirs were dude to the webcam feed being null initially..all im doing here is scanning an image

Face Detection Using OCL (OPENCV)

I am trying to detect faces using the below code making use of GPU
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <iostream>
#include <stdio.h>
#include <opencv2\ocl\ocl.hpp>
std::string face_cascade = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
std::vector<cv::Rect> detectFaces(cv::Mat gray){
cv::ocl::oclMat oclGray;
std::vector<cv::Rect> faces;
cv::ocl::OclCascadeClassifier face_detector;
oclGray.upload(gray);
face_detector.load(face_cascade);
face_detector.detectMultiScale(oclGray, faces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30), cv::Size(0, 0));
return faces;
}
int main(){
cv::VideoCapture webcam;
cv::Mat mainImage;
std::vector<cv::Rect> faces;
webcam.open(0);
cv::namedWindow("face",CV_WINDOW_AUTOSIZE);
while(webcam.isOpened()){
webcam.read(mainImage);
if(!mainImage.empty()){
cv::resize(mainImage,mainImage,cv::Size(640,480),0,0,1);
cv::Mat gray(mainImage);
cv::cvtColor(gray,gray,CV_BGR2GRAY);
cv::equalizeHist(gray,gray);
faces = detectFaces(gray);
for(unsigned int i=0;i<faces.size();i++){
cv::Rect f_rect = faces[i];
cv::rectangle(mainImage,f_rect,CV_RGB(255,0,0),1,8,0);
}
cv::imshow("face",mainImage);
}
cv::waitKey(40);
}
return 0;
}
I wasnt satisfied with t speed of the normal cascade classifier and thus coded for Ocl based classifier. The program gets started but shows error message:
I have installed APP SDK v 2.9.1
I am using Visual Studio 2012 express edition, Opencv 2.4.10
Where did I go wrong??
Thanks
EDIT>>
cv::ocl::oclMat oclGray;
oclGray.upload(gray);
The above code is causing error..
It looks like it fails on this line face_detector.load(face_cascade); (use debugger to make sure that i'm right). Make sure that the path is correct and that format of cascade file is valid, you may try to use different cascade as well and of course make sure that OCL is installed and configured correctly.

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