1. 首先写好待编译的.cpp文件,使用混合编程,以人脸检测为例

#include "mex.h" // Required for the use of MEX files

// Required for OpenCV
#include "cv.h"
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;

/** This is a the only prototype function that you need to get a mex file to work. */
void mexFunction(int output_size, mxArray *output[], int input_size, const mxArray *input[])
{
char *input_buf;
int buflen;
mxArray *xData;
double *xValues;
int i,j;
int NoOfCols, NoOfRows;

/* check for proper number of arguments */
if(input_size!=2)
mexErrMsgTxt("Usage: FaceDetect (<HaarCascade File>, <GrayImage>)");

/* input must be a string */
if ( mxIsChar(input[0]) != 1)
mexErrMsgTxt("Input 1 must be a string.");

/* get the length of the input string */
buflen = (mxGetM(input[0]) * mxGetN(input[0])) + 1;

/* copy the string data from input[0] into a C string input_ buf. */
input_buf = mxArrayToString(input[0]);

if(input_buf == NULL)
mexErrMsgTxt("Could not read HarrCascade Filename to string.");

// Read the Haar Cascade from the XML file
cascade = (CvHaarClassifierCascade*) cvLoad( input_buf, 0, 0, 0);

if( !cascade )
{
mexErrMsgTxt("ERROR: Could not load classifier cascade" );
}

/* Check if the input image is in double format*/
if (!(mxIsDouble(input[1]))) {
mexErrMsgTxt("Input image must be gray scale and of type double");
}

//Copy input pointer
// This carries the input grayscale image that was sent from Matlab
xData = (mxArray *)input[1];

//Get the matrix from the input data
// The matrix is rasterized in a column wise read
xValues = mxGetPr(xData);
NoOfCols = mxGetN(xData); // Gives the number of Columns in the image
NoOfRows = mxGetM(xData); // Gives the number of Rows in the image

/* Get the number of dimensions in the input Image */
int number_of_dims = mxGetNumberOfDimensions(input[1]);
if (number_of_dims > 2)
mexErrMsgTxt("Input image should be gray scale and of type double");

// Create an IplImage from the data so face detection can be run on it
IplImage* gray = cvCreateImage( cvSize(NoOfCols, NoOfRows), IPL_DEPTH_8U, 1 );

// Load the column wise vector into the IplImage
// IplImage data is read in a rowwise manner
// Appropriate conversion is carried out here
for(i=0;i<NoOfCols;i++)
for(j=0;j<NoOfRows;j++)
{
int value = xValues[(i*NoOfRows)+j];
gray->imageData[j*NoOfCols+i] = value;
}

/**********************************************************************
* There is a bug in OpenCV that if one calls the cvLoad function before calling
* any other function from the cxCore lib, an error is thrown by the
* cvRead function that is part of cvLoad. In order to overcome this
* any function from the cxcore lib needs to be called. Here we create
* a dummy image 11x11 pixels in size and erode the image using a small
* kernel.
***********************************************************************/
IplImage* dummy = cvCreateImage( cvSize(11, 11), IPL_DEPTH_8U, 1 ); // Make a dummy image
IplConvKernel* se = cvCreateStructuringElementEx(3,3,1,1,CV_SHAPE_ELLIPSE); // Create a filter
cvErode(dummy,dummy,se); // Erode
cvReleaseImage( &dummy );

/*******************************************************************
* Detect faces *
********************************************************************/

// Histogram Equalize the image
cvEqualizeHist( gray, gray);

// This is required in the face detection process
storage = cvCreateMemStorage(0);
cvClearMemStorage( storage );

// Do Face Detection
CvSeq* faces = cvHaarDetectObjects( gray, cascade, storage,
1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(30, 30) );

// Allocate output variable
// Number of rows = number of detected faces
// Number of columns = 4
// 1: Location X of the face
// 2: Location Y of the face
// 3: Width of the face
// 4: Height of the face
double *Data;
if (faces->total)
{
output[0] = mxCreateDoubleMatrix(faces->total, 4, mxREAL);
Data = mxGetPr(output[0]); // Get the pointer to output variable
// Iterate trou each of the detected faces
for( i = 0; i < faces->total; i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
/* The Data pointer again has to be filled in a column wise manner
* The first column will contain the x location of all faces
* while column two will contain y location of all faces */
Data [i] = r->x;
Data [i+faces->total] = r->y;
Data [i+faces->total*2] = r->width;
Data [i+faces->total*3] = r->height;
// Debug
// printf ("%d %d %d %d\n", r->x, r->y, r->width, r->height);
}
}
else
{
output[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
Data = mxGetPr(output[0]); // Get the pointer to output variable
Data[0] = -1;
}

// Release all the memory
cvReleaseImage( &gray );
cvReleaseMemStorage(&storage);
cvReleaseHaarClassifierCascade(&cascade);
return;
}

2. 写好待mex 的.m文件(转自http://www.cnblogs.com/lukylu/p/3966871.html)

注意这里要把opencv的bin目录(C:\opencv\build\x64\vc10\bin)添加到环境变量里,重启电脑。

mex -setup 的时候去与之相应的c++ vs2010 编译器。

clear all;

% Get the architecture of this computer
is_64bit = strcmp(computer,'MACI64') || strcmp(computer,'GLNXA64') || strcmp(computer,'PCWIN64');

%----------------------------------------------------------------------------------------------
%% The configuration of compiler
% You need to modify this configuration according to your own path of OpenCV
% Notice: if your system is 64bit, your OpenCV must be 64bit!
out_dir='./';
CPPFLAGS = ' -O -DNDEBUG -I.\ -IC:\opencv\build\include -IC:\opencv\build\include\opencv -IC:\opencv\build\include\opencv2'; % your OpenCV "include" path
LDFLAGS = ' -LC:\opencv\build\x64\vc10\lib'; % your OpenCV "lib" path
%LIBS = ' -lopencv_calib3d249d -lopencv_contrib249d -lopencv_core249d -lopencv_features2d249d -lopencv_flann249d -lopencv_gpu249d -lopencv_highgui249d -lopencv_imgproc249d -lopencv_legacy249d -lopencv_ml249d -lopencv_nonfree249d -lopencv_objdetect249d -lopencv_photo249d -lopencv_stitching249d -lopencv_ts249d -lopencv_video249d -lopencv_videostab249d';
LIBS = ' -lopencv_calib3d249 -lopencv_contrib249 -lopencv_core249 -lopencv_features2d249 -lopencv_flann249 -lopencv_gpu249 -lopencv_highgui249 -lopencv_imgproc249 -lopencv_legacy249 -lopencv_ml249 -lopencv_nonfree249 -lopencv_objdetect249 -lopencv_photo249 -lopencv_stitching249 -lopencv_ts249 -lopencv_video249 -lopencv_videostab249';
if is_64bit
CPPFLAGS = [CPPFLAGS ' -largeArrayDims'];
end

% add your files here!!
compile_files = {
%the list of your code files which need to be compiled
% 'ImageCalibration.cpp'
'FaceDetect.cpp'
};
%----------------------------------------------------------------------------------------------

%----------------------------------------------------------------------------------------------
%% compiling
for k = 1 : length(compile_files)
str = compile_files{k};
fprintf('compilation of: %s\n', str);
str = [str ' -outdir ' out_dir CPPFLAGS LDFLAGS LIBS];
args = regexp(str, '\s+', 'split');
mex(args{:});
end
fprintf('Congratulations, compilation successful!!!\n');

matlab下mex 调用opencv库的更多相关文章

  1. windows下Qt Creator5.1.0编写程序以及调用OpenCV库

    系统说明 最近使用opencv编写程序,程序编的差不多就学习使用QT加个界面,首先声明下本人的系统和使用的软件版本, 系统: windows xp QT IDE:QT Creator5.1.0 Ope ...

  2. 简单的调用OpenCV库的Android NDK开发 工具Android Studio

    前言 本博客写于2017/08/11, 博主非专业搞安卓开发, 只是工作的需要倒腾了下Android NDK相关的开发, 博文中有什么不正确.不严格的地方欢迎指正哈    本文后续也许还会有删改, 就 ...

  3. python调用opencv库教程

    OpenCV安装pip install --upgrade setuptoolspip install numpy Matplotlibpip install opencv-python OpenCV ...

  4. python调用Opencv库和dlib库

    python是一门胶水语言,可以调用C++编译好的dll库 python调用opencv-imggui.dll文件 https://www.cnblogs.com/zhangxian/articles ...

  5. Linux下JNA 调用 so 库

    原文:https://blog.csdn.net/withiter/article/details/8077470 博文链接:https://i.cnblogs.com/EditPosts.aspx? ...

  6. Linux下Qt调用共享库文件.so

    修改已有的pro文件,添加如下几句: INCLUDEPATH += /home/ubuntu/camera/camera/LIBS += -L/home/ubuntu/camera/camera -l ...

  7. 一种调用opencv库的C++工程通用的Makefile模板

    第一次自己写makefile,记录一下 #Compilers #CXX=/opt/compiler/gcc-/bin/g++ CXX = g++ #Includes INCLUDE_FLAGS = - ...

  8. [转] Matlab与C++混合编程,添加OpenCV库

    原文地址 峰回璐转 最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍 受.软件立刻移植到C++上又不太实际,故采用 ...

  9. Matlab与C++混合编程,添加OpenCV库

    最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍受.软件立刻移植到C++上又不太实际,故采用联合编程的方式,速度难 ...

随机推荐

  1. ubuntu系统安装mysql二进制压缩包(tar.gz)以及navicat远程连接服务器(linux系统)

    一.ubuntu安装mysql5.6二进制压缩包(tar.gz) 准备 0. 获取 mysql-5.5.15-linux2.6-i686.tar.gz 二进制安装文件 mysql 官网下载页面选择 L ...

  2. Drools规则引擎环境搭建

    Drools 是一款基于Java 的开源规则引擎,所以在使用Drools 之前需要在开发机器上安装好JDK 环境,Drools5 要求的JDK 版本要在1.5 或以上. Drools5 提供了一个基于 ...

  3. C++构造函数和析构函数顺序

    构造函数    先看看构造函数的调用顺序规则,只要我们在平时编程的时候遵守这种约定,任何关于构造函数的调用问题都能解决:构造函数的调用顺序总是如下:1.基类构造函数.如果有多个基类,则构造函数的调用顺 ...

  4. php桶排序简单实现

    桶排序中最重要的环节是映射函数. 初步学习桶排序的过程中,映射比较简单.实现代码如下: /** * 第一种桶排序的办法,每个桶存储相同值的数据 * */ function bucketSort($no ...

  5. 多例模式,保证实例的唯一性,仅适用于form窗体

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. Codeforces 807 B T-Shirt Hunt

    B. T-Shirt Hunt http://codeforces.com/problemset/problem/807/B time limit per test 2 seconds memory ...

  7. sublime wrong

    Q1: sublime报错: There are no packages available for installation A1: window下的:C:\Windows\System32\dri ...

  8. CF839 D 容斥

    求$gcd>1$的所有$gcd(a_i,a_{i+1}…a_{n})*(n-i+1)$的和 首先先标记所有出现的数.从高到低枚举一个数k,记录它的倍数出现次数cnt,那么当前所有组合的答案就是$ ...

  9. Java读取大文件的高效率实现

    1.概述 本教程将演示如何用Java高效地读取大文件.这篇文章是Baeldung (http://www.baeldung.com/) 上“Java——回归基础”系列教程的一部分. 2.在内存中读取 ...

  10. 【CF802C】 Heidi and Library (hard)(费用流)

    题目链接 感觉跟餐巾计划问题有点像.费用流. 决定每天买不买不太好搞,不如先把所有东西都买进来,再卖掉不必要的. 拆点,每个点拆成\(x,y\). 源点向每个点的\(x\)连费用为当天的价格,流量为1 ...