您的位置:首页 > Web前端

vl_feat 调用 lbp 特征 c接口

2016-06-27 22:35 295 查看
matlab 瞬间就能查到资料,然而c还是有几个注意的点的!

先贴代码 

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
using namespace cv;
using namespace std;

extern "C"
{
#include <vl/generic.h>
#include <vl/stringop.h>
#include <vl/pgm.h>
#include<vl/lbp.h>
#include<vl/getopt_long.h>
};

vl_size cellsize = 4;

void
vl_lbp_process_my (VlLbp * self,
float * features,
float * image, vl_size width, vl_size height,
vl_size cellSize);

int main(){

string imgPath ="E:/face system program/image deal/cohn-kanada-deal-dlib/2d-2.5d/S005_001_00000011.png";
IplImage* imgrgb = cvLoadImage(imgPath.c_str(),1);
IplImage* img = cvCreateImage(cvGetSize(imgrgb),IPL_DEPTH_8U,1);
cvCvtColor(imgrgb,img,CV_RGB2GRAY);

VlLbpMappingType type;
type = VlLbpUniform;
VlLbp* lbpfeature = vl_lbp_new(type,true);

vl_size dim = vl_lbp_get_dimension(lbpfeature);

float* feature = (float*)malloc(((img->width/cellsize) * (img->height/cellsize)*dim)*sizeof(float));
if(img!=NULL){
float *frame = (float*)malloc(img->height*img->width*sizeof(float));
uchar* Ldata = (uchar*)img->imageData;
int count =0;
for(int i = 0 ;i < img ->height ;i++){
for(int j = 0 ; j < img -> width ; j++){
frame[i*img->width+j] = (float)Ldata[i*img->widthStep+j*img->nChannels];
}
}

/* self LBP object.
features buffer to write the features to.
image image.
width image width.
height image height.
cellSize size of the LBP cells.*/
vl_lbp_process_my(lbpfeature, feature,frame,img->width,img->height,cellsize);
vl_lbp_delete(lbpfeature);
}
system("pause");
return 0;
}

void
vl_lbp_process_my (VlLbp * self,
float * features,
float * image, vl_size width, vl_size height,
vl_size cellSize)
{
vl_size cwidth = width / cellSize;
vl_size cheight = height / cellSize ;
vl_size cstride = cwidth * cheight ;
vl_size cdimension = vl_lbp_get_dimension(self) ;
vl_index x,y,cx,cy,k,bin ;

#define at(u,v) (*(image + width * (v) + (u)))
#define to(u,v,w) (*(features + cstride * (w) + cwidth * (v) + (u)))

/* clear the output buffer */
memset(features, 0, sizeof(float)*cdimension*cstride) ;

/* accumulate pixel-level measurements into cells */
for (y = 1 ; y < (signed)height - 1 ; ++y) {
float wy1 = (y + 0.5f) / (float)cellSize - 0.5f ;
int cy1 = (int) vl_floor_f(wy1) ;
int cy2 = cy1 + 1 ;
float wy2 = wy1 - (float)cy1 ;
wy1 = 1.0f - wy2 ;
if (cy1 >= (signed)cheight) continue ;

for (x = 1 ; x < (signed)width - 1; ++x) {
float wx1 = (x + 0.5f) / (float)cellSize - 0.5f ;
int cx1 = (int) vl_floor_f(wx1) ;
int cx2 = cx1 + 1 ;
float wx2 = wx1 - (float)cx1 ;
wx1 = 1.0f - wx2 ;
if (cx1 >= (signed)cwidth) continue ;

{
int unsigned bitString = 0 ;
float center = at(x,y) ;
if(at(x+1,y+0) > center) bitString |= 0x1 << 0; /* E */
if(at(x+1,y+1) > center) bitString |= 0x1 << 1; /* SE */
if(at(x+0,y+1) > center) bitString |= 0x1 << 2; /* S */
if(at(x-1,y+1) > center) bitString |= 0x1 << 3; /* SW */
if(at(x-1,y+0) > center) bitString |= 0x1 << 4; /* W */
if(at(x-1,y-1) > center) bitString |= 0x1 << 5; /* NW */
if(at(x+0,y-1) > center) bitString |= 0x1 << 6; /* N */
if(at(x+1,y-1) > center) bitString |= 0x1 << 7; /* NE */
bin = self->mapping[bitString] ;
}

if ((cx1 >= 0) & (cy1 >=0)) {
to(cx1,cy1,bin) += wx1 * wy1;
}
if ((cx2 < (signed)cwidth) & (cy1 >=0)) {
to(cx2,cy1,bin) += wx2 * wy1 ;
}
if ((cx1 >= 0) & (cy2 < (signed)cheight)) {
to(cx1,cy2,bin) += wx1 * wy2 ;
}
if ((cx2 < (signed)cwidth) & (cy2 < (signed)cheight)) {
to(cx2,cy2,bin) += wx2 * wy2 ;
}
} /* x */
} /* y */

/* normalize cells */
for (cy = 0 ; cy < (signed)cheight ; ++cy) {
for (cx = 0 ; cx < (signed)cwidth ; ++ cx) {
float norm = 0 ;
for (k = 0 ; k < (signed)cdimension ; ++k) {
norm += features[k * cstride] ;
}
norm = sqrtf(norm) + 1e-10f; ;
for (k = 0 ; k < (signed)cdimension ; ++k) {
features[k * cstride] = sqrtf(features[k * cstride]) / norm ;
}
features += 1 ;
}
} /* next cell to normalize */
}

里面有几点注意
1 重点是要把IplImage 转换成float类型

2 我开始直接调用dll 中的lbp_process 一直报 .1.#ND 错误,把里面实现代码拷贝出来,结果就对了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vl_feat