1. 程式人生 > >顯著性檢測——PFT模型

顯著性檢測——PFT模型

1. 參考文獻

Guo C, Ma Q, Zhang L. Spatio-temporal Saliency detection using phase spectrum of quaternion fourier transform[J]. 2008:1-8.

2. 模型實現

2.1 顯著性檢測公共標頭檔案

#ifndef SALIENTCOMMON_H
#define SALIENTCOMMON_H
// std lib
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

// opencv lib
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

#endif // SALIENTCOMMON_H

2.2 顯著性檢測標頭檔案

#ifndef SALIENTPFT_H
#define SALIENTPFT_H
#include "salientcommon.h"
// Guo C, Ma Q, Zhang L. Spatio-temporal Saliency detection using phase spectrum of quaternion fourier transform[J]. 2008:1-8.

class salientPFT
{
public:
    salientPFT(Mat& orgImg);
    void m_getSalientPFT(Mat& result);
    ~salientPFT();
private:
    Mat m_OrgImg;
    Mat m_resultImg;
};

#endif // SALIENTPFT_H

2.3 顯著性檢測實現

#include "salientpft.h"

salientPFT::salientPFT(Mat& orgImg)
{
    orgImg.copyTo(m_OrgImg);
}

salientPFT::~salientPFT()
{
    ;
}

void salientPFT::m_getSalientPFT(Mat& result)
{
    Mat tmpOrgImg;
    if(m_OrgImg.channels() == 3)
    {
        cvtColor(m_OrgImg, tmpOrgImg, CV_RGB2GRAY);
    }
    else
    {
        m_OrgImg.copyTo(tmpOrgImg);
    }

    // Get FFT2 Factorys
    int M = getOptimalDFTSize(tmpOrgImg.rows);
    int N = getOptimalDFTSize(tmpOrgImg.cols);

    // padding Image
    Mat tmpPaddingImg;
    copyMakeBorder(tmpOrgImg, tmpPaddingImg, 0, M - tmpOrgImg.rows, 0, N - tmpOrgImg.cols, BORDER_CONSTANT, Scalar(0));
    // FFT2 Translate
    Mat planes[2];
    tmpPaddingImg.convertTo(tmpPaddingImg, CV_32F);
    planes[0] = tmpPaddingImg;
    planes[1] = Mat::zeros(tmpPaddingImg.size(), CV_32F);

    Mat tmpComplexImg;
    merge(planes, 2,  tmpComplexImg);
    dft(tmpComplexImg, tmpComplexImg);

    split(tmpComplexImg, planes);
    // compute phase
    Mat tmpPhaseImg;
    phase(planes[0], planes[1], tmpPhaseImg);
    // compute log abs Magnitude
    Mat tmpMagImg;
    magnitude(planes[0], planes[1], tmpMagImg);
    // compute sin & cosion
    Mat tmpSin, tmpCosion;
    cv::divide(planes[0], tmpMagImg, tmpCosion); // Re part
    cv::divide(planes[1], tmpMagImg, tmpSin);    // Im Part

    planes[0] = tmpCosion;
    planes[1] = tmpSin;

    merge(planes, 2, tmpComplexImg);

    Mat tmpSalientImg;
    dft(tmpComplexImg, tmpSalientImg, DFT_INVERSE);

    // Normalize
    split(tmpSalientImg, planes);
    magnitude(planes[0], planes[1], tmpMagImg);
    GaussianBlur(tmpMagImg, tmpMagImg, Size(7,7), 3);
    double minVal = 0, maxVal = 0;
    minMaxLoc(tmpMagImg, &minVal, &maxVal);
    double scale = 255./(maxVal - minVal);
    double shift = -minVal * scale;
    convertScaleAbs(tmpMagImg(Rect(0,0,m_OrgImg.cols, m_OrgImg.rows)), m_resultImg, scale, shift);
    m_resultImg.copyTo(result);
}

3. 模型效果