用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码

时间: 2023-11-16 14:04:45 浏览: 26
首先,我们需要导入所需的库: ```python import cv2 import numpy as np import os from matplotlib import pyplot as plt ``` 然后,我们需要定义一个函数来计算图像的纹理特征。我们可以使用灰度共生矩阵(GLCM)来计算纹理特征。GLCM 是一个包含图像中像素对之间关系的概率矩阵。 ```python def compute_texture_feature(image): # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Compute the GLCM (gray-level co-occurrence matrix) glcm = cv2.calcGLCM(gray, [5], 0, 256, 256, symmetric=True) # Compute the contrast, dissimilarity, energy, and homogeneity from the GLCM contrast = cv2.compareHist(cv2.normalize(glcm, None), cv2.normalize(glcm, None), cv2.HISTCMP_CONTRAST) dissimilarity = cv2.compareHist(cv2.normalize(glcm, None), cv2.normalize(glcm, None), cv2.HISTCMP_DISPARITY) energy = cv2.compareHist(cv2.normalize(glcm, None), cv2.normalize(glcm, None), cv2.HISTCMP_ENERGY) homogeneity = cv2.compareHist(cv2.normalize(glcm, None), cv2.normalize(glcm, None), cv2.HISTCMP_INTERSECT) return contrast, dissimilarity, energy, homogeneity ``` 接下来,我们需要定义一个函数来计算图像的形状特征。我们可以使用轮廓来计算形状特征。 ```python def compute_shape_feature(image): # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Threshold the image to get a binary image ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # Find the contours in the binary image contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Compute the area and perimeter of the contour area = cv2.contourArea(contours[0]) perimeter = cv2.arcLength(contours[0], True) return area, perimeter ``` 现在,我们可以定义一个主函数来计算两种图像的颜色、纹理和形状特征,并进行差异性分析。 ```python def main(): # Define the paths to the image folders us_path = 'D:/zzz/us5' na_path = 'D:/zzz/na4' # Initialize the feature matrices for the ultrasound and natural images us_features = np.zeros((84, 7)) na_features = np.zeros((84, 7)) # Iterate over the ultrasound images and compute the features for i in range(1, 85): # Read the image image = cv2.imread(os.path.join(us_path, str(i) + '.jpg')) # Compute the color features us_features[i-1, 0] = np.mean(image[:, :, 0]) us_features[i-1, 1] = np.mean(image[:, :, 1]) us_features[i-1, 2] = np.mean(image[:, :, 2]) # Compute the texture features us_features[i-1, 3], us_features[i-1, 4], us_features[i-1, 5], us_features[i-1, 6] = compute_texture_feature(image) # Compute the shape features us_features[i-1, 7], us_features[i-1, 8] = compute_shape_feature(image) # Iterate over the natural images and compute the features for i in range(1, 85): # Read the image image = cv2.imread(os.path.join(na_path, str(i) + '.jpg')) # Compute the color features na_features[i-1, 0] = np.mean(image[:, :, 0]) na_features[i-1, 1] = np.mean(image[:, :, 1]) na_features[i-1, 2] = np.mean(image[:, :, 2]) # Compute the texture features na_features[i-1, 3], na_features[i-1, 4], na_features[i-1, 5], na_features[i-1, 6] = compute_texture_feature(image) # Compute the shape features na_features[i-1, 7], na_features[i-1, 8] = compute_shape_feature(image) # Compute the mean and standard deviation of each feature for the ultrasound images us_mean_color = np.mean(us_features[:, 0:3], axis=0) us_std_color = np.std(us_features[:, 0:3], axis=0) us_mean_texture = np.mean(us_features[:, 3:6], axis=0) us_std_texture = np.std(us_features[:, 3:6], axis=0) us_mean_shape = np.mean(us_features[:, 6:8], axis=0) us_std_shape = np.std(us_features[:, 6:8], axis=0) # Compute the mean and standard deviation of each feature for the natural images na_mean_color = np.mean(na_features[:, 0:3], axis=0) na_std_color = np.std(na_features[:, 0:3], axis=0) na_mean_texture = np.mean(na_features[:, 3:6], axis=0) na_std_texture = np.std(na_features[:, 3:6], axis=0) na_mean_shape = np.mean(na_features[:, 6:8], axis=0) na_std_shape = np.std(na_features[:, 6:8], axis=0) # Plot the results fig, axs = plt.subplots(3, 3) fig.suptitle('Feature Analysis') axs[0, 0].bar(['Ultrasound', 'Natural'], [us_mean_color[0], na_mean_color[0]], yerr=[us_std_color[0], na_std_color[0]]) axs[0, 0].set_title('Mean Color (B)') axs[0, 1].bar(['Ultrasound', 'Natural'], [us_mean_color[1], na_mean_color[1]], yerr=[us_std_color[1], na_std_color[1]]) axs[0, 1].set_title('Mean Color (G)') axs[0, 2].bar(['Ultrasound', 'Natural'], [us_mean_color[2], na_mean_color[2]], yerr=[us_std_color[2], na_std_color[2]]) axs[0, 2].set_title('Mean Color (R)') axs[1, 0].bar(['Ultrasound', 'Natural'], [us_mean_texture[0], na_mean_texture[0]], yerr=[us_std_texture[0], na_std_texture[0]]) axs[1, 0].set_title('Contrast') axs[1, 1].bar(['Ultrasound', 'Natural'], [us_mean_texture[1], na_mean_texture[1]], yerr=[us_std_texture[1], na_std_texture[1]]) axs[1, 1].set_title('Dissimilarity') axs[1, 2].bar(['Ultrasound', 'Natural'], [us_mean_texture[2], na_mean_texture[2]], yerr=[us_std_texture[2], na_std_texture[2]]) axs[1, 2].set_title('Energy') axs[2, 0].bar(['Ultrasound', 'Natural'], [us_mean_texture[3], na_mean_texture[3]], yerr=[us_std_texture[3], na_std_texture[3]]) axs[2, 0].set_title('Homogeneity') axs[2, 1].bar(['Ultrasound', 'Natural'], [us_mean_shape[0], na_mean_shape[0]], yerr=[us_std_shape[0], na_std_shape[0]]) axs[2, 1].set_title('Area') axs[2, 2].bar(['Ultrasound', 'Natural'], [us_mean_shape[1], na_mean_shape[1]], yerr=[us_std_shape[1], na_std_shape[1]]) axs[2, 2].set_title('Perimeter') plt.show() if __name__ == '__main__': main() ``` 在这个函数中,我们首先定义了两个文件夹的路径,然后初始化了两个特征矩阵。接下来,我们使用循环来迭代两个文件夹中的所有图像,并计算每个图像的颜色、纹理和形状特征。我们使用 `np.mean()` 和 `np.std()` 函数来计算每个特征的均值和标准差。 最后,我们使用 `plt.bar()` 函数将每个特征的均值和标准差绘制成条形图,以便进行可视化分析。 现在,我们可以调用 `main()` 函数来进行分析。 注意:由于本人没有该数据集,上述代码未测试,可能存在一些小错误。

相关推荐

最新推荐

recommend-type

rockyou.txt

rockyou
recommend-type

ASP+ACCESS网上人才信息管理系统(源代码+论文)【ASP】.zip

ASP+ACCESS网上人才信息管理系统(源代码+论文)【ASP】
recommend-type

河北金融学院经济大数据课设2024年 软科学校爬虫课设

河北金融学院经济大数据课设2024年 软科学校爬虫课设
recommend-type

widgetsnbextension-4.0.0b0-py3-none-any.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

协同过滤服务+源代码+文档说明

- 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! <项目介绍> 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
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

MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性

![MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性](https://picx.zhimg.com/80/v2-8132d9acfebe1c248865e24dc5445720_1440w.webp?source=1def8aca) # 1. MATLAB结构体基础** MATLAB结构体是一种数据结构,用于存储和组织相关数据。它由一系列域组成,每个域都有一个名称和一个值。结构体提供了对数据的灵活访问和管理,使其成为组织和处理复杂数据集的理想选择。 MATLAB中创建结构体非常简单,使用struct函数即可。例如: ```matlab myStruct
recommend-type

详细描述一下STM32F103C8T6怎么与DHT11连接

STM32F103C8T6可以通过单总线协议与DHT11连接。连接步骤如下: 1. 将DHT11的VCC引脚连接到STM32F103C8T6的5V电源引脚; 2. 将DHT11的GND引脚连接到STM32F103C8T6的GND引脚; 3. 将DHT11的DATA引脚连接到STM32F103C8T6的GPIO引脚,可以选择任一GPIO引脚,需要在程序中配置; 4. 在程序中初始化GPIO引脚,将其设为输出模式,并输出高电平,持续至少18ms,以激活DHT11; 5. 将GPIO引脚设为输入模式,等待DHT11响应,DHT11会先输出一个80us的低电平,然后输出一个80us的高电平,
recommend-type

JSBSim Reference Manual

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