SIFT算法的第一个部分是尺度空间的构建,需要生成高斯金字塔和高斯差分金字塔,步骤细节和部分数学原理(https://blog.csdn.net/lcj369387335/article/details/18258333)
函数-高斯金字塔和高斯差分金字塔的构建(引自https://download.csdn.net/download/bjaijyj/9533770):
void MySift::BuildGaussianPyramid()
{
sigma[0][0] = sqrt(0.5);
Img1Down[0] = Img1;
Img2Down[0] = Img2;
for (int i = 0; i < nOctaves; i++)
{
for (int j = 0; j < nlayers; j++)
{
sigma[i][j] = sqrt(0.5) * pow(2, (double)j / 5);
}
}
for (int i = 0; i < nOctaves - 1; i++)
{
resize(Img1Down[i], Img1Down[i + 1], Size(Img1Down[i].cols / 2, Img1Down[i].rows / 2), 0, 0, INTER_LINEAR);
resize(Img2Down[i], Img2Down[i + 1], Size(Img2Down[i].cols / 2, Img2Down[i].rows / 2), 0, 0, INTER_LINEAR);
}
for (int i = 0; i < nOctaves; i++)
{
for (int j = 0; j < nlayers; j++)
{
GaussianBlur(Img1Down[i], Img1Gr[i][j], Size(), sigma[i][j], sigma[i][j]);//伴随序号sigma每组每层逐渐增大
GaussianBlur(Img2Down[i], Img2Gr[i][j], Size(), sigma[i][j], sigma[i][j]);
}
}
}
void MySift::BuildDoG()
{
for (int i = 0; i < nOctaves; i++)
{
for (int j = 0; j < nlayers - 1; j++)
{
subtract(Img1Gr[i][j], Img1Gr[i][j + 1], Img1Sub[i][j]);//, noArray(), CV_16S);
subtract(Img2Gr[i][j], Img2Gr[i][j + 1], Img2Sub[i][j]);//, noArray(), CV_16S);
}
}
}
函数-显示高斯金字塔的构建结果(自编):
void MySift::ShowPyramid()
{
Rect PyramidROI[12][12];
int basecols=400; //0层的图像宽度
int baserows = round(400 * Img1.rows / Img1.cols); //0层的图像高度
int w=0,h=0,x=0,y=0;
Mat currentimg;
Mat showimg(basecols * 3, basecols * 6, Img1Gr[0][0].type());
for (int i = 0; i < nOctaves; i++)
{
for (int j = 0; j < nlayers; j++)
{
w = basecols / pow(2, i); //当前金字塔层图像的宽度
h = baserows / pow(2, i); //当前金字塔层图像的高度
x = w*j; //当前金字塔层图像的左上角角点横坐标
PyramidROI[i][j] = Rect(x, y, w, h);
}
y = y + h; //当前金字塔层图像的左上角角点纵坐标
}
for (int i = 0; i < nOctaves; i++)
{
for (int j = 0; j < nlayers; j++)
{
resize(Img1Gr[i][j], currentimg, Size(PyramidROI[i][j].width, PyramidROI[i][j].height), 0, 0, INTER_LINEAR);
Rect roi_rect= PyramidROI[i][j]; //当前图像在画布上的区域
currentimg.copyTo(showimg(roi_rect)); //将当前金字塔层图像粘贴到画布上
}
}
namedWindow("Gaussian Pyramid", WINDOW_AUTOSIZE);
imshow("Gaussian Pyramid", showimg);
waitKey(0);
destroyWindow("Gaussian Pyramid");
}
显示出的结果如下:
下一步是尺度空间极值点的检测。