OpenCV assertion in facerec_eigenfaces from getMat - c++

When I run the facerec_eigenfaces demo program from OpenCV 3.1 I receive an unhandled exception:
OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::_InputArray::getMat_
This follows a call to _InputArray::getMat(int). I reduced the facerec_eigenfaces program to the following which gives the same error. (I also tried other binary pgm files with the same result.) Can anyone suggest what may be wrong?
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <vector>
int main(int argc, char *argv[])
{
auto m = cv::imread("../att_faces/s1/1.pgm", 0);
std::vector<cv::Mat> imgs;
imgs.push_back(m);
cv::InputArrayOfArrays ia(imgs);
ia.getMat(0); // assertion here
return 0;
}

Related

error: no matching function for call to 'FaceDetector::FaceDetector(std::__cxx11::string)'

I am new to C++ and i am getting error like
error: no matching function for call to 'FaceDetector::FaceDetector(std::__cxx11::string)'
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
and i am attaching my code and error log how to fix this please guide me
#define DEFAULT_CASCADE_PATH "cascades/haarcascade_frontalface_default.xml"
#define ORIGINALS_LIST "obama_raw/list"
#define OUTPUT_DIR "obama_faces"
#define OUTPUT_LIST "list"
#define FACE_SIZE Size(150,150)
#include <cstdlib>
#include <fstream>
#include "cv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "FaceDetector.h"
using namespace std;
using namespace cv;
void read_input_list(const string &list_path, vector<Mat> &images) {
ifstream file(list_path.c_str());
string path;
while (getline(file, path)) {
images.push_back(imread(path));
}
}
int main(int argc, char** argv) {
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
vector<Mat> raw_faces;
ofstream out_list(format("%s/%s", OUTPUT_DIR, OUTPUT_LIST).c_str());
read_input_list(string(ORIGINALS_LIST), raw_faces);
int img_c = 0; //images counter
//now detect the faces in each of the raw images:
for (vector<Mat>::const_iterator raw_img = raw_faces.begin() ; raw_img != raw_faces.end() ; raw_img++){
vector<Rect> faces;
//detect faces in the image (there should be only one):
fd.findFacesInImage(*raw_img, faces);
//cut each face and write to disk:
for (vector<Rect>::const_iterator face = faces.begin() ; face != faces.end() ; face++){
int edge_size = max(face->width, face->height);
Rect square(face->x, face->y, edge_size, edge_size);
Mat face_img = (*raw_img)(square);
//resize:
resize(face_img, face_img, FACE_SIZE);
//write to disk:
string face_path = format("%s/%d.jpg", OUTPUT_DIR, img_c++);
imwrite(face_path,face_img);
out_list << face_path << endl;
}
}
out_list.close();
return 0;
}
and i am attaching my error log.Please can any one help.
Thanks in advance
Error : https://i.stack.imgur.com/RZXXK.jpg
From GCC 5, A new ABI is enabled by default. In that new ABI, std::__cxx11 namesapce was introduced.
According to your error message, It seems that your program and OpenCV library you want to link with were build with different GCC version, which made incompatible binary.
For more information, you can read the following page:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

Error while trying to build stasm minimal

I do not have experience on C++. And I have to use stasm for face detection. I'm traying to build the minimal example. On page 4 of this tutorial is possible to know what is necessary to make it work. But I'm these two errors:
g++ -Wno-deprecated -o teste minimal.cpp `pkg-config opencv --cflags --libs` -I/home/caaarlos/workspace/StasmDesbravando/stasm
In file included from minimal.cpp:46:0:
/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/pinstart.cpp: In function ‘void stasm::CopyPoint(stasm::Shape&, const Shape&, int, int)’:
/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/pinstart.cpp:138:13: error: redefinition of ‘void stasm::CopyPoint(stasm::Shape&, const Shape&, int, int)’
static void CopyPoint( // copy a point from oldshape to shape
^
In file included from minimal.cpp:37:0:
/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/convshape.cpp:9:13: error: ‘void stasm::CopyPoint(stasm::Shape&, const Shape&, int, int)’ previously defined here
static void CopyPoint( // copy a point from oldshape to shape
This is my code:
// minimal.cpp: Display the landmarks of a face in an image.
// This demonstrates stasm_search_single.
#include <stdio.h>
#include <stdlib.h>
#include "opencv/highgui.h"
#include <opencv/cv.h>
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm_lib.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/asm.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/atface.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/basedesc.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/classicdesc.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/convshape.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/err.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/eyedet.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/eyedist.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/faceroi.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/hat.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/hatdesc.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/landmarks.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/landtab_muct77.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/misc.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/pinstart.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/print.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shape17.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shapehacks.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shapemod.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/startshape.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm_landmarks.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm_lib.h"
//#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm_lib_ext.h"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/asm.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/classicdesc.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/convshape.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/err.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/eyedet.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/eyedist.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/faceroi.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/hat.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/hatdesc.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/landmarks.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/misc.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/pinstart.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/print.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shape17.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shapehacks.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/shapemod.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/startshape.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/stasm_lib.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/MOD_1/facedet.h"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/MOD_1/initasm.h"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/MOD_1/initasm.cpp"
#include "/home/caaarlos/Documentos/TCC/stasm4.1.0/stasm/MOD_1/facedet.cpp"
using namespace cv;
using namespace std;
int main()
{
static const char* const path = "../data/testface.jpg";
cv::Mat_<unsigned char> img(cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE));
if (!img.data)
{
printf("Cannot load %s\n", path);
exit(1);
}
int foundface;
float landmarks[2 * stasm_NLANDMARKS]; // x,y coords (note the 2)
if (!stasm_search_single(&foundface, landmarks,
(const char*)img.data, img.cols, img.rows, path, "../data"))
{
printf("Error in stasm_search_single: %s\n", stasm_lasterr());
exit(1);
}
if (!foundface)
printf("No face found in %s\n", path);
else
{
// draw the landmarks on the image as white dots (image is monochrome)
stasm_force_points_into_image(landmarks, img.cols, img.rows);
for (int i = 0; i < stasm_NLANDMARKS; i++)
img(cvRound(landmarks[i*2+1]), cvRound(landmarks[i*2])) = 255;
}
cv::imwrite("minimal.bmp", img);
cv::imshow("stasm minimal", img);
cv::waitKey();
return 0;
}
I'm compiling my code in thi way:
g++ -Wno-deprecated -o teste minimal.cpp `pkg-config opencv --cflags --libs` -I/home/caaarlos/workspace/StasmDesbravando/stasm
What am I doing wrong? Can someone help me?
Thanks.
I've found a solution for this problem. If somebody has a problem like mine, you should enter on this site and use the CMakeList.txt and CMake to build Stasm. If you get any error you can try the intructions provides o the readme.

error C2664: 'cvSaveImage' : cannot convert parameter 2 from 'cv::Mat' to 'const CvArr *'

I am getting the the following error "cannot convert cv::Mat to constCvArr".Here's the code.
Could anyone help in this case
What is the reason for the error? how could I correct it.
#include "stdafx.h"
#include "opencv2\calib3d\calib3d.hpp"
#include <opencv2\core\core.hpp>
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <stdio.h>
using namespace cv;
Mat src_gray;
int _tmain(int argc, _TCHAR* argv[])
{
char *str1=(char *)malloc(500);
int nums=20;
for(int iter=0;iter<nums;iter++)
{
sprintf(str1,"training1//image%d.png",iter);
Mat img=imread(str1);
cvtColor(img,src_gray,CV_BGR2GRAY);
cv::Mat output = cv::Mat::zeros(img.rows,img.cols,img.type());
threshold(src_gray,output,128,255,THRESH_OTSU | THRESH_BINARY_INV);
char *out=(char *)malloc(500);
sprintf(out,"out%d.png",iter);
cvSaveImage(output,out);
namedWindow("threshold",1);
imshow("threshold",output);
waitKey(0);
return 0;
}
}
you're mixing old , deprecated c-api calls(bad) with the newer c++ api(good).
instead of:
cvSaveImage(output,out);
use:
imwrite(output,out);
also, whatever you malloc(), you'll have to free ;) (you got 2 memleaks there), again, avoid the whole issue:
Mat img=imread( cv::format("training1/image%d.png",iter) ); // note: *single* slash in path, or *double backslash
// ...
imwrite( cv::format("out%d.png",iter) );

OpenCV Error: Assertion failed in cvAdaptiveThreshold

I recently started some OpenCV programming on OSX (just using text editor and compiling in terminal). I found program on the internet that is very useful to me but can't seem to run it.
This is the code:
#include <stdio.h>
#include "cv.h"
#include <highgui.h>
#include <iostream>
#include <cstdio>
using namespace std;
int widthU;
int heightU;
int xU = 0;
int yU = 0;
int main(int argc, char *argv[])
{
IplImage *imgPicThres, *imgPicInput;
imgPicInput = cvLoadImage("bitmap.png", -1);
imgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);
cvNamedWindow("Input picture", 0);
cvNamedWindow("Thres picture", 0);
//Picture
//cvThreshold(imgPicInput,imgPicThres,100,255,CV_THRESH_BINARY);
cvAdaptiveThreshold(imgPicInput, imgPicThres,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
cvShowImage("Input picture", imgPicInput);
cvShowImage("Thres picture", imgPicThres);
while (true)
{
int c = cvWaitKey(10);
if(c==27)
break;
}
cvDestroyWindow("Input picture");
cvDestroyWindow("Thres picture");
return 0;
}
And this is the error that I get:
OpenCV Error: Assertion failed (src.size == dst.size && src.type() == dst.type()) in cvAdaptiveThreshold, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.5/modules/imgproc/src/thresh.cpp, line 873
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6
I tried to change this line
ImgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);
into
ImgPicThres = cvCreateImage(cvGetSize(imgPicInput), IPL_DEPTH_8U, 1);
with no luck.
OpenCV is installed via Macports and is running the latest version. Any help would be appreciated. Thanks!
imgPicInput = cvLoadImage("bitmap.png",CV_LOAD_IMAGE_GRAYSCALE);
to ensure that the image you read is actually grayscale.
Additionally to suggestion from perfanoff, I'd rather clone image rather then creating it.
imgPicThres = cvCloneImage(imgPicInput );
I have found the answer but forgot to mention it. As the error says imgPicInput and imgPicThres are not of the same size and type. Also I was supposed to watch after the image channels which I didn't.

Cant save image using OpenCV

I was trying to save images from a video file but It couldn't save any image in my hard drive. I compiled following program without errors.
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main(int argc, char* argv[]) {
int c=1;
IplImage* img=0;
IplImage* small;
IplImage* small=new IplImage(inputArray);
char buffer[1000];
CvCapture* cv_cap=cvCaptureFromFile("test.avi");
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
small = cvCreateImage(cvSize(img->width/2,img->height/2), 8, 3);
sprintf(buffer,"C:/image%u.jpg",c);
c++;
if (cvWaitKey(100)== 27) break;
}
cvDestroyWindow("Video");
return 0;
}
I am using a 2.3.1 openCV library and Visual Studio 2010.
How can I save the image from a video file?
I have also tried this code,above program code are something wrong.please see following program code.After i adding following code ,also there was a same problem.
cvSaveImage(buffer,img);
Corrected program is shown below.please see this and tell me what i did wrong.but there is not compile error.after i run the program it doesn't save any image.
#include"stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<cxcore.h>
int main(int argc, char* argv[]) {
int c=1;
IplImage* img=0;
char buffer[1000];
CvCapture* cv_cap=cvCaptureFromCAM(-1);
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
sprintf(buffer,"D:/image%u.jpg",c);
cvSaveImage(buffer,img);
c++;
if (cvWaitKey(100)== 27) break;
}
cvDestroyWindow("Video");
return 0;
}
can you tell me how to save a image .above program doesn't save any images.please give me your suggestions.thank you.
You forgot to actually save the image:
cvSaveImage(buffer ,img);
Also, the following is redundant:
IplImage* small; // <-- you don't need this
IplImage* small=new IplImage(inputArray);
Don't know if you solved the problem or not. if not you can use imwrite("test.jpg",img); to save an image,
here is an example-
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
imwrite("test.jpg",img);
}