不使用from skimage.feature import greycomatrix, greycoprops,用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2’,自然图像的文件路径是‘D:\zzz\zna2’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码

时间: 2023-11-27 22:51:45 浏览: 32
这个问题比较复杂,需要用到多种图像处理和分析的方法。以下是一个可能的解决方案,包含了颜色、纹理、形状和差异性分析的方法。 首先,我们需要导入必要的库: ```python import numpy as np import cv2 import os from scipy.stats import entropy from sklearn.cluster import KMeans from skimage.feature import greycomatrix, greycoprops from skimage.measure import label, regionprops ``` 然后读取图像,计算颜色特征。这里我们使用K-means聚类算法将图像的像素点分成几个颜色组,然后计算每个组的比例和熵。 ```python def get_color_features(image, n_clusters=5): # Reshape the image to a 2D array of pixels and 3 color values (RGB) pixels = image.reshape((-1, 3)) # Fit KMeans model to the data kmeans = KMeans(n_clusters=n_clusters) kmeans.fit(pixels) # Get the color proportions for each cluster _, counts = np.unique(kmeans.labels_, return_counts=True) proportions = counts / np.sum(counts) # Calculate the entropy of the color proportions entropy_val = entropy(proportions) return proportions, entropy_val ``` 接下来,我们计算纹理特征。这里我们使用灰度共生矩阵(GLCM)来描述图像的纹理。GLCM是一个二维矩阵,用于描述图像中灰度级相邻像素对的位置和出现频率。我们使用skimage库的greycomatrix和greycoprops函数来计算GLCM特征。 ```python def get_texture_features(image, distances=[1], angles=[0, np.pi/4, np.pi/2, 3*np.pi/4], properties=['contrast', 'homogeneity']): # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Compute the GLCM matrix for each distance and angle combination glcms = [greycomatrix(gray, distance, angle, symmetric=True, normed=True) for distance in distances for angle in angles] # Compute the requested GLCM properties for each matrix features = np.ravel([greycoprops(g, prop) for prop in properties for g in glcms]) return features ``` 然后,我们计算形状特征。这里我们使用区域分割算法将图像中的每个物体分离出来,然后计算每个物体的面积、周长、长宽比等特征。 ```python def get_shape_features(image, threshold=128): # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Threshold the image to create a binary mask mask = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY)[1] # Label the connected components in the mask labels = label(mask) # Extract the region properties for each labeled region props = regionprops(labels) # Compute the requested shape properties for each region areas = [p.area for p in props] perimeters = [p.perimeter for p in props] eccentricities = [p.eccentricity for p in props] solidity = [p.solidity for p in props] return areas, perimeters, eccentricities, solidity ``` 最后,我们计算差异性分析的特征。这里我们比较两张图像的直方图,然后计算它们之间的交叉熵。 ```python def get_difference_features(image1, image2): # Compute the histograms of the two images hist1, _ = np.histogram(image1.ravel(), bins=256, range=(0, 255)) hist2, _ = np.histogram(image2.ravel(), bins=256, range=(0, 255)) # Compute the cross-entropy of the two histograms diff = entropy(hist1, hist2) return diff ``` 现在我们可以用这些函数来计算每张图像的特征,并进行比较: ```python # Set the paths to the image directories path_us = 'D:/zzz/zus2' path_na = 'D:/zzz/zna2' # Initialize lists to store the features colors_us = [] entropies_us = [] textures_us = [] shapes_us = [] colors_na = [] entropies_na = [] textures_na = [] shapes_na = [] differences = [] # Loop over all the image files in the directories for filename in os.listdir(path_us): if filename.endswith('.jpg'): # Load the image image_us = cv2.imread(os.path.join(path_us, filename)) image_na = cv2.imread(os.path.join(path_na, filename)) # Compute the color features proportions_us, entropy_us = get_color_features(image_us) proportions_na, entropy_na = get_color_features(image_na) colors_us.append(proportions_us) entropies_us.append(entropy_us) colors_na.append(proportions_na) entropies_na.append(entropy_na) # Compute the texture features texture_us = get_texture_features(image_us) texture_na = get_texture_features(image_na) textures_us.append(texture_us) textures_na.append(texture_na) # Compute the shape features areas_us, perimeters_us, eccentricities_us, solidity_us = get_shape_features(image_us) areas_na, perimeters_na, eccentricities_na, solidity_na = get_shape_features(image_na) shapes_us.append((areas_us, perimeters_us, eccentricities_us, solidity_us)) shapes_na.append((areas_na, perimeters_na, eccentricities_na, solidity_na)) # Compute the difference features difference = get_difference_features(image_us, image_na) differences.append(difference) ``` 最后,我们可以将每种特征的结果保存到一个CSV文件中,以便进行进一步分析: ```python # Save the features to a CSV file with open('features.csv', 'w') as f: # Write the header row f.write('filename,') f.write('color_entropy_us,') f.write('color_entropy_na,') for i in range(n_clusters): f.write(f'color_us_{i},') f.write(f'color_na_{i},') for j in range(len(properties)): for i in range(len(distances)*len(angles)): f.write(f'texture_us_{properties[j]}_{i},') f.write(f'texture_na_{properties[j]}_{i},') f.write('area_us,') f.write('perimeter_us,') f.write('eccentricity_us,') f.write('solidity_us,') f.write('area_na,') f.write('perimeter_na,') f.write('eccentricity_na,') f.write('solidity_na,') f.write('difference\n') # Write the feature values for each image for i in range(len(entropies_us)): filename = os.listdir(path_us)[i] f.write(f'{filename},') f.write(f'{entropies_us[i]},') f.write(f'{entropies_na[i]},') for j in range(n_clusters): f.write(f'{colors_us[i][j]},') f.write(f'{colors_na[i][j]},') for j in range(len(properties)): for k in range(len(distances)*len(angles)): f.write(f'{textures_us[i][j*len(distances)*len(angles)+k]},') f.write(f'{textures_na[i][j*len(distances)*len(angles)+k]},') f.write(f'{shapes_us[i][0][0]},') f.write(f'{shapes_us[i][1][0]},') f.write(f'{shapes_us[i][2][0]},') f.write(f'{shapes_us[i][3][0]},') f.write(f'{shapes_na[i][0][0]},') f.write(f'{shapes_na[i][1][0]},') f.write(f'{shapes_na[i][2][0]},') f.write(f'{shapes_na[i][3][0]},') f.write(f'{differences[i]}\n') ```

相关推荐

最新推荐

recommend-type

【车牌识别】 GUI BP神经网络车牌识别(带语音播报)【含Matlab源码 668期】.zip

Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描视频QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
recommend-type

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4
recommend-type

3文件需求申请单.xls

3文件需求申请单.xls
recommend-type

【脑肿瘤检测】 GUI SOM脑肿瘤检测【含Matlab源码 2322期】.zip

【脑肿瘤检测】 GUI SOM脑肿瘤检测【含Matlab源码 2322期】
recommend-type

GOGO语言基础教程、实战案例和实战项目讲解

GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解
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

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

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