久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

python實(shí)現(xiàn)canny邊緣檢測(cè)

瀏覽:2日期:2022-07-11 11:46:01

canny邊緣檢測(cè)原理

canny邊緣檢測(cè)共有5部分組成,下邊我會(huì)分別來介紹。

1 高斯模糊(略)

2 計(jì)算梯度幅值和方向。

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

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

python實(shí)現(xiàn)canny邊緣檢測(cè)

進(jìn)一步可以得到圖像梯度的幅值:

python實(shí)現(xiàn)canny邊緣檢測(cè)

為了簡(jiǎn)化計(jì)算,幅值也可以作如下近似:

python實(shí)現(xiàn)canny邊緣檢測(cè)

角度為:

python實(shí)現(xiàn)canny邊緣檢測(cè)

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

python實(shí)現(xiàn)canny邊緣檢測(cè)

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

3、根據(jù)角度對(duì)幅值進(jìn)行非極大值抑制

劃重點(diǎn):是沿著梯度方向?qū)Ψ颠M(jìn)行非極大值抑制,而非邊緣方向,這里初學(xué)者容易弄混。

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

python實(shí)現(xiàn)canny邊緣檢測(cè)

python實(shí)現(xiàn)canny邊緣檢測(cè)

即梯度方向分別為

α = 90

α = 45

α = 0

α = -45

非極大值抑制即為沿著上述4種類型的梯度方向,比較3*3鄰域內(nèi)對(duì)應(yīng)鄰域值的大小:

python實(shí)現(xiàn)canny邊緣檢測(cè)

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

4、用雙閾值算法檢測(cè)和連接邊緣

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

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

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

python 實(shí)現(xiàn)

import cv2import numpy as npm1 = 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)# 第二步:完成一階有限差分計(jì)算,計(jì)算每一點(diǎn)的梯度幅值與方向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.shapefor 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] = 90continueelse:temp = ((np.arctan2(Gy[0], Gx[0])) * 180 / np.pi)+90if 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.0elif (((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.0elif (((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.0elif (((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] = 90continueelse: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.0elif (((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.0elif (((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.0elif (((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’’’# 第三步:進(jìn)行 非極大值抑制計(jì)算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]# 第四步:雙閾值檢測(cè)和邊緣連接img3 = np.zeros(img2.shape) # 定義雙閾值圖像# TL = 0.4*np.max(img2)# TH = 0.5*np.max(img2)TL = 50TH = 100# 關(guān)鍵在這兩個(gè)閾值的選擇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] = 0elif img2[i, j] > TH:img3[i, j] = 255elif ((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] = 255cv2.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)

運(yùn)行結(jié)果如下

python實(shí)現(xiàn)canny邊緣檢測(cè)

python實(shí)現(xiàn)canny邊緣檢測(cè)

以上就是python實(shí)現(xiàn)canny邊緣檢測(cè)的詳細(xì)內(nèi)容,更多關(guān)于canny邊緣檢測(cè)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久国产精品久久 | 日日做| 国产成人福利在线观看 | 在线观看毛片网站 | 北条麻妃一区二区三区在线观看 | 中文字幕永久第一页 | 国产福利一区二区 | 欧美一区不卡 | 国产精品亲子伦av一区二区三区 | 在线视频自拍 | 久久久精品 | 国产精品视频免费播放 | 亚洲精品视频在线观看免费 | 国产成人99 | 日本a视频 | 中文字幕精品三区 | 97在线免费观看 | 日韩午夜免费 | 五月天婷婷国产精品 | 成人国产| 欧美日韩在线免费观看 | 亚洲欧美日韩在线一区二区三区 | 99久久精品免费看国产一区二区三区 | 久久一区| 男人天堂av网站 | 日韩欧美在线播放 | 午夜视频在线观看网站 | 99精品国产高清一区二区麻豆 | 国产特一级黄色片 | 日韩电影中文字幕 | 99在线视频观看 | 在线免费国产视频 | 少妇一级淫片免费放 | 在线久草 | 成人免费高清 | 日韩免费高清视频 | 国产日韩欧美一区 | 成人 在线 | 日韩日日夜夜 | 一级黄色片看看 | 国产精品婷婷午夜在线观看 |