skimage下的灰度共生矩阵计算源码

时间: 2023-11-27 11:55:40 浏览: 24
在scikit-image中,计算灰度共生矩阵的函数为`skimage.feature.greycomatrix`。以下是其代码实现: ```python import numpy as np from skimage.util.dtype import dtype_range from skimage.util import img_as_ubyte def greycomatrix(image, distances, angles, levels=None, symmetric=False, normed=False): if levels is None: if np.issubdtype(image.dtype, np.integer): # For integer images, the number of levels is determined by the # range of values. imin, imax = dtype_range[image.dtype.type] levels = np.arange(imin, imax + 1) else: # For floating point images, we use 256 levels by default. levels = np.linspace(0, 1, 256, endpoint=False) # Convert the image to uint8, so that we can use it as an index into the # levels array. image = img_as_ubyte(image) glcm = np.zeros((len(levels), len(levels), len(distances), len(angles)), dtype=np.double) for d, distance in enumerate(distances): for a, angle in enumerate(angles): coords1, coords2 = _offsets_to_coords(image.shape, distance, angle) patch1 = image[coords1[:, 0], coords1[:, 1]] patch2 = image[coords2[:, 0], coords2[:, 1]] # Find the indices into the levels array for each pixel in the # patch. This allows us to construct a joint histogram of levels. index1 = np.searchsorted(levels, patch1) index2 = np.searchsorted(levels, patch2) # Clip the indices to ensure that they are within the range [0, # len(levels)-1]. index1 = np.clip(index1, 0, len(levels) - 1) index2 = np.clip(index2, 0, len(levels) - 1) # Update the co-occurrence matrix by counting the number of times # each combination of levels occurs. for i, j in zip(index1, index2): glcm[i, j, d, a] += 1 if symmetric: # Make the matrix symmetric by mirroring over the diagonal. glcm = glcm + np.transpose(glcm, (1, 0, 2, 3)) if normed: glcm /= np.sum(glcm) return glcm ``` 该函数接受以下参数: - `image`:输入的灰度图像。 - `distances`:灰度共生矩阵中用于计算概率的像素对之间的距离列表。例如,`[1, 2, 3]`表示将在距离为1、2和3的像素对之间计算概率。 - `angles`:灰度共生矩阵中用于计算概率的像素对之间的角度列表。例如,`[0, np.pi/4, np.pi/2, 3*np.pi/4]`表示将在水平、45度、垂直和135度方向上计算概率。 - `levels`:灰度级别列表。默认情况下,对于整数图像,该列表将由图像数据类型的范围确定(例如在[0, 255]范围内),而对于浮点图像,该列表将由256个等间距的值组成。您也可以指定自定义级别列表。 - `symmetric`:如果为True,则将灰度共生矩阵设置为对称矩阵。这在某些应用中很有用,例如,当您需要比较两个灰度图像时。 - `normed`:如果为True,则将灰度共生矩阵归一化为概率矩阵。 函数返回一个4D数组,其中每个元素都是一个灰度共生矩阵,表示给定距离和角度的像素对之间的概率。数组的第一个和第二个维度表示像素级别,第三个维度表示距离,第四个维度表示角度。

相关推荐

最新推荐

recommend-type

灰度共生矩阵讲解matlab

灰度共生矩阵介绍,还有matlab函数,个人觉得讲得还是短而精悍的,推荐推荐
recommend-type

基于灰度共生矩阵的图像分割方法研究

文中主要以某山地航拍图为研究对像,对其进行背景...由于该山地背景纹理特征明显,故利用纹理分析对其进行背景分析,而灰度共生矩阵是纹理分析方法中最常用的一种方法。文中采用灰度共生矩阵方法对该图像进行分割研究。
recommend-type

z-blog模板网站导航网站源码 带后台管理.rar

z-blog模板网站导航网站源码 带后台管理.rarz-blog模板网站导航网站源码 带后台管理.rar
recommend-type

基于TI的MSP430单片机的无叶风扇控制器+全部资料+详细文档(高分项目).zip

【资源说明】 基于TI的MSP430单片机的无叶风扇控制器+全部资料+详细文档(高分项目).zip基于TI的MSP430单片机的无叶风扇控制器+全部资料+详细文档(高分项目).zip基于TI的MSP430单片机的无叶风扇控制器+全部资料+详细文档(高分项目).zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

1124905257887411C++图书管理系统.zip

1124905257887411C++图书管理系统.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。