C++ Debug Assertion Failed While Using Opencv - c++

I am trying to compare two image and check whether they belongs to same person. This code giving Debug assertion failed Like here. I have checked, it can access to photos. Then what is the problem. People say that you are trying to access the something which is not actually exist. However, I could not find anything about it in my code.
Regards;
#include <iostream>
#include <string>
#include "opencv2\core\core.hpp"
#include "opencv2\core.hpp"
#include "opencv2\face.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\opencv.hpp"
#include<direct.h>
#include "vector"
using namespace std;
using namespace cv;
using namespace cv::face;
int main() {
vector<Mat> img;
img[0]= imread("C://Users//Gökhan//Desktop//faces//faces442.jpg");
Mat img2 = imread("C://Users//Gökhan//Desktop//faces//faces448.jpg");
vector<int> label;
label[0] = 1;
Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();
model->train(img[0], label[0]);
int predictedLabel = model->predict(img2);
if (predictedLabel == 1) {
cout << "found";
}
}

You are trying to access elements of vectors that have no elements.
You have to allocate elements before accessing.
You can use the constructor with specifying the number of elements to allocate to allocate elements, for example. To do this, change the lines
vector<Mat> img;
vector<int> label;
to
vector<Mat> img(1);
vector<int> label(1);

Related

Why Scalar does not work?

Following code results in build success, but no window. Without "m = Scalar(255,0,0);", it creates black window. Why including scalar does not work?
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat m = Mat::zeros(200,200,CV_8UC3);
m = Scalar(255,0,0); //without this, it creates window.
imshow("m", m);
waitKey();
}
It will not give you any compilation error as Mat and Scalar are basically array types of OpenCV.
This is a possible duplicate of How to set all pixels of an OpenCV Mat to a specific value?
Your code have to work!
take a look at the doc:
C++: void imshow(const string& winname, InputArray mat)
and I can confirm you is working fine on my environment(opencv320, VisualStudio2017):
you need to clean the project and build again.

"Process terminated with status -1073741819" simple program with vector

for some reason I get the "Process terminated with status -1073741819" error whenever I run my program, I've read that some people get this error because of something wrong with code-blocks/the compiler, i just wanted to know if there is anything wrong with my code before i go reinstalling compilers and such. I'm using code::blocks and the GNU GCC compiler.
my code creates a vector which stores 40 working hours in a week, and a vector inside that vector which stores letters representing the 5 people available in those hours.
Schedule.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
/// Creates a Vector which holds 40 items (each hour in the week)
/// each item has 5 values ( J A P M K or X, will intialize as J A P M K)
vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule(){
for (int i = 0; i<40; i++){
week[i][0] = 'J';
week[i][1] = 'A';
week[i][2] = 'P';
week[i][3] = 'M';
week[i][4] = 'K';
}
// test
cout << week[1][3] << endl;
}
header file:
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include <vector>
#include <string>
using namespace std;
class Schedule
{
public:
Schedule();
protected:
private:
vector< vector<string> > week;
};
#endif // SCHEDULE_H
main.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
int main()
{
Schedule theWeek;
}
This is not a copiler bug.
You are getting a memory fault in your constructor.
There are several things wrong with your code, for example in your cpp you declare a global vector week which then is hiden in the constructor since the constructor will access Schedule::week .
Your cpp should be something like :
// comment out the global declaration of a vector week ...
// you want a vector for each object instantiation, not a shared vector between all Schedule objects
// vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule()
{
for (int i=0;i<40;i++)
{
vector<string> stringValues;
stringValues.push_back("J");
stringValues.push_back("A");
stringValues.push_back("P");
stringValues.push_back("M");
stringValues.push_back("K");
week.push_back(stringValues);
}
}
You get the memory fault in your code when you try to access your week vector for the first time :
week[i][0] = 'J' ;
At the moment you call that line of code, your Schedule::week vector has 0 elements inside it (so week[i] is already a fault).

Instance of overloaded function in OpenCV

Hello I am trying to implement a Fast Feature Detector code,in the initial phase of it i get the following errors
(1)no instance of overloaded function "cv::FastFeatureDetector::detect" matches the argument list
(2)"KeyPointsToPoints" is undefined
Please help me.
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
int main()
{
Mat img1 = imread("0000.jpg", 1);
Mat img2 = imread("0001.jpg", 1);
// Detect keypoints in the left and right images
FastFeatureDetector detector(50);
Vector<KeyPoint> left_keypoints,right_keypoints;
detector.detect(img1, left_keypoints);
detector.detect(img2, right_keypoints);
vector<Point2f>left_points;
KeyPointsToPoints(left_keypoints,left_points);
vector<Point2f>right_points(left_points.size());
return 0;
}
The problem is in this line:
Vector<KeyPoint> left_keypoints,right_keypoints;
C++ is case-sensitive, it sees that Vector is something different than vector (what it should really be). Why would Vector work is beyond me, I would have expected an error earlier.
cv::FastFeatureDetector::detect only knows how to work with vector, not a Vector, so try to fix this bug and try again.
Also, KeyPointsToPoints does not exist in the OpenCV library (unless you program it yourself), make sure you use KeyPoint::convert(const vector<KeyPoint>&, vector<Point2f>&) to do the conversion.
There are a few small problems with the code as given. First you are missing using namespace std which is needed to use the vectors the way you are using them. You are also missing the include for vectors #include <vector>. vector<KeyPoints> should also have a lower case v. I'm also not sure if KeyPointsToPoints is part of OpenCV. You may have to implement this function on your own. I'm not sure, I just haven't seen it before.
#include <stdio.h>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("0000.jpg", 1);
Mat img2 = imread("0001.jpg", 1);
// Detect keypoints in the left and right images
FastFeatureDetector detector(50);
vector<KeyPoint> left_keypoints,right_keypoints;
detector.detect(img1, left_keypoints);
detector.detect(img2, right_keypoints);
vector<Point2f>left_points;
for (int i = 0; i < left_keypoints.size(); ++i)
{
left_points.push_back(left_keypoints.pt);
}
vector<Point2f>right_points(left_points.size());
return 0;
}
I didn't test the code above. Check out the OpenCV documentation here

How to load Multidimensional array values into vector?

This is part of the code (header and the main part):
#include <iostream>
#include <sstream>
#include <string>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glut.h>
#include <RassHost.h>
#include <api\iomap.h>
#include <api\iotrans.h>
#include <api\cgeometry.h>
#include <vector>
using namespace std;
int main()
{
cout << "Enter IP: " << endl;
getline(cin, server_ip);
enum(KEY_L = 'A', KEY_R = 'D', KEY_RUN = 'WW', KEY_JUMP='SPACE');
typedef OBJECT_3D_SYS_TYPES_NUM OBJECT3D_RCN_TYPE;
OBJECT3D_RCN_TYPE _psyObjects[][] = getPsyhicsPartObjects();
vector<OBJECT3D_RCN_TYPE> _objects;
//I would like to load _psyObjects[][] into vector<OBJECT3D_RCN_TYPE> _objects;
Server::StartGame(Server::getIP(), 8888, "-r run", false);
system("pause");
return 0;
}
Is it possible to copy _psyObjects values into vector<OBJECT3D_RCN_TYPE>?
I want to control the multidimensional array with vector api, if it is possible.
Thanks!
You'll need to create a vector of vectors:
vector< vector<OBJECT3D_RCN_TYPE> > _objects;
Then just fill it like a normal vector.
I'd post more code, but you need to know the dimensions of the array, and I can't see those from the code.
You could also use a Boost::multi_array. It's api is like std::vector's, but possibly similar enough to meet your needs.

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>