[C++] 纯文本查看 复制代码#include
#include
#include
#include
#include
#include
#define PI 3.1415926
#define INF 99999999 用于直线斜率的初始化,代表无穷大
using namespace std;
using namespace cv;
CvMemStorage *storage = cvCreateMemStorage();内存块,存储中间变量
CvSeq *lines = 0;存储Hough 变换所得结果
//高斯金字塔结构体
typedef struct GaussPyr
{
IplImage *lev[9];
}GaussPyr;
//将金字塔尺寸定义为全局变量
CvSize PyrSize[9] = { NULL };
//初始化金字塔结构体
void initPyr(GaussPyr *p)
{
for (int i = 0; i < 9; i++)
p->lev[i] = cvCreateImage(PyrSize[i], IPL_DEPTH_64F, 1);
}
//根据层数,求第i层的尺寸
int downsample(int x, int level)
{
if (level-- > 0)
{
if (x % 2 == 0)
x = x / 2;
else
x = (x + 1) / 2;
downsample(x, level);
}
if (level == -1)
return x;
}
//计算并产生一幅图的高斯金字塔 每层的图像
void Gscale(GaussPyr *p, IplImage *data, int level, double sigma)
{
for (int i = 0; i
{
if (i == 0)
cvSmooth(data, p->lev[0], CV_GAUSSIAN, 5, 5, sigma, 0);
else
{
IplImage *tem = cvCreateImage(PyrSize[i - 1], IPL_DEPTH_64F, 1);
cvSmooth(p->lev[i - 1], tem, CV_GAUSSIAN, 5, 5, sigma, 0);
for (int a = 0; a < PyrSize[i].height; a++)
for (int b = 0; b < PyrSize[i].width; b++)
((double *)(p->lev[i]->imageData + a*p->lev[i]->widthStep))[b] = ((double *)(tem->imageData + 2 * a*tem->widthStep))[2 * b];
}
}
}
//c-s过程中用到的跨尺度相减
void overScaleSub(IplImage *s1, IplImage *s2, IplImage *dst)
{
cvResize(s2, dst, CV_INTER_LINEAR);
cvAbsDiff(s1, dst, dst);
}
//求图像的局部最大值
void getLocalMaxima(IplImage *scr, double thresh, double *lm_sum, int *lm_num, double*lm_avg)
{
*lm_sum = 0.0;
*lm_num = 0;
*lm_avg = 0.0;
int count = 0;
//查找局部最大值
for (int a = 1; a < ((scr->height) - 1); a++)
for (int b = 1; b< ((scr->width) - 1); b++)
{
double val = ((double *)(scr->imageData + a*scr->widthStep))[b];
if ((val >= thresh) &&
(val >= ((double *)(scr->imageData + (a - 1)*scr->widthStep))[b]) &&
(val >= ((double *)(scr->imageData + (a + 1)*scr->widthStep))[b]) &&
(val >= ((double *)(scr->imageData + a*scr->widthStep))[b - 1]) &&
(val >= ((double *)(scr->imageData + a*scr->widthStep))[b + 1]))
{
if (val == 10) count++;//因为设定图像的最大值是10
*lm_sum += val;//局部最大值加上全局最大值
(*lm_num)++;//总的个数 包含局部和全局
}
}
if (*lm_num > count)
{
*lm_sum = *lm_sum - 10 * count;//局部最大值的总和
*lm_num = *lm_num - count;//局部最大值的个数
if (*lm_num > 0)
*lm_avg = *lm_sum / *lm_num;//局部最大值的平均值
else
*lm_avg = 0;
}
else
*lm_avg = 0;
}
//N操作,包括下采样到第5层的尺寸