画出SIFT高斯金字塔和DOG金字塔

https://blog.csdn.net/zddblog/article/details/7521424

https://codeload.github.com/gz7seven/image-processing-C---/zip/master

//高斯金字塔
void GaussianPyramid(const Mat &src, vector<Mat>&gauss_pyr, int octaves, int intervals = INTERVALS, double sigma = SIGMA)
{
	//
	double *sigmas = new double[intervals+3];
	double k = pow(2.0, 1.0/intervals);

//cout <<"k=" <<k<<endl;
	sigmas[0] = sigma;
/*
	for(int i = 1; i < intervals+3; i++)
	{
		sigmas[i] = k*sigmas[i-1];
//cout << " "<<sigmas[i] ;
	}
*/
	double sig_prev, sig_total;
	for(int i = 1; i < intervals + 3; i++ )
	{
		sig_prev = pow( k, i - 1 ) * sigma;
		sig_total = sig_prev * k;
		sigmas[i] = sqrt( sig_total * sig_total - sig_prev * sig_prev );
	}

	for(int o = 0; o < octaves; o++)
	{
		//每组多三层
		for(int i = 0; i < intervals+3; i++)
		{
			Mat mat;
			if(o == 0 && i == 0)
			{
				src.copyTo(mat);
			}
			else if(i == 0)
			{
				//zdd于2012年5月17日修正
//				DownSample(gauss_pyr[o*(intervals+3)-2], mat);  //error
				//前一组高斯图像的倒数第三层
				//如图像下标为:
				//0 1 2 3  4  5  //o=0
				//6 7 8 9 10 11  //o=1
				//...
				//第一组第一张图(下标为6)的图像是0组下标为3的图像降采样得来
				DownSample(gauss_pyr[(o-1)*(intervals+3)+intervals], mat);
			}
			else
			{
				//每组中下一层由上一层高斯模糊得到
				GaussianSmooth(gauss_pyr[o * (intervals+3)+i-1], mat, sigmas[i]);
			}
			gauss_pyr.push_back(mat);
			std::stringstream ssTemp1;
			ssTemp1 << o;
			std::string strDst1 = ssTemp1.str();
			std::stringstream ssTemp2;
			ssTemp2 << i;
			std::string strDst2 = ssTemp2.str();

			string save_path = "GaussianPyramid_octaves_" + strDst1 + "_intervals_" + strDst2 + ".jpg";
			Mat img2;
			convertScaleAbs(mat, img2, 255, 0);
			img2.convertTo(img2, CV_8UC1);
			cvtColor(img2, img2, CV_GRAY2BGR);
			cv::imwrite(save_path, img2);
		}
	}
	
	delete[] sigmas;
}
//差分金字塔
void DogPyramid(const Vector<Mat>& gauss_pyr, Vector<Mat>& dog_pyr, int octaves, int intervals=INTERVALS)
{
	for(int o = 0; o < octaves; o ++)
	{
		for(int i = 1; i < intervals+3; i++)
		{
			Mat mat;
			Sub(gauss_pyr[o*(intervals+3)+i], gauss_pyr[o*(intervals+3)+i-1], mat);
			dog_pyr.push_back(mat);
			std::stringstream ssTemp1;
			ssTemp1 << o;
			std::string strDst1 = ssTemp1.str();

			std::stringstream ssTemp2;
			ssTemp2 << i;
			std::string strDst2 = ssTemp2.str();

			string save_path = "DogPyramid_octaves_" + strDst1 + "_intervals_" + strDst2 + ".jpg";
			Mat img2;
			convertScaleAbs(mat, img2, 255, 0);
			img2.convertTo(img2, CV_8UC1);
			cvtColor(img2, img2, CV_GRAY2BGR);
			cv::imwrite(save_path, img2);
		}
	}
}

 

添加-1层 

# -*- coding: utf-8 -*-
import cv2
import os
import sys
import numpy as np

def searchDirFile(rootDir):
    garyimglist = []
    depimglist = []
    namelist=[]

    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        # 判断是否为文件
        if os.path.isfile(filePath):
            # 如果是文件再判断是否以.jpg结尾,不是则跳过本次循环
            if os.path.basename(filePath).endswith('.jpg'):
                if filePath.split('/')[-1].split('_')[0]=="GaussianPyramid":
                    garyimglist.append(filePath)
                    namelist.append(filePath.split('/')[-1].split('.jpg')[0])
            else:
                continue
        # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
        elif os.path.isdir(filePath):
            searchDirFile(filePath)
        else:
            print('not file and dir '+os.path.basename(filePath))
    return garyimglist,namelist

octa,inte = 7,6
ImageList = ['']*octa*inte
path = '/home/spple/lena'
garyimglist, namelist = searchDirFile(path)
for key in range(len(namelist)):
    subname = namelist[key].split('_')
    octaves = int(subname[2])
    intervals = int(subname[4])
    # if (octaves == 0):
    #     continue
    # ImageList[(octaves-1)*inte+intervals] = cv2.imread(garyimglist[key])
    ImageList[(octaves)*inte+intervals] = cv2.imread(garyimglist[key])
vis = np.zeros((ImageList[0].shape[:2][0]*inte,ImageList[0*inte].shape[:2][1]+ImageList[1*inte].shape[:2][1]+
                                            ImageList[2*inte].shape[:2][1]+ImageList[3*inte].shape[:2][1]+
                                            #ImageList[4*inte].shape[:2][1]+ImageList[5*inte].shape[:2][1]), np.uint8)
                                            ImageList[4*inte].shape[:2][1]+ImageList[5*inte].shape[:2][1]+
                                            ImageList[6*inte].shape[:2][1]), np.uint8)
shapelist = 0
for cols in range(octa):
    for rows in range(inte):
        h,w = ImageList[cols*inte+rows].shape[:2]
        image = ImageList[cols*inte+rows][:,:,0]
        vis[h*rows:h*(rows+1), shapelist:w+shapelist] = image
    shapelist += w
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
cv2.imwrite("./all.jpg", vis)

 

不添加-1层  

# -*- coding: utf-8 -*-
import cv2
import os
import sys
import numpy as np

def searchDirFile(rootDir):
    garyimglist = []
    depimglist = []
    namelist=[]

    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        # 判断是否为文件
        if os.path.isfile(filePath):
            # 如果是文件再判断是否以.jpg结尾,不是则跳过本次循环
            if os.path.basename(filePath).endswith('.jpg'):
                if filePath.split('/')[-1].split('_')[0]=="GaussianPyramid":
                    garyimglist.append(filePath)
                    namelist.append(filePath.split('/')[-1].split('.jpg')[0])
            else:
                continue
        # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
        elif os.path.isdir(filePath):
            searchDirFile(filePath)
        else:
            print('not file and dir '+os.path.basename(filePath))
    return garyimglist,namelist

octa,inte = 6,6
ImageList = ['']*octa*inte
path = '/home/spple/lena'
garyimglist, namelist = searchDirFile(path)
for key in range(len(namelist)):
    subname = namelist[key].split('_')
    octaves = int(subname[2])
    intervals = int(subname[4])
    if (octaves == 0):
        continue
    ImageList[(octaves-1)*inte+intervals] = cv2.imread(garyimglist[key])
vis = np.zeros((ImageList[0].shape[:2][0]*inte,ImageList[0*inte].shape[:2][1]+ImageList[1*inte].shape[:2][1]+
                                            ImageList[2*inte].shape[:2][1]+ImageList[3*inte].shape[:2][1]+
                                            ImageList[4*inte].shape[:2][1]+ImageList[5*inte].shape[:2][1]), np.uint8)
shapelist = 0
for cols in range(octa):
    for rows in range(inte):
        h,w = ImageList[cols*inte+rows].shape[:2]
        image = ImageList[cols*inte+rows][:,:,0]
        vis[h*rows:h*(rows+1), shapelist:w+shapelist] = image
    shapelist += w
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
cv2.imwrite("./all_1.jpg", vis)

 

DOG:不添加-1层:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值