用python实现基于概率分布的超声图像与自然图像性质差异分析,超声图像的文件路径是D:\zzz\na5,自然图像的文件路径是D:\zzz\us5,两种图像都有778张,请注意每张图像的长和宽不都一样可能需要调整,对比的特征包含纹理和形态,需要可视化使结果尽量明显

时间: 2024-01-24 11:17:08 浏览: 31
首先,我们需要导入必要的库:numpy、opencv、skimage、matplotlib等。 ```python import numpy as np import cv2 from skimage import feature, color import matplotlib.pyplot as plt ``` 然后,我们可以编写一个函数来计算每张图像的纹理和形态特征,并将结果可视化。 ```python def analyze_image(img_path): # 读取图像 img = cv2.imread(img_path) # 调整图像大小 img = cv2.resize(img, (256, 256)) # 转换为灰度图像 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算纹理特征 glcm = feature.greycomatrix(img_gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=256, symmetric=True, normed=True) contrast = feature.greycoprops(glcm, 'contrast').mean() correlation = feature.greycoprops(glcm, 'correlation').mean() energy = feature.greycoprops(glcm, 'energy').mean() homogeneity = feature.greycoprops(glcm, 'homogeneity').mean() # 计算形态特征 img_canny = feature.canny(img_gray) img_hough = cv2.HoughLinesP(img_canny, rho=1, theta=np.pi/180, threshold=50, minLineLength=30, maxLineGap=10) num_lines = len(img_hough) line_lengths = np.sqrt(np.sum((img_hough[:,0,:2] - img_hough[:,0,2:]) ** 2, axis=1)) line_angles = np.arctan2(img_hough[:,0,3] - img_hough[:,0,1], img_hough[:,0,2] - img_hough[:,0,0]) line_angles_deg = np.rad2deg(line_angles) line_angles_deg[line_angles_deg < 0] += 180 # 将角度转换到0-180度范围内 avg_line_length = np.mean(line_lengths) max_line_length = np.max(line_lengths) min_line_length = np.min(line_lengths) avg_line_angle = np.mean(line_angles_deg) max_line_angle = np.max(line_angles_deg) min_line_angle = np.min(line_angles_deg) # 可视化结果 fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) ax = axes.ravel() ax[0].imshow(img) ax[0].set_title('Original Image') ax[0].axis('off') ax[1].imshow(img_canny, cmap=plt.cm.gray) ax[1].set_title('Canny Edge Detection') ax[1].axis('off') plt.show() print('Contrast: {:.2f}'.format(contrast)) print('Correlation: {:.2f}'.format(correlation)) print('Energy: {:.2f}'.format(energy)) print('Homogeneity: {:.2f}'.format(homogeneity)) print('Number of lines: {}'.format(num_lines)) print('Average line length: {:.2f}'.format(avg_line_length)) print('Max line length: {:.2f}'.format(max_line_length)) print('Min line length: {:.2f}'.format(min_line_length)) print('Average line angle: {:.2f}'.format(avg_line_angle)) print('Max line angle: {:.2f}'.format(max_line_angle)) print('Min line angle: {:.2f}'.format(min_line_angle)) ``` 接下来,我们可以遍历每张图像,调用上述函数进行特征分析。 ```python # 遍历超声图像并分析特征 for i in range(1, 779): img_path = 'D:/zzz/na5/{}.jpg'.format(i) print('Analyzing image {}'.format(img_path)) analyze_image(img_path) # 遍历自然图像并分析特征 for i in range(1, 779): img_path = 'D:/zzz/us5/{}.jpg'.format(i) print('Analyzing image {}'.format(img_path)) analyze_image(img_path) ``` 最后,我们可以将结果进行比较,以便更好地了解超声图像和自然图像之间的性质差异。 ```python # 比较结果 fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 10)) ax = axes.ravel() ax[0].bar(['Contrast', 'Correlation', 'Energy', 'Homogeneity'], [na_contrast.mean(), na_correlation.mean(), na_energy.mean(), na_homogeneity.mean()], width=0.4, color='b') ax[0].bar(['Contrast', 'Correlation', 'Energy', 'Homogeneity'], [us_contrast.mean(), us_correlation.mean(), us_energy.mean(), us_homogeneity.mean()], width=0.4, color='r') ax[0].set_title('Texture Features') ax[0].legend(['Natural Images', 'Ultrasound Images']) ax[1].bar(['Number of Lines', 'Average Line Length', 'Max Line Length', 'Min Line Length'], [na_num_lines.mean(), na_avg_line_length.mean(), na_max_line_length.mean(), na_min_line_length.mean()], width=0.4, color='b') ax[1].bar(['Number of Lines', 'Average Line Length', 'Max Line Length', 'Min Line Length'], [us_num_lines.mean(), us_avg_line_length.mean(), us_max_line_length.mean(), us_min_line_length.mean()], width=0.4, color='r') ax[1].set_title('Shape Features') ax[1].legend(['Natural Images', 'Ultrasound Images']) ax[2].imshow(na_texture_diff, cmap=plt.cm.RdYlGn) ax[2].set_title('Texture Difference') ax[2].axis('off') ax[3].imshow(na_shape_diff, cmap=plt.cm.RdYlGn) ax[3].set_title('Shape Difference') ax[3].axis('off') plt.show() ``` 完整代码如下: ```python import numpy as np import cv2 from skimage import feature, color import matplotlib.pyplot as plt def analyze_image(img_path): # 读取图像 img = cv2.imread(img_path) # 调整图像大小 img = cv2.resize(img, (256, 256)) # 转换为灰度图像 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算纹理特征 glcm = feature.greycomatrix(img_gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=256, symmetric=True, normed=True) contrast = feature.greycoprops(glcm, 'contrast').mean() correlation = feature.greycoprops(glcm, 'correlation').mean() energy = feature.greycoprops(glcm, 'energy').mean() homogeneity = feature.greycoprops(glcm, 'homogeneity').mean() # 计算形态特征 img_canny = feature.canny(img_gray) img_hough = cv2.HoughLinesP(img_canny, rho=1, theta=np.pi/180, threshold=50, minLineLength=30, maxLineGap=10) num_lines = len(img_hough) line_lengths = np.sqrt(np.sum((img_hough[:,0,:2] - img_hough[:,0,2:]) ** 2, axis=1)) line_angles = np.arctan2(img_hough[:,0,3] - img_hough[:,0,1], img_hough[:,0,2] - img_hough[:,0,0]) line_angles_deg = np.rad2deg(line_angles) line_angles_deg[line_angles_deg < 0] += 180 # 将角度转换到0-180度范围内 avg_line_length = np.mean(line_lengths) max_line_length = np.max(line_lengths) min_line_length = np.min(line_lengths) avg_line_angle = np.mean(line_angles_deg) max_line_angle = np.max(line_angles_deg) min_line_angle = np.min(line_angles_deg) # 可视化结果 fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) ax = axes.ravel() ax[0].imshow(img) ax[0].set_title('Original Image') ax[0].axis('off') ax[1].imshow(img_canny, cmap=plt.cm.gray) ax[1].set_title('Canny Edge Detection') ax[1].axis('off') plt.show() print('Contrast: {:.2f}'.format(contrast)) print('Correlation: {:.2f}'.format(correlation)) print('Energy: {:.2f}'.format(energy)) print('Homogeneity: {:.2f}'.format(homogeneity)) print('Number of lines: {}'.format(num_lines)) print('Average line length: {:.2f}'.format(avg_line_length)) print('Max line length: {:.2f}'.format(max_line_length)) print('Min line length: {:.2f}'.format(min_line_length)) print('Average line angle: {:.2f}'.format(avg_line_angle)) print('Max line angle: {:.2f}'.format(max_line_angle)) print('Min line angle: {:.2f}'.format(min_line_angle)) # 遍历超声图像并分析特征 us_contrast = [] us_correlation = [] us_energy = [] us_homogeneity = [] us_num_lines = [] us_avg_line_length = [] us_max_line_length = [] us_min_line_length = [] for i in range(1, 779): img_path = 'D:/zzz/na5/{}.jpg'.format(i) print('Analyzing image {}'.format(img_path)) contrast, correlation, energy, homogeneity, num_lines, avg_line_length, max_line_length, min_line_length = analyze_image(img_path) us_contrast.append(contrast) us_correlation.append(correlation) us_energy.append(energy) us_homogeneity.append(homogeneity) us_num_lines.append(num_lines) us_avg_line_length.append(avg_line_length) us_max_line_length.append(max_line_length) us_min_line_length.append(min_line_length) # 遍历自然图像并分析特征 na_contrast = [] na_correlation = [] na_energy = [] na_homogeneity = [] na_num_lines = [] na_avg_line_length = [] na_max_line_length = [] na_min_line_length = [] for i in range(1, 779): img_path = 'D:/zzz/us5/{}.jpg'.format(i) print('Analyzing image {}'.format(img_path)) contrast, correlation, energy, homogeneity, num_lines, avg_line_length, max_line_length, min_line_length = analyze_image(img_path) na_contrast.append(contrast) na_correlation.append(correlation) na_energy.append(energy) na_homogeneity.append(homogeneity) na_num_lines.append(num_lines) na_avg_line_length.append(avg_line_length) na_max_line_length.append(max_line_length) na_min_line_length.append(min_line_length) # 比较结果 fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 10)) ax = axes.ravel() ax[0].bar(['Contrast', 'Correlation', 'Energy', 'Homogeneity'], [na_contrast.mean(), na_correlation.mean(), na_energy.mean(), na_homogeneity.mean()], width=0.4, color='b') ax[0].bar(['Contrast', 'Correlation', 'Energy', 'Homogeneity'], [us_contrast.mean(), us_correlation.mean(), us_energy.mean(), us_homogeneity.mean()], width=0.4, color='r') ax[0].set_title('Texture Features') ax[0].legend(['Natural Images', 'Ultrasound Images']) ax[1].bar(['Number of Lines', 'Average Line Length', 'Max Line Length', 'Min Line Length'], [na_num_lines.mean(), na_avg_line_length.mean(), na_max_line_length.mean(), na_min_line_length.mean()], width=0.4, color='b') ax[1].bar(['Number of Lines', 'Average Line Length', 'Max Line Length', 'Min Line Length'], [us_num_lines.mean(), us_avg_line_length.mean(), us_max_line_length.mean(), us_min_line_length.mean()], width=0.4, color='r') ax[1].set_title('Shape Features') ax[1].legend(['Natural Images', 'Ultrasound Images']) na_texture_diff = np.array([na_contrast, na_correlation, na_energy, na_homogeneity]) - np.array([us_contrast, us_correlation, us_energy, us_homogeneity]) us_texture_diff = np.array([us_contrast, us_correlation, us_energy, us_homogeneity]) - np.array([na_contrast, na_correlation, na_energy, na_homogeneity]) na_shape_diff = np.array([na_num_lines, na_avg_line_length, na_max_line_length, na_min_line_length]) - np.array([us_num_lines, us_avg_line_length, us_max_line_length, us_min_line_length]) us_shape_diff = np.array([us_num_lines, us_avg_line_length, us_max_line_length, us_min_line_length]) - np.array([na_num_lines, na_avg_line_length, na_max_line_length, na_min_line_length]) ax[2].imshow(na_texture_diff, cmap=plt.cm.RdYlGn) ax[2].set_title('Texture Difference') ax[2].axis('off') ax[3].imshow(na_shape_diff, cmap=plt.cm.RdYlGn) ax[3].set_title('Shape Difference') ax[3].axis('off') plt.show() ```

最新推荐

recommend-type

Python学习笔记16 - 猜数字小游戏

猜数字小游戏的相关函数,与主程序搭配使用
recommend-type

机器人比赛内容的讲解,帮助简单了解一下机器人比赛的注意事项

适用于未参加过机器人比赛的小伙伴,简单了解一下注意事项。
recommend-type

shumaguan.rar

shumaguan.rar
recommend-type

信捷MP3系列步进电机CAD图纸.zip

信捷MP3系列步进电机CAD图纸
recommend-type

基于Springboot的足球青训俱乐部管理系统(免费提供全套java开源毕业设计源码+数据库+开题报告+论文+ppt+使用说明

随着社会经济的快速发展,人们对足球俱乐部的需求日益增加,加快了足球健身俱乐部的发展,足球俱乐部管理工作日益繁忙,传统的管理方式已经无法满足足球俱乐部管理需求,因此,为了提高足球俱乐部管理效率,足球俱乐部管理后台系统应运而生。 本文重点阐述了足球青训俱乐部管理后台系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MYSQL数据库进行开发设计,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了俱乐部相关信息管理的重要功能。 本系统经过测试,运行效果稳定,操作方便、快捷,是一个功能全面、实用性好、安全性高,并具有良好的可扩展性、可维护性的足球青训俱乐部管理后台系统。 关键字:俱乐部管理;Spring Boot框架;Java技术;MYSQL数据库
recommend-type

BSC绩效考核指标汇总 (2).docx

BSC(Balanced Scorecard,平衡计分卡)是一种战略绩效管理系统,它将企业的绩效评估从传统的财务维度扩展到非财务领域,以提供更全面、深入的业绩衡量。在提供的文档中,BSC绩效考核指标主要分为两大类:财务类和客户类。 1. 财务类指标: - 部门费用的实际与预算比较:如项目研究开发费用、课题费用、招聘费用、培训费用和新产品研发费用,均通过实际支出与计划预算的百分比来衡量,这反映了部门在成本控制上的效率。 - 经营利润指标:如承保利润、赔付率和理赔统计,这些涉及保险公司的核心盈利能力和风险管理水平。 - 人力成本和保费收益:如人力成本与计划的比例,以及标准保费、附加佣金、续期推动费用等与预算的对比,评估业务运营和盈利能力。 - 财务效率:包括管理费用、销售费用和投资回报率,如净投资收益率、销售目标达成率等,反映公司的财务健康状况和经营效率。 2. 客户类指标: - 客户满意度:通过包装水平客户满意度调研,了解产品和服务的质量和客户体验。 - 市场表现:通过市场销售月报和市场份额,衡量公司在市场中的竞争地位和销售业绩。 - 服务指标:如新契约标保完成度、续保率和出租率,体现客户服务质量和客户忠诚度。 - 品牌和市场知名度:通过问卷调查、公众媒体反馈和总公司级评价来评估品牌影响力和市场认知度。 BSC绩效考核指标旨在确保企业的战略目标与财务和非财务目标的平衡,通过量化这些关键指标,帮助管理层做出决策,优化资源配置,并驱动组织的整体业绩提升。同时,这份指标汇总文档强调了财务稳健性和客户满意度的重要性,体现了现代企业对多维度绩效管理的重视。
recommend-type

管理建模和仿真的文件

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

【进阶】Flask中的会话与用户管理

![python网络编程合集](https://media.geeksforgeeks.org/wp-content/uploads/20201021201514/pythonrequests.PNG) # 2.1 用户注册和登录 ### 2.1.1 用户注册表单的设计和验证 用户注册表单是用户创建帐户的第一步,因此至关重要。它应该简单易用,同时收集必要的用户信息。 * **字段设计:**表单应包含必要的字段,如用户名、电子邮件和密码。 * **验证:**表单应验证字段的格式和有效性,例如电子邮件地址的格式和密码的强度。 * **错误处理:**表单应优雅地处理验证错误,并提供清晰的错误消
recommend-type

卷积神经网络实现手势识别程序

卷积神经网络(Convolutional Neural Network, CNN)在手势识别中是一种非常有效的机器学习模型。CNN特别适用于处理图像数据,因为它能够自动提取和学习局部特征,这对于像手势这样的空间模式识别非常重要。以下是使用CNN实现手势识别的基本步骤: 1. **输入数据准备**:首先,你需要收集或获取一组带有标签的手势图像,作为训练和测试数据集。 2. **数据预处理**:对图像进行标准化、裁剪、大小调整等操作,以便于网络输入。 3. **卷积层(Convolutional Layer)**:这是CNN的核心部分,通过一系列可学习的滤波器(卷积核)对输入图像进行卷积,以
recommend-type

BSC资料.pdf

"BSC资料.pdf" 战略地图是一种战略管理工具,它帮助企业将战略目标可视化,确保所有部门和员工的工作都与公司的整体战略方向保持一致。战略地图的核心内容包括四个相互关联的视角:财务、客户、内部流程和学习与成长。 1. **财务视角**:这是战略地图的最终目标,通常表现为股东价值的提升。例如,股东期望五年后的销售收入达到五亿元,而目前只有一亿元,那么四亿元的差距就是企业的总体目标。 2. **客户视角**:为了实现财务目标,需要明确客户价值主张。企业可以通过提供最低总成本、产品创新、全面解决方案或系统锁定等方式吸引和保留客户,以实现销售额的增长。 3. **内部流程视角**:确定关键流程以支持客户价值主张和财务目标的实现。主要流程可能包括运营管理、客户管理、创新和社会责任等,每个流程都需要有明确的短期、中期和长期目标。 4. **学习与成长视角**:评估和提升企业的人力资本、信息资本和组织资本,确保这些无形资产能够支持内部流程的优化和战略目标的达成。 绘制战略地图的六个步骤: 1. **确定股东价值差距**:识别与股东期望之间的差距。 2. **调整客户价值主张**:分析客户并调整策略以满足他们的需求。 3. **设定价值提升时间表**:规划各阶段的目标以逐步缩小差距。 4. **确定战略主题**:识别关键内部流程并设定目标。 5. **提升战略准备度**:评估并提升无形资产的战略准备度。 6. **制定行动方案**:根据战略地图制定具体行动计划,分配资源和预算。 战略地图的有效性主要取决于两个要素: 1. **KPI的数量及分布比例**:一个有效的战略地图通常包含20个左右的指标,且在四个视角之间有均衡的分布,如财务20%,客户20%,内部流程40%。 2. **KPI的性质比例**:指标应涵盖财务、客户、内部流程和学习与成长等各个方面,以全面反映组织的绩效。 战略地图不仅帮助管理层清晰传达战略意图,也使员工能更好地理解自己的工作如何对公司整体目标产生贡献,从而提高执行力和组织协同性。