查看“︁蒲瑞维特算子”︁的源代码
←
蒲瑞维特算子
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{Refimprove|time=2024-06-17T09:01:41+00:00}} {{FeatureDetectionCompVisNavbox}} '''蒲瑞维特算子'''({{lang-en|Prewitt Operator}})是一種在[[图像处理|影像處理]]中,用於[[边缘检测|邊緣檢測]]的[[差分|離散微分]]運算子。此算子由Judith M. S. Prewitt發明,並以她的名字命名<ref>{{Cite conference |last=Prewitt |first=J.M.S. |date=1970 |title=Object Enhancement and Extraction |publisher=Academic Press |book-title=Picture processing and Psychopictorics}}</ref>。蒲瑞维特算子通過在水平方向和垂直方向上對影像進行捲積,使用一個小型的整數值濾波器來完成運算,因此在計算成本方面相對較低,類似於[[索伯算子]]。蒲瑞维特算子的優點在於其計算簡單且效率高,適合於需要快速邊緣檢測的應用場景。 然而,其粗糙的梯度近似使得它在處理細節豐富或高頻率變化較多的影像時效果不佳。為了更準確地檢測邊緣,通常需要使用如[[坎尼算子]]等更先進的方法。 ==公式== 蒲瑞维特算子包含兩個3x3的矩陣,分別為水平方向以及垂直方向,表示為<math>M_x</math>與<math>M_y</math>。 :<math> \mathbf{M_x} = \begin{bmatrix} +1 &+1 &+1 \\ 0 &0 &0 \\ -1 &-1 &-1 \end{bmatrix} \quad \mbox{and} \quad \mathbf{M_y} = \begin{bmatrix} +1 & 0 & -1 \\ +1 & 0 & -1 \\ +1 & 0 & -1 \end{bmatrix} </math> 利用上述兩矩陣與影像做[[卷积|2d卷積運算]],即可達到水平向與垂直向差分的效果,得到其對應亮度梯度向量,公式如下: :<math>\mathbf{G_x} = \mathbf{M_x} * \mathbf{A} \quad \mbox{and} \quad \mathbf{G_y} = \mathbf{M_y} * \mathbf{A}</math> 在這裏運算子'*'代表的2d卷積運算,A代表的是原影像。 對於影像中每個點的梯度值,透過以下公式得到: :<math>\mathbf{G} = \sqrt{ {\mathbf{G}_x}^2 + {\mathbf{G}_y}^2 }</math> 運用<math> G_x </math>與<math> G_y </math>,可以計算其梯度向量: :<math>\mathbf{\Theta} = \operatorname{atan2}\left({ \mathbf{G}_y , \mathbf{G}_x }\right)</math> == 實作 == 在實作上,因為只有該點的周圍八個點需要被計算,同時在梯度向量的計算上也都只牽涉到整數的加法計算,而且上述的兩個離散濾波器是可分解的: <math>\mathbf{M_x} = \begin{bmatrix} +1 & +1 & +1 \\ 0 & 0 & 0 \\ -1 & -1 & -1 \end{bmatrix} = \begin{bmatrix} +1\\ 0\\ -1 \end{bmatrix} \begin{bmatrix} +1 & +1 & +1 \end{bmatrix}</math><math> \mathbf{M_y} = \begin{bmatrix} +1 & 0 & -1 \\ +1 & 0 & -1 \\ +1 & 0 & -1 \end{bmatrix} = \begin{bmatrix} +1\\ +1\\ +1 \end{bmatrix} \begin{bmatrix} +1 & 0 & -1 \end{bmatrix} </math> : ==範例== {| class="wikitable" |+ ![[File:Fruits image.png|thumb|水果影像]] ![[File:Fruits horizontal edge.png|thumb|水平向的邊緣]] |- |[[File:Fruits vertical edge.png|thumb|'''垂直向的邊緣''']] |[[File:Fruits edge.png|thumb|'''水果影像的邊緣偵測(蒲瑞维特算子)''']] |} == 程式範例 == <syntaxhighlight lang="python3"> # Read image and convert it into grayscale img_file = '<name of input image file>.<file format>' img = np.array(Image.open(img_file).convert('L')) # Prepare edge, horizontal edge and vertical edge image arrays edge_img = np.zeros_like(img) horizontal_edge_img = np.zeros_like(img) vertical_edge_img = np.zeros_like(img) # Prewitt operator matrices Mx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]]) My = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]]) # Perform 2d convolution for i in range(1, img.shape[0]-1): for j in range(1, img.shape[1]-1): A = img[i-1:i+2, j-1:j+2] Gx = np.sum(Mx * A) Gy = np.sum(My * A) horizontal_edge_img[i][j] = abs(Gx) vertical_edge_img[i][j] = abs(Gy) edge_img[i][j] = np.sqrt(Gx ** 2 + Gy ** 2) # Define a threshold value thresholdValue = 80 edge_img = np.where(edge_img < thresholdValue, 0, edge_img) horizontal_edge_img = np.where(horizontal_edge_img < thresholdValue, 0, horizontal_edge_img) vertical_edge_img = np.where(vertical_edge_img < thresholdValue, 0, vertical_edge_img) plt.figure() plt.subplot(221) plt.axis('off'); plt.title('Grayscale image of fruits'); plt.imshow(img, cmap='gray', vmin=0, vmax=255) plt.subplot(222) plt.axis('off') plt.title('Horizontal edge') plt.imshow(horizontal_edge_img, cmap='gray', vmin=0, vmax=255) plt.subplot(223) plt.axis('off') plt.title('Vertical edge') plt.imshow(vertical_edge_img, cmap='gray', vmin=0, vmax=255) plt.subplot(224) plt.axis('off') plt.title('Edge image') plt.imshow(edge_img, cmap='gray', vmin=0, vmax=255) plt.show()</syntaxhighlight> ==參見== * [[索伯算子]] * [[坎尼算子]] * [[边缘检测|邊緣偵測]] * [[特征检测|特徵偵測]] * [[數位影像處理]] * [[图像处理|影像處理]] * [[计算机视觉|電腦視覺]] * [[数字信号处理|數位訊號處理]] ==References== {{Reflist}} [[Category:特征检测_(计算机视觉)]]
该页面使用的模板:
Template:Cite conference
(
查看源代码
)
Template:FeatureDetectionCompVisNavbox
(
查看源代码
)
Template:Lang-en
(
查看源代码
)
Template:Refimprove
(
查看源代码
)
Template:Reflist
(
查看源代码
)
返回
蒲瑞维特算子
。
导航菜单
个人工具
登录
命名空间
页面
讨论
不转换
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
工具
链入页面
相关更改
页面信息