提取图像灰度共生矩阵特征

 

头文件:

#ifndef GLMC_H
#define GLMC_H
#include"Datastruct.h"
using namespace std;
using namespace cv;

class GLCM
{
public:
    GLCM();
    ~GLCM();
    enum
    {
        GLCM_HORIZATION=0,
        GLCM_VERTICAL=1,
        GLCM_ANGLE45=2,
        GLCM_ANGLE135=3
    };
    //计算灰度共生矩阵;
    void calGLCM(IplImage* intputImg,Vec2D& vecGLCM,int angle);
    //计算特征值;
    void getGLCMFeatures(Vec2D& vecGLCM,GLCMFeatures &features);
    //初始化共生矩阵;
    void initGLCM(Vec2D& vecGLCM,int size=16);
    //设置灰度划分等级;
    void setGrayLevel(int grayLevel){m_grayLevel=grayLevel;}
    //void SaveGLCMimg(std::vector<std::vector<double> > &temp,std::string &RoiFile,std::string &RootPath_glcm);
    int getGrayLevel()const
    {
        return m_grayLevel;
    }
private:
    //水平灰度共生矩阵
    void getHorisonGLCM(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight);
    //垂直灰度共生矩阵
    void getVerticalGLCM(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight);
    //45°灰度共生矩阵
    void getGLCM45(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight);
    //135°灰度共生矩阵
    void getGLCM135(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight);
    int m_grayLevel;

};
#endif

cpp文件:

#include "StdAfx.h"
#include"GLMC.h"


int savenum=0;

GLCM::GLCM():m_grayLevel(16)
{}
GLCM::~GLCM()
{}
void GLCM::initGLCM(Vec2D& vecGLCM,int size)
{
    assert(size==m_grayLevel);
    vecGLCM.resize(size);
    for(int i=0;i<size;++i)
        vecGLCM[i].resize(size);
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            vecGLCM[i][j]=0;
        }
    }
}
void GLCM::getHorisonGLCM(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight)
{
    int height=imgHeight;
    int width=imgWidth;
    for(int i=0;i<height;i++)
    {
        for(int j=0;j<width-1;j++)
        {
            int pixel_rows=src[i][j];
            int pixel_cols=src[i][j+1];
            dst[pixel_rows][pixel_cols]++;
        }
    }
}
void GLCM::getVerticalGLCM(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight)
{
    int height=imgHeight;
    int width=imgWidth;
    for(int i=0;i<height-1;i++)
    {
        for(int j=0;j<width;j++)
        {
            int pixel_rows=src[i][j];
            int pixel_cols=src[i+1][j];
            dst[pixel_rows][pixel_cols]++;
        }
    }
}
void GLCM::getGLCM45(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight)
{
    int height=imgHeight;
    int width=imgWidth;
    for(int i=0;i<height-1;i++)
    {
        for(int j=0;j<width-1;j++)
        {
            int pixel_rows=src[i][j];
            int pixel_cols=src[i+1][j+1];
            dst[pixel_rows][pixel_cols]++;
        }
    }
}
void GLCM::getGLCM135(Vec2D &src,Vec2D &dst,int imgWidth,int imgHeight)
{

    int height=imgHeight;
    int width=imgWidth;
    for(int i=0;i<height-1;++i)
    {
        for(int j=1;j<width;++j)
        {
            int pixel_rows=src[i][j];
            int pixel_cols=src[i+1][j-1];
            dst[pixel_rows][pixel_cols]++;
        }
    }

void GLCM::calGLCM(IplImage* intputImg,Vec2D& vecGLCM,int angle)
{
    assert(intputImg->nChannels==1);
    IplImage* src=NULL;
    src=cvCreateImage(cvGetSize(intputImg),IPL_DEPTH_32S,intputImg->nChannels);
    cvConvert(intputImg,src);
    int height=src->height;
    int width=src->width;
    int maxGrayLevel=0;
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<width;++j)
        {
            int grayVal=cvGetReal2D(src,i,j);
            if(grayVal>maxGrayLevel)
                maxGrayLevel=grayVal;
        }
    }
    ++maxGrayLevel;
    Vec2D tempVec;
    tempVec.resize(height);
    for(int i=0;i<height;i++)
    {
        tempVec[i].resize(width);
    }
    if(maxGrayLevel>16)
    {
        for(int i=0;i<height;i++)
        {
            for(int j=0;j<width;j++)
            {
                int tempVal=cvGetReal2D(src,i,j);
                tempVal/=m_grayLevel;
                tempVec[i][j]=tempVal;
            }
        }
        if(angle==GLCM_HORIZATION)
            getHorisonGLCM(tempVec,vecGLCM,width,height);
        if(angle==GLCM_VERTICAL)
            getVerticalGLCM(tempVec,vecGLCM,width,height);
        if(angle==GLCM_ANGLE45)
            getGLCM45(tempVec,vecGLCM,width,height);
        if(angle==GLCM_ANGLE135)
            getGLCM135(tempVec,vecGLCM,width,height);
    }
    else
    {
        for(int i=0;i<height;++i)
        {
            for(int j=0;j<width;++j)
            {
                int tempVal=cvGetReal2D(src,i,j);
                tempVec[i][j]=tempVal;
            }
        }
        if(angle==GLCM_HORIZATION)
            getHorisonGLCM(tempVec,vecGLCM,width,height);
        if(angle==GLCM_VERTICAL)
            getVerticalGLCM(tempVec,vecGLCM,width,height);
        if(angle==GLCM_ANGLE45)
            getGLCM45(tempVec,vecGLCM,width,height);
        if(angle==GLCM_ANGLE135)
            getGLCM135(tempVec,vecGLCM,width,height);
    }
    cvReleaseImage(&src);
}

void GLCM::getGLCMFeatures(Vec2D& vecGLCM,GLCMFeatures &features)
{
     int total=0;
    for(int i=0;i<m_grayLevel;++i)
    {
        for(int j=0;j<m_grayLevel;++j)
        {
            total+=vecGLCM[i][j];
        }
    }
    vector<vector<double> >temp;
    temp.resize(m_grayLevel);
    for(int i=0;i<m_grayLevel;++i)
        temp[i].resize(m_grayLevel);
    //归一化;
    for(int i=0;i<m_grayLevel;++i)
    {
        //std::cout<<std::endl;
        for(int j=0;j<m_grayLevel;++j)
        {
            
            temp[i][j]=(double)vecGLCM[i][j]/(double)total;
            //std::cout<<temp[i][j]<<" ";
        }
    }
    for(int i=0;i<m_grayLevel;++i)
    {
        for(int j=0;j<m_grayLevel;++j)
        {
            features.energy+=temp[i][j]*temp[i][j];
            if(temp[i][j]>0)
                features.entropy-=temp[i][j]*log(temp[i][j]);
            features.contrast+=(double)(i-j)*(double)(i-j)*temp[i][j];
            features.idmoment+=temp[i][j]/(1+(double)(i-j)*(double)(i-j));
        }
    }
}

/*void SaveGLCMimg(std::vector<std::vector<double> > &temp,std::string &RoiFile,std::string &RootPath_glcm)
{
    cv::Mat saveImg=Mat::zeros(16,16,CV_32S);
    savenum++;
    std::string s=RootPath_glcm;
    std::string save=RoiFile;
    s.replace(0,32,s);
    const char* SavePath=NULL;
    for(int i=0;i<16;++i)
    {
        for(int j=0;j<16;++j)
        {
            int value=0;
            value=(int)temp[i][j]*255;
            value=value>255?255:value;
            if(value<0)
                value=0;
            saveImg.at<int>(i,j)=value;
        }
    }
    SavePath=s.c_str();
    cv::imwrite(SavePath,saveImg);
}*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值