不使用opencv库用python编写对于应用了非最大抑制技术后的图像采取双阈值方法确定边缘代码

时间: 2023-05-20 16:04:10 浏览: 43
以下是一个使用Python编写的双阈值方法确定边缘的代码示例,该代码不使用OpenCV库: ```python import numpy as np from PIL import Image def non_max_suppression(img, D): M, N = img.shape Z = np.zeros((M,N), dtype=np.int32) angle = D * 180. / np.pi angle[angle < 0] += 180 for i in range(1,M-1): for j in range(1,N-1): q = 255 r = 255 # angle 0 if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180): q = img[i, j+1] r = img[i, j-1] # angle 45 elif (22.5 <= angle[i,j] < 67.5): q = img[i+1, j-1] r = img[i-1, j+1] # angle 90 elif (67.5 <= angle[i,j] < 112.5): q = img[i+1, j] r = img[i-1, j] # angle 135 elif (112.5 <= angle[i,j] < 157.5): q = img[i-1, j-1] r = img[i+1, j+1] if (img[i,j] >= q) and (img[i,j] >= r): Z[i,j] = img[i,j] else: Z[i,j] = 0 return Z def threshold(img, lowThresholdRatio=0.05, highThresholdRatio=0.09): highThreshold = img.max() * highThresholdRatio lowThreshold = highThreshold * lowThresholdRatio M, N = img.shape res = np.zeros((M,N), dtype=np.int32) weak = np.int32(25) strong = np.int32(255) strong_i, strong_j = np.where(img >= highThreshold) zeros_i, zeros_j = np.where(img < lowThreshold) weak_i, weak_j = np.where((img <= highThreshold) & (img >= lowThreshold)) res[strong_i, strong_j] = strong res[weak_i, weak_j] = weak return (res, weak, strong) def hysteresis(img, weak, strong=255): M, N = img.shape for i in range(1, M-1): for j in range(1, N-1): if (img[i,j] == weak): if ((img[i+1, j-1] == strong) or (img[i+1, j] == strong) or (img[i+1, j+1] == strong) or (img[i, j-1] == strong) or (img[i, j+1] == strong) or (img[i-1, j-1] == strong) or (img[i-1, j] == strong) or (img[i-1, j+1] == strong)): img[i, j] = strong else: img[i, j] = 0 return img def canny_edge_detection(img, sigma=1.4, kernel_size=5, lowThresholdRatio=0.05, highThresholdRatio=0.09): # Gaussian blur img_smoothed = np.zeros(img.shape) gaussian_kernel = np.zeros((kernel_size, kernel_size)) for i in range(kernel_size): for j in range(kernel_size): gaussian_kernel[i, j] = (1/(2*np.pi*(sigma**2)))*np.exp(-(((i-(kernel_size//2))**2)+((j-(kernel_size//2))**2))/(2*(sigma**2))) img_smoothed = convolve(img, gaussian_kernel) # Gradient Gx = np.zeros(img_smoothed.shape) Gy = np.zeros(img_smoothed.shape) for i in range(1, img_smoothed.shape[0]-1): for j in range(1, img_smoothed.shape[1]-1): Gx[i, j] = img_smoothed[i, j+1] - img_smoothed[i, j-1] Gy[i, j] = img_smoothed[i-1, j] - img_smoothed[i+1, j] G = np.sqrt(np.square(Gx) + np.square(Gy)) D = np.arctan2(Gy, Gx) # Non-maximum suppression img_suppressed = non_max_suppression(G, D) # Double thresholding img_thresholded, weak, strong = threshold(img_suppressed, lowThresholdRatio, highThresholdRatio) # Hysteresis img_hysteresis = hysteresis(img_thresholded, weak, strong) return img_hysteresis # Example usage img = np.array(Image.open('image.jpg').convert('L')) canny_img = canny_edge_detection(img) Image.fromarray(canny_img).show() ``` 请注意,此代码仅用于演示目的,可能需要进行优化和调整以适应您的特定用例。

相关推荐

最新推荐

recommend-type

python opencv 实现对图像边缘扩充

今天小编就为大家分享一篇python opencv 实现对图像边缘扩充,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python opencv 图像拼接的实现方法

高级图像拼接也叫作基于特征匹配的图像拼接,拼接时消去两幅图像相同的部分,实现拼接合成全景图。这篇文章主要介绍了python opencv 图像拼接,需要的朋友可以参考下
recommend-type

python环境下安装opencv库的方法

注意:安装opencv之前需要先安装numpy,matplotlib等 一、安装方法 方法一、在线安装 1.先安装opencv-python pip install opencv-python --user...备注1:此种方法安装后,import cv2 as cv后,代码可以正常运行,但是
recommend-type

python opencv对图像进行旋转且不裁剪图片的实现方法

今天小编就为大家分享一篇python opencv对图像进行旋转且不裁剪图片的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python使用Opencv实现图像特征检测与匹配的方法

主要介绍了Python使用Opencv实现图像特征检测与匹配的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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