灰度共生矩阵(GLCM,Gray-Level Co-occurrence Matrix)

版权声明:涉猎过的知识都像是不断汇入大海的涓涓细流,你怎么知道是哪条汇入的溪流让海洋成为海洋呢【转载请注明出处】 https://blog.csdn.net/panda1234lee/article/details/71405880


概念

由于纹理是由灰度分布在空间位置上反复出现而形成的,因而在图像空间中相隔某距离的两像素之间会存在一定的灰度关系,即图像中灰度的空间相关特性。灰度共生矩阵就是一种通过研究灰度的空间相关特性来描述纹理的常用方法。

灰度共生矩阵是涉及像素距离和角度的矩阵函数,它通过计算图像中一定距离和一定方向的两点灰度之间的相关性,来反映图像在方向、间隔、变化幅度及快慢上的综合信息。

灰度直方图是对图像上单个像素具有某个灰度进行统计的结果,而灰度共生矩阵是对图像上保持某距离的两像素分别具有某灰度的状况进行统计得到的。


GLCM 所代表的含义

灰度共生矩阵元素所表示的含义,以(1,1)点为例,GLCM(1,1)值为1说明左侧原图只有一对灰度为1的像素水平相邻。GLCM(1,2)值为2,是因为原图有两对灰度为1和2的像素水平相邻。






灰度共生矩阵的特征


1) 角二阶矩(Angular Second Moment, ASM)
公式:ASM = sum(p(i,j).^2),其中  p(i,j) 表示归一后的灰度共生矩阵
意义:角二阶矩是图像灰度分布均匀程度和纹理粗细的一个度量,当图像纹理绞细致、灰度分布均匀时,能量值较大,反之,较小。

2) 熵(Entropy, ENT)
公式:ENT=sum(p(i,j)*(-log(p(i,j)))
意义:描述图像具有的信息量的度量,表明图像的复杂程度,当复杂程度高时,熵值较大,反之则较小。

3) 反差分矩阵(Inverse Differential Moment, IDM)
公式:IDM=sum(p(i,j)/(1+(i-j)^2))
意义:反映了纹理的清晰程度和规则程度,纹理清晰、规律性较强、易于描述的,值较大;杂乱无章的,难于描述的,值较小。


OpenCV 代码

// 0°灰度共生矩阵
void getGLCM0(Mat& src, Mat& dst, int gray_level)// 0度灰度共生矩阵
{
	CV_Assert(1 == src.channels());

	int height = src.rows;
	int width = src.cols;

	dst.create(gray_level, gray_level, CV_32SC1);
	dst = Scalar::all(0);

	for (int i = 0; i < height; i++)
	{
		int*srcdata = src.ptr<int>(i);
		for (int j = 0; j < width - 1; j++)
		{
			// 同样的像素对,水平相邻
			int rows = srcdata[j];
			int cols = srcdata[j + 1];
			dst.ptr<int>(rows)[cols]++;
		}
	}

}

// 90°灰度共生矩阵
void getGLCM90(Mat& src, Mat& dst, int gray_level)
{
	CV_Assert(1 == src.channels());

	int height = src.rows;
	int width = src.cols;

	dst = Mat(gray_level, gray_level, CV_32SC1, Scalar(0));

	for (int i = 0; i < height - 1; i++)
	{
		int*srcdata = src.ptr<int>(i);
		int*srcdata1 = src.ptr<int>(i + 1);
		for (int j = 0; j < width; j++)
		{
			// 同样的像素对,垂直相邻
			int rows = srcdata[j];
			int cols = srcdata1[j];
			dst.ptr<int>(rows)[cols]++;
		}
	}
}

// 45°灰度共生矩阵
void getGLCM45(Mat& src, Mat& dst, int gray_level)
{
	CV_Assert(1 == src.channels());

	int height = src.rows;
	int width = src.cols;

	dst = Mat(gray_level, gray_level, CV_32SC1, Scalar(0));

	for (int i = 0; i < height - 1; i++)
	{
		int*srcdata = src.ptr<int>(i);
		int*srcdata1 = src.ptr<int>(i + 1);
		for (int j = 0; j < width - 1; j++)
		{
			// 同样的像素对,45°相邻
			int rows = srcdata[j];
			int cols = srcdata1[j + 1];
			dst.ptr<int>(rows)[cols]++;
		}
	}
}

// 135°灰度共生矩阵
void getGLCM135(Mat& src, Mat& dst, int gray_level)
{
	CV_Assert(1 == src.channels());

	int height = src.rows;
	int width = src.cols;

	dst = Mat(gray_level, gray_level, CV_32SC1, Scalar(0));

	for (int i = 0; i < height - 1; i++)
	{
		int*srcdata = src.ptr<int>(i);
		int*srcdata1 = src.ptr<int>(i + 1);
		for (int j = 1; j < width; j++)
		{
			// 同样的像素对,135°相邻
			int rows = srcdata[j];
			int cols = srcdata1[j - 1];
			dst.ptr<int>(rows)[cols]++;
		}
	}

}

// 计算特征值
void featureGLCM(Mat&src, double& Asm, double& Ent, double& Con, double& Idm)
{
	
	CV_Assert(src.channels() == 1);
	
	int height = src.rows;
	int width = src.cols;
	int total = 0;

	//求图像所有像素的灰度值的和
	for (int i = 0; i < height; i++)
	{
		int*srcdata = src.ptr<int>(i);
		for (int j = 0; j < width; j++)
		{
			total += srcdata[j];
		}
	}

	//图像每一个像素的的值除以像素总和
	Mat mean;
	mean.create(height, width, CV_64FC1);
	for (int i = 0; i < height; i++)
	{
		int*srcdata = src.ptr<int>(i);
		double*copydata = mean.ptr<double>(i);
		for (int j = 0; j < width; j++)
		{
			copydata[j] = (double)srcdata[j] / (double)total;
		}
	}

	for (int i = 0; i < height; i++)
	{
		double*srcdata = mean.ptr<double>(i);
		for (int j = 0; j < width; j++)
		{
			// 能量
			Asm += srcdata[j] * srcdata[j];

			// 熵(Entropy) 
			if (srcdata[j]>0)
				Ent -= srcdata[j] * log(srcdata[j]);

			// 对比度
			Con += (double)(i - j)*(double)(i - j)*srcdata[j];

			// 逆差矩
			Idm += srcdata[j] / (1 + (double)(i - j)*(double)(i - j));
		}
	}
}


注:本代码以 offset = 1 为例


参考自:

http://baike.baidu.com/link?url=3cuLq2FM6QHWNxgMaswYbKBacMCQ1ptYU6H-T1c8YpcFxmnMEuCtYC9hvCAScZcnxEQDl9a8TXrNntgkoEFliL5tuWapiC3Ovf_vabsEFa1_CouZwLTUByMUhUqLaExrt1KAdEr9ralX-LQKLCj_qq

http://blog.csdn.net/weiyuweizhi/article/details/5724050

http://blog.csdn.net/yanxiaopan/article/details/52356777

http://blog.csdn.net/u014488388/article/details/52877710





猜你喜欢

转载自blog.csdn.net/panda1234lee/article/details/71405880
今日推荐