Skip to content
Create a gist now

Instantly share code, notes, and snippets.

This is the main part, a snippet of a face detection program, runable if you have OpenCV installed
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
//My c wrapper for vector<Rect>, to convert a vector<Rect> to a c array. Rect is a C++ class
typedef vector<Rect> vector_Rect;
Rect* std_vectorrToCArray(vector_Rect* s) {
return s->data();
// my c wrapper for OpenCV's detectMultiScale here:
void cv_CascadeClassifier_detectMultiScale(CascadeClassifier* self, Mat* image, vector_Rect* objects, double scaleFactor, int minNeighbors, int flags, Size* minSize, Size* maxSize) {
self->detectMultiScale(*image, *objects, scaleFactor, minNeighbors, flags, *minSize, *maxSize);
// Global variables
// create data to use in detectMultiScale
string face_cascade_name = "/home/w/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
int main()
{ //Create c++ vector
std::vector<Rect> faces;
Mat frame_gray;
//load image with a face on it
Mat frame = imread( "/home/w/Desktop/opencv-master/samples/cpp/lena.jpg", 1);
//Convert image to grayscale so its ready to use in detectMultiScale
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
//equalize image so its ready to use in detectMultiScale
equalizeHist(frame_gray, frame_gray);
// create size params for detectMultiScale
Size a = Size(30, 30);
Size b = Size();
// run face detector , the faces variable is set with 1 or more vector<Rect> by detectMultiScale, usually 0, 1, or 2
//I'm using my c wrapper for c++ detectMultiScale here to test it and it works as good as the the C++ function it wraps
cv_CascadeClassifier_detectMultiScale(&face_cascade, &frame_gray, &faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, &a,&b);
// Set Region of Interest
size_t ic = 0; // ic is index of current element
for (ic = 0; ic < faces.size(); ic++)
{// using my vector wrapper to convert the vector<Rect> output to a Rect*
Rect* c = std_vectorrToCArray(&faces);
//here I can access different elements of the array C, where in Lisp I cant.
In Lisp when I run (cffi:mem-aref (vector-rect-to-c-array C) ic) I get a memory fault error...cont. at bottom
cout << c[ic].x;
here is my wrapper for std_vectorr_to_carray...I think its defined correctly
(defcfun ("std_vectorr_to_carray" %vector-rect-to-c-array) :pointer
(s (:pointer vector-rect)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.