OpenCV庫學習筆記(七)
- 1. 影像梯度
- 1.1 Soble算子
- 1.2 拉普拉斯算子
- 2. Canny邊緣提取
- 2.1 Canny演算法介紹
- 2.2 代碼演示
- 3. 直線檢測
- 3.1 霍夫直線變換介紹
- 3.2 代碼演示
1. 影像梯度
1.1 Soble算子
- 求x,y方向影像梯度
import cv2 as cv
def sobel_demo(image):
grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) # 求x方向的梯度,對x求一階導數
grad_y = cv.Sobel(image, cv.CV_32F, 0, 1) # 求x方向的梯度,對y求一階導數
gradx = cv.convertScaleAbs(grad_x)
grady = cv.convertScaleAbs(grad_y)
cv.imshow("grandient-x", gradx)
cv.imshow("grandient-y", grady)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
sobel_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()

- 求整體梯度
import cv2 as cv
def sobel_demo(image):
grad_x = cv.Sobel(image, cv.CV_32F, 1, 0)
grad_y = cv.Sobel(image, cv.CV_32F, 0, 1)
gradx = cv.convertScaleAbs(grad_x)
grady = cv.convertScaleAbs(grad_y)
gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0)
cv.imshow("gradxy",gradxy)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
sobel_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()

1.2 拉普拉斯算子
- 拉普拉斯算子
import cv2 as cv
def lapalian_demo(image):
dst = cv.Laplacian(image, cv.CV_32F)
lpls = cv.convertScaleAbs(dst)
cv.imshow("lapalian_demo", lpls)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
lapalian_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()

- 自定義拉普拉斯算子
import cv2 as cv
import numpy
def lapalian_demo(image):
kernel = numpy.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
lpls = cv.filter2D(image, cv.CV_32F, kernel=kernel)
cv.imshow("lapalian_demo", lpls)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
lapalian_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()

2. Canny邊緣提取
2.1 Canny演算法介紹
Canny是邊緣檢測演算法,是1986年提出的
步驟:對彩色影像進行高斯模糊,去除噪聲,再進行灰度轉換,對影像求取梯度,根據影像角度實作非最大信號壓制,進行高低閾值過濾
- 高斯模糊:GaussianBlur
- 灰度轉換:cvtColor
- 計算梯度:Sobel/Scharr
- 非最大信號抑制
- 高低閾值輸出二值影像
2.2 代碼演示
import cv2 as cv
import numpy
def edge_demo(image):
blurred = cv.GaussianBlur(image, (3, 3), 0)
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
# x gradient
gradx = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
grady = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
# edge 高閾值為150 低閾值為50
edge_output = cv.Canny(gradx, grady, 50, 150)
cv.imshow("Canny Edge", edge_output)
dst = cv.bitwise_and(image, image, mask=edge_output)
cv.imshow("Color Edge", dst)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
edge_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()

3. 直線檢測
3.1 霍夫直線變換介紹
- Hough Line Transform用來做直線檢測
- 前提條件:邊緣檢測已經完成
- 需要理解:平面空間到極坐標空間轉換
3.2 代碼演示
import cv2 as cv
import numpy
def line_detection(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 50, 150, apertureSize=3)
lines = cv.HoughLines(edges, 1, numpy.pi/180, 200)
for line in lines:
rho, theta = line[0]
a = numpy.cos(theta)
b = numpy.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("image lines", image)
image = cv.resize(cv.imread("star.jpg"), (300,400))
cv.imshow("image", image)
line_detection(image)
cv.waitKey(0)
cv.destroyAllWindows()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/251776.html
標籤:python
