基于从Gabor

时间:2018-03-11 08:46:24

标签: opencv textures k-means pca gabor-filter

我正在尝试根据我创建的Gabor银行提取的功能对纹理进行聚类,但结果与通常预期的结果相差很远,所以这就是我正在做的>>

1 - 生成一个滤波器组(基于Miki的答案here我得到了实部和虚部,以便我以后可以提取幅度特征)

void Gabor::generateFilterbank(int bankRows,int bankCols)
{
    bankCol=bankCols;
    bankRow=bankRows;
    setBankSize();

    int thetaStep=pos_th_max/bankCols;
    int lambadaStep=(pos_lm_max/bankRows) /10;

    for (int i = 1; i <= bankRows; i++)    // cols = theta  rows = lambda
        for (int j = 1; j <= bankCols; j++ )
        {
            int ks=(kernel_size-1)/2;
            Size KernalSize(ks,ks);
            double Sigma = pos_sigma;
            double Lambda = 0.5+(i*lambadaStep);
            double Theta = (j*thetaStep)*CV_PI/180;
            double psi = pos_psi;//*CV_PI/180
            double Gamma = 0;
            vector<Mat> realImag;
            realImag.push_back(getGaborKernel(KernalSize,Sigma,Theta,Lambda,Gamma,psi,CV_32F));//real
            realImag.push_back(getGaborKernelImag(KernalSize,Sigma,Theta,Lambda,Gamma,psi,CV_32F));//imag
            Kernels.push_back(realImag);
         }

}

2处理输入图像

void Gabor::process(Mat img)
{
    img.convertTo(img, CV_32F, 1.0/255, 0);
    for (int i=0;(unsigned)i<Kernels.size();i++)
    {
        vector<Mat > responseIR;
        for (int j=0;(unsigned)j<Kernels[i].size();j++)
        {
            Mat IR ;
            filter2D(img, IR , CV_32F,Kernels[i][j]);
            responseIR.push_back(IR );
            IR.release();
        }
        responses.push_back(responseIR);
    }
}

3-从所有Gabor响应中提取特征向量

    vector<float> Gabor::getGaborFetures(ofstream &out,float name)
    {
        vector<float>fetures;
        Mat magnituder (responses.size(),responses[0][0].rows*responses[0][0].cols,CV_32FC1, Scalar(0.0));
        fetures.push_back(name);
        if (responses.size()>0)
        {
            for (int i=0;(unsigned)i<responses.size();i+=2)
            {
                vector<float> mag,rel,imag;

                mat2Vector(responses[i][0],rel);
                mat2Vector( responses[i][1],imag);
                cv::magnitude(rel,imag, mag);

                for (int j=0;j<mag.size();j++)
                {
                  magnituder.at<float>(i,j)  =mag[j];
                }
            }

            PCA pca( magnituder,Mat(),CV_PCA_DATA_AS_ROW,10);
            Mat result(pca.project( magnituder));
            magnituder.release();
            for (int i=0;i<result.rows;i++)
            {
                for(int j=0;j<result.cols;j++)
                {
                    fetures.push_back(result.at<float>(i,j));

                }
            }
            result.release();
            for (int i=0;(unsigned)i<fetures.size();i++)
                out <<fetures[i]<<",";
            out<<endl;
            return fetures;
        }

    else
    {
        cerr<<"You have to process your image first!"<<endl;
        return fetures;
    }
}

4-最终使用K-means

聚类提取的特征
int kt=14;
Mat labTexture;
kmeans(ftTexture,kt,labTexture,TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000,0.0001),10,KMEANS_RANDOM_CENTERS );

目前,我正在测试this dataset上的代码我希望将数据集聚为14类,例如,所有砖块图像都会转到一个文件夹。

0 个答案:

没有答案