显著性检测——LC模型

1. 参考文献:

Visual Attention Detection in Video Sequences Using Spatiotemporal Cues。 Yun Zhai and Mubarak Shah.  Page 4-5


2. 模型实现

2.1 显著性检测公共头文件

#ifndef SALIENTCOMMON_H
#define SALIENTCOMMON_H
// std lib
#include 
#include 
#include 
#include 

// opencv lib
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

#endif // SALIENTCOMMON_H

2.2 显著性检测头文件

#ifndef SALIENTLC_H
#define SALIENTLC_H
#include "salientcommon.h"
// 参考论文:Visual Attention Detection in Video Sequences Using Spatiotemporal Cues。 Yun Zhai and Mubarak Shah.  Page 4-5
//
class salientLC
{
public:
    salientLC(Mat orgImg);
    ~salientLC();
    void m_getSalientLC(Mat& resultImg);
private:
    void m_getHistgram();
    void m_Normalize(Mat& distMap);
    void m_rgb2Lab();
    void m_getDistTable();
private:
    Mat m_orgImg;
    Mat m_labImg;
    unsigned int m_maxDist;
    unsigned int m_minDist;
    unsigned int m_hist[256];
    unsigned int m_distTable[256];
    Mat m_resultImg;
};

#endif // SALIENTLC_H

2.3 显著性检测实现文件(.cpp)

#include "salientlc.h"

salientLC::salientLC(Mat orgImg)
{
    orgImg.copyTo(m_orgImg);
    m_minDist = (unsigned int)0xEFFFFFFF;
    m_maxDist = (unsigned int)0x00000000;
    memset(m_hist, 0, sizeof(m_hist));
    memset(m_distTable, 0, sizeof(m_distTable));
    m_rgb2Lab();
    m_getDistTable();
}

salientLC::~salientLC()
{
    ;
}

void salientLC::m_getSalientLC(Mat& resultImg)
{
    int W = m_labImg.cols;
    int H = m_labImg.rows;
    int rowIter, colIter;
    unsigned int histIter;
    int channelIter;
    Mat distMap = Mat::zeros(Size(W,H), CV_32FC1);
    m_getHistgram();
    for(rowIter = 0; rowIter < H; ++rowIter)
    {
        for(colIter = 0; colIter < W; ++colIter)
        {
            unsigned int tmpVal = 0;
            for(channelIter = 0; channelIter < 3; ++channelIter)
            {
                unsigned int  value = m_labImg.at(rowIter, colIter)[channelIter];
                for(histIter = 0; histIter < 256; ++histIter)
                {
                    int absVal = histIter > value ? histIter - value : value - histIter;
                    tmpVal += m_hist[histIter] * m_distTable[absVal];
                }
            }
            distMap.at(rowIter, colIter)  =  tmpVal;
            if(distMap.at(rowIter, colIter) < m_minDist)
            {
                m_minDist = distMap.at(rowIter, colIter);
            }

            if(distMap.at(rowIter, colIter) > m_maxDist)
            {
                m_maxDist = distMap.at(rowIter, colIter);
            }
        }
    }
    cout << "minDist = " << m_minDist << ", " << "maxDist = " << m_maxDist << endl;
    m_Normalize(distMap);
    m_resultImg.copyTo(resultImg);
}

void salientLC::m_getHistgram()
{
    int W = m_labImg.cols;
    int H = m_labImg.rows;
    int rowIter, colIter;
    for(rowIter = 0; rowIter < H; ++rowIter)
    {
        for(colIter = 0; colIter < W; ++colIter)
        {
            m_hist[m_labImg.at(rowIter, colIter)]++;
        }
    }
}

void salientLC::m_getDistTable()
{
    int iter;
    for(iter = 0; iter < 256; ++iter)
    {
        m_distTable[iter] = iter * iter;
    }
}

void salientLC::m_Normalize(Mat& distMap)
{
    int W = distMap.cols;
    int H = distMap.rows;
    int rowIter, colIter;
    m_resultImg = Mat::zeros(Size(W,H), CV_8UC1);
    for(rowIter = 0; rowIter < H; ++rowIter)
    {
        for(colIter = 0; colIter < W; ++colIter)
        {
            m_resultImg.at(rowIter,colIter) = (int)((distMap.at(rowIter, colIter)  - m_minDist)* 1.0 / (m_maxDist - m_minDist) * 255);
        }
    }
}

void salientLC::m_rgb2Lab()
{
    Mat tmpImg;
    if(m_orgImg.channels() == 1)
    {
        cvtColor(m_orgImg, tmpImg, CV_GRAY2BGR);
    }
    else
    {
        m_orgImg.copyTo(tmpImg);
    }
    cvtColor(tmpImg, m_labImg, CV_BGR2Lab);
}

3. 实现效果:


你可能感兴趣的:(opencv函数学习,图像处理,算法实现)