日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python canny优化_python实现canny边缘检测

發布時間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python canny优化_python实现canny边缘检测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

canny邊緣檢測原理

canny邊緣檢測共有5部分組成,下邊我會分別來介紹。

1 高斯模糊(略)

2 計算梯度幅值和方向。

可選用的模板:soble算子、Prewitt算子、Roberts模板等等;

一般采用soble算子,OpenCV也是如此,利用soble水平和垂直算子與輸入圖像卷積計算dx、dy:

進一步可以得到圖像梯度的幅值:

為了簡化計算,幅值也可以作如下近似:

角度為:

如下圖表示了中心點的梯度向量、方位角以及邊緣方向(任一點的邊緣與梯度向量正交) :

θ = θm = arctan(dy/dx)(邊緣方向)

α = θ + 90= arctan(dy/dx) + 90(梯度方向)

3、根據角度對幅值進行非極大值抑制

劃重點:是沿著梯度方向對幅值進行非極大值抑制,而非邊緣方向,這里初學者容易弄混。

例如:3*3區域內,邊緣可以劃分為垂直、水平、45°、135°4個方向,同樣,梯度反向也為四個方向(與邊緣方向正交)。因此為了進行非極大值,將所有可能的方向量化為4個方向,如下圖:

即梯度方向分別為

α = 90

α = 45

α = 0

α = -45

非極大值抑制即為沿著上述4種類型的梯度方向,比較3*3鄰域內對應鄰域值的大小:

在每一點上,領域中心 x 與沿著其對應的梯度方向的兩個像素相比,若中心像素為最大值,則保留,否則中心置0,這樣可以抑制非極大值,保留局部梯度最大的點,以得到細化的邊緣。

4、用雙閾值算法檢測和連接邊緣

1選取系數TH和TL,比率為2:1或3:1。(一般取TH=0.3或0.2,TL=0.1);

2 將小于低閾值的點拋棄,賦0;將大于高閾值的點立即標記(這些點為確定邊緣 點),賦1或255;

3將小于高閾值,大于低閾值的點使用8連通區域確定(即:只有與TH像素連接時才會被接受,成為邊緣點,賦 1或255)

python 實現

import cv2

import numpy as np

m1 = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])

m2 = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])

from matplotlib import pyplot as plt

# 第一步:完成高斯平滑濾波

img = cv2.imread("B9064CF1D57871735CE11A0F368DCF27.jpg", 0)

sobel = cv2.Canny(img, 50, 100)

cv2.namedWindow('5', 0)

cv2.resizeWindow("5", 640, 480)

cv2.imshow("5", sobel) # 角度值灰度圖

img = cv2.GaussianBlur(img, (3, 3), 2)

# 第二步:完成一階有限差分計算,計算每一點的梯度幅值與方向

img1 = np.zeros(img.shape, dtype="uint8") # 與原圖大小相同

theta = np.zeros(img.shape, dtype="float") # 方向矩陣原圖像大小

img = cv2.copyMakeBorder(img, 1, 1, 1, 1, borderType=cv2.BORDER_REPLICATE)

rows, cols = img.shape

for i in range(1, rows - 1):

for j in range(1, cols - 1):

Gy = [np.sum(m2 * img[i - 1:i + 2, j - 1:j + 2])]

#Gy = (np.dot(np.array([1, 1, 1]), (m2 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))

Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]

#Gx = (np.dot(np.array([1, 1, 1]), (m1 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))

if Gx[0] == 0:

theta[i - 1, j - 1] = 90

continue

else:

temp = ((np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)+90

if Gx[0] * Gy[0] > 0:

if Gx[0] > 0:

# 第一象線

theta[i - 1, j - 1] = np.abs(temp)

else:

# 第三象線

theta[i - 1, j - 1] = (np.abs(temp) - 180)

if Gx[0] * Gy[0] < 0:

if Gx[0] > 0:

# 第四象線

theta[i - 1, j - 1] = (-1) * np.abs(temp)

else:

# 第二象線

theta[i - 1, j - 1] = 180 - np.abs(temp)

img1[i - 1, j - 1] = (np.sqrt(Gx[0] ** 2 + Gy[0] ** 2))

for i in range(1, rows - 2):

for j in range(1, cols - 2):

if (((theta[i, j] >= -22.5) and (theta[i, j] < 22.5)) or

((theta[i, j] <= -157.5) and (theta[i, j] >= -180)) or

((theta[i, j] >= 157.5) and (theta[i, j] < 180))):

theta[i, j] = 0.0

elif (((theta[i, j] >= 22.5) and (theta[i, j] < 67.5)) or

((theta[i, j] <= -112.5) and (theta[i, j] >= -157.5))):

theta[i, j] = -45.0

elif (((theta[i, j] >= 67.5) and (theta[i, j] < 112.5)) or

((theta[i, j] <= -67.5) and (theta[i, j] >= -112.5))):

theta[i, j] = 90.0

elif (((theta[i, j] >= 112.5) and (theta[i, j] < 157.5)) or

((theta[i, j] <= -22.5) and (theta[i, j] >= -67.5))):

theta[i, j] = 45.0

'''

for i in range(1, rows - 1):

for j in range(1, cols - 1):

Gy = [np.sum(m2 * img[i - 1:i + 2, j - 1:j + 2])]

#Gy = (np.dot(np.array([1, 1, 1]), (m2 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))

Gx = [np.sum(m1 * img[i - 1:i + 2, j - 1:j + 2])]

#Gx = (np.dot(np.array([1, 1, 1]), (m1 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1], [1], [1]]))

if Gx[0] == 0:

theta[i - 1, j - 1] = 90

continue

else:

temp = (np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)

if Gx[0] * Gy[0] > 0:

if Gx[0] > 0:

# 第一象線

theta[i - 1, j - 1] = np.abs(temp)

else:

# 第三象線

theta[i - 1, j - 1] = (np.abs(temp) - 180)

if Gx[0] * Gy[0] < 0:

if Gx[0] > 0:

# 第四象線

theta[i - 1, j - 1] = (-1) * np.abs(temp)

else:

# 第二象線

theta[i - 1, j - 1] = 180 - np.abs(temp)

img1[i - 1, j - 1] = (np.sqrt(Gx[0] ** 2 + Gy[0] ** 2))

for i in range(1, rows - 2):

for j in range(1, cols - 2):

if (((theta[i, j] >= -22.5) and (theta[i, j] < 22.5)) or

((theta[i, j] <= -157.5) and (theta[i, j] >= -180)) or

((theta[i, j] >= 157.5) and (theta[i, j] < 180))):

theta[i, j] = 90.0

elif (((theta[i, j] >= 22.5) and (theta[i, j] < 67.5)) or

((theta[i, j] <= -112.5) and (theta[i, j] >= -157.5))):

theta[i, j] = 45.0

elif (((theta[i, j] >= 67.5) and (theta[i, j] < 112.5)) or

((theta[i, j] <= -67.5) and (theta[i, j] >= -112.5))):

theta[i, j] = 0.0

elif (((theta[i, j] >= 112.5) and (theta[i, j] < 157.5)) or

((theta[i, j] <= -22.5) and (theta[i, j] >= -67.5))):

theta[i, j] = -45.0

'''

# 第三步:進行 非極大值抑制計算

img2 = np.zeros(img1.shape) # 非極大值抑制圖像矩陣

for i in range(1, img2.shape[0] - 1):

for j in range(1, img2.shape[1] - 1):

# 0度j不變

if (theta[i, j] == 0.0) and (img1[i, j] == np.max([img1[i, j], img1[i + 1, j], img1[i - 1, j]])):

img2[i, j] = img1[i, j]

if (theta[i, j] == -45.0) and img1[i, j] == np.max([img1[i, j], img1[i - 1, j - 1], img1[i + 1, j + 1]]):

img2[i, j] = img1[i, j]

if (theta[i, j] == 90.0) and img1[i, j] == np.max([img1[i, j], img1[i, j + 1], img1[i, j - 1]]):

img2[i, j] = img1[i, j]

if (theta[i, j] == 45.0) and img1[i, j] == np.max([img1[i, j], img1[i - 1, j + 1], img1[i + 1, j - 1]]):

img2[i, j] = img1[i, j]

# 第四步:雙閾值檢測和邊緣連接

img3 = np.zeros(img2.shape) # 定義雙閾值圖像

# TL = 0.4*np.max(img2)

# TH = 0.5*np.max(img2)

TL = 50

TH = 100

# 關鍵在這兩個閾值的選擇

for i in range(1, img3.shape[0] - 1):

for j in range(1, img3.shape[1] - 1):

if img2[i, j] < TL:

img3[i, j] = 0

elif img2[i, j] > TH:

img3[i, j] = 255

elif ((img2[i + 1, j] < TH) or (img2[i - 1, j] < TH) or (img2[i, j + 1] < TH) or

(img2[i, j - 1] < TH) or (img2[i - 1, j - 1] < TH) or (img2[i - 1, j + 1] < TH) or

(img2[i + 1, j + 1] < TH) or (img2[i + 1, j - 1] < TH)):

img3[i, j] = 255

cv2.namedWindow('1', 0)

cv2.resizeWindow("1", 640, 480)

cv2.namedWindow('2', 0)

cv2.resizeWindow("2", 640, 480)

cv2.namedWindow('3', 0)

cv2.resizeWindow("3", 640, 480)

cv2.namedWindow('4', 0)

cv2.resizeWindow("4", 640, 480)

cv2.imshow("1", img) # 原始圖像

cv2.imshow("2", img1) # 梯度幅值圖

cv2.imshow("3", img2) # 非極大值抑制灰度圖

cv2.imshow("4", img3) # 最終效果圖

cv2.waitKey(0)

運行結果如下

以上就是python實現canny邊緣檢測的詳細內容,更多關于canny邊緣檢測的資料請關注我們其它相關文章!

本文標題: python實現canny邊緣檢測

本文地址: http://www.cppcns.com/jiaoben/python/345130.html

總結

以上是生活随笔為你收集整理的python canny优化_python实现canny边缘检测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。