问题描述
- 关于关于这个代码中终点计算的问题
-
请问师兄,下面的那个uniform完整代码中终点为什么要这么计算?
还有基于uniform的旋转不变性LBP值,在u>2时是p+1?
解决方案
为什么看不到你有粘代码?代码贴出来吧~这样不知道你的问题在哪里
解决方案二:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#define PI 3.1415926
#define MAX(x,y) (x)>(y)?(x):(y)
#define MIN(x,y) (x)<(y)?(x):(y)
typedef struct MyPoint
{
double x;
double y;
}MyPoint;
void calc_position(int radius,int num_sp,MyPoint *spoint)
{
double theta;
theta = 2*PI/num_sp;
for (int i = 0; i < num_sp; i++)
{
spoint[i].y = -radius * sin(i * theta);
spoint[i].x = radius * cos(i * theta);
}
}
int calc_sum(int r)
{
int res_sum;
res_sum = 0;
while (r)
{
res_sum = res_sum + r % 2;
r /= 2;
}
return res_sum;
}
void rotation_uniform_invariant_mapping(int range,int num_sp,int *Mapping)
{
int numt,i,j,tem_xor;
numt = 0;
tem_xor = 0;
for (i = 0; i< range; i++)
{
j = i << 1;
if (j > range -1)
{
j = j - (range -1);
}
tem_xor = i ^ j; // 异或
numt = calc_sum(tem_xor);//计算异或结果中1的个数,即跳变个数
if (numt <= 2)
{
Mapping[i] = calc_sum(i);
}else{
Mapping[i] = num_sp+1;
}
}
}
void rotation_uniform_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint,int *Mapping)
{
IplImage *target,*hist;
int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;
double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;
int *result;
float dishu;
dishu = 2.0;
max_x=0;max_y=0;min_x=0;min_y=0;
for (k=0;k<num_sp;k++)
{
if (max_x<spoint[k].x)
{
max_x=spoint[k].x;
}
if (max_y<spoint[k].y)
{
max_y=spoint[k].y;
}
if (min_x>spoint[k].x)
{
min_x=spoint[k].x;
}
if (min_y>spoint[k].y)
{
min_y=spoint[k].y;
}
}
//计算模版大小
box_x = ceil(MAX(max_x,0)) - floor(MIN(min_x,0)) + 1;
box_y = ceil(MAX(max_y,0)) - floor(MIN(min_y,0)) + 1;
if (width<box_x||height<box_y)
{
printf("Too small input image. Should be at least (2*radius+1) x (2*radius+1)");
return;
}
//计算可滤波图像大小,opencv图像数组下标从0开始
orign_x = 0 - floor(MIN(min_x,0));//起点
orign_y = 0 - floor(MIN(min_x,0));
dx = width - box_x+1;//终点
dy = height - box_y+1;
target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);
result = (int *)malloc(sizeof(int)*dx*dy);
memset(result,0,sizeof(int)*dx*dy);
CvRect roi =cvRect(orign_x, orign_y, dx, dy);
cvSetImageROI(src, roi);
cvCopy(src, target);
cvResetImageROI(src);
cvSaveImage("haha.jpg",target);
for ( k = 0; k<num_sp;k++)
{
x = spoint[k].x+orign_x;
y = spoint[k].y+orign_y;
//二线性插值图像
fy = floor(y); //向下取整
fx = floor(x);
cy = ceil(y); //向上取整
cx = ceil(x);
ty = y - fy;
tx = x - fx;
w1 = (1 - tx) * (1 - ty);
w2 = tx * (1 - ty);
w3 = (1 - tx) * ty ;
w4 = tx * ty ;
v = pow(dishu,(float)k);
for (i = 0;i<dy;i++)
{
for (j = 0;j<dx;j++)
{
//灰度插值图像像素
N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+
w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+
w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+
w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];
if( N >= (double)(unsigned char)target->imageData[i*dx+j])
{
result[i*dx+j] = result[i*dx+j] + v * 1;
}else{
result[i*dx+j] = result[i*dx+j] + v * 0;
}
}
}
}
//将result的值映射为mapping的值
for(i = 0; i < dy ;i++)
{
for (j = 0; j < dx ;j ++)
{
result[i*dx+j] = Mapping[result[i*dx+j]];
}
}
//显示图像
int cols = 0;//直方图的横坐标,也是result数组的元素种类
int mapping_size = pow(dishu,(float)num_sp);
for (i = 0;i < mapping_size; i++ )
{
if (cols < Mapping[i])
{
cols = Mapping[i];
}
}
if (cols < 255)
{
//只有采样数小于8,则编码范围0-255,才能显示图像
for (i = 0;i<dy;i++)
{
for (j = 0;j<dx;j++)
{
target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];
//printf("%dn",(unsigned char)target->imageData[i*width+j]);
}
}
cvSaveImage("result.jpg",target);
}
//计算直方图
hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);//直方图图像
double *val_hist = (double *)malloc(sizeof(double)*cols); //直方图数组
for (i=0;i<cols;i++)
{
val_hist[i]=0.0;
}
for (i=0; i<dy*dx;i++)
{
val_hist[result[i]]++;
}
double temp_max=0.0;
for (i=0;i<cols;i++) //求直方图最大值,为了归一化
{
//printf("%fn",val_hist[i]);
if (temp_max<val_hist[i])
{
temp_max=val_hist[i];
}
}
//画直方图
CvPoint p1,p2;
double bin_width=(double)hist->width/cols;
double bin_unith=(double)hist->height/temp_max;
for (i=0;i<cols;i++)
{
p1.x=i*bin_width;p1.y=hist->height;
p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;
cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);
}
cvSaveImage("hist.jpg",hist);
}
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *src,*grey,*result;
int samples,radius,range,*mapping;
MyPoint *spoint;
float Mi;
samples = 8;
radius = 10;
Mi = 2.0;
range = pow(Mi,samples);
src = cvLoadImage("test2.jpg");
grey = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
cvCvtColor(src,grey,CV_BGR2GRAY);
mapping = (int *)malloc(sizeof(int)*range);
memset(mapping,0,sizeof(int)*range);
//计算采样点相对坐标
spoint = (MyPoint *)malloc(sizeof(MyPoint)*samples);
calc_position(radius,samples,spoint);
//计算灰度不变性LBP特征,写回浮点数图像矩阵中
//gray_invariant_lbp(grey,src->height,src->width,samples,spoint);
//计算旋转不变形LBP特征
//rotation_invariant_mapping(range,samples,mapping);
//rotation_invariant_lbp(grey,src->height,src->width,samples,spoint,mapping);
//计算旋转不变等价LBP特征
rotation_uniform_invariant_mapping(range,samples,mapping);
rotation_uniform_invariant_lbp(grey,src->height,src->width,samples,spoint,mapping);
return 0;
}