點擊上方「AI有道」,選擇「星標」公衆號
html
重磅乾貨,第一時間送達python
現在深度學習的快速發展給計算機視覺注入了史無前例的新活力!其中在計算機圖形學和計算機視覺裏面最流行的一個庫就是 OpenCV。OpenCV 在自動駕駛和仿生機器人當中的應用很是普遍。git
而在 2018 年 11 月份,OpenCV 經過 GITHUB 正式發佈了 OpenCV 又一個重要里程碑版本 OpenCV 4.0。github
今天給你們推薦一個乾貨滿滿的 GitHub 項目。該項目包含了 CV 領域,OpenCV 圖像處理入門 100 題實例解析,並配備完整的 Pyhon 代碼。瀏覽器
項目地址:機器學習
https://github.com/yoyoyo-yo/Gasyori100knockide
極簡安裝:學習
做者推薦了 OpenCV 的極簡安裝方法:ui
1. 安裝 MiniCondaspa
地址:https://conda.io/miniconda.html
2. 建立虛擬環境並激活
$ conda create python = 3.6 - n gasyori 100
$ source actiavte gasyori 100
3. 安裝包
$ pip install -r requirement.txt
其中,requirement.txt 文件在項目根目錄下,下載至命令行所在目錄直接運行上述命令便可。
100 題實例:
做者寫的 OpenCV 100 題按照從簡單到複雜逐一解析,很是適合咱們的學習路徑。
例如 Q1-10:
咱們首先來看一個簡單的例子。
Q1. 讀取圖像並按 BGR 順序更改 RGB
import cv2
# Read image
img = cv2.imread("imori.jpg")
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# RGB > BGR
img[:, :, 0] = r
img[:, :, 1] = g
img[:, :, 2] = b
# Save result
cv2.imwrite("out.jpg", img)
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
例如 Q41-50:
咱們來看一個稍微複雜的例子。
Q41. Canny邊緣檢測(步驟1)邊緣強度
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read image
img = cv2.imread("imori.jpg").astype(np.float32)
H, W, C = img.shape
# Gray
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
# Gaussian Filter
K_size = 5
sigma = 1.4
## Zero padding
pad = K_size // 2
gau = np.zeros((H + pad*2, W + pad*2), dtype=np.float32)
#gau[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float32)
gau = np.pad(gray, (pad, pad), 'edge')
tmp = gau.copy()
## Kernel
K = np.zeros((K_size, K_size), dtype=np.float32)
for x in range(-pad, -pad+K_size):
for y in range(-pad, -pad+K_size):
K[y+pad, x+pad] = np.exp( -(x**2 + y**2) / (2* (sigma**2)))
K /= (sigma * np.sqrt(2 * np.pi))
K /= K.sum()
for y in range(H):
for x in range(W):
gau[pad+y, pad+x] = np.sum(K * tmp[y:y+K_size, x:x+K_size])
## Sobel vertical
KSV = np.array(((-1., -2., -1.), (0., 0., 0.), (1., 2., 1.)), dtype=np.float32)
## Sobel horizontal
KSH = np.array(((-1., 0., 1.), (-2., 0., 2.), (-1., 0., 1.)), dtype=np.float32)
gau = gau[pad-1:H+pad+1, pad-1:W+pad+1]
fy = np.zeros_like(gau, dtype=np.float32)
fx = np.zeros_like(gau, dtype=np.float32)
K_size = 3
pad = K_size // 2
for y in range(H):
for x in range(W):
fy[pad+y, pad+x] = np.sum(KSV * gau[y:y+K_size, x:x+K_size])
fx[pad+y, pad+x] = np.sum(KSH * gau[y:y+K_size, x:x+K_size])
fx = fx[pad:pad+H, pad:pad+W]
fy = fy[pad:pad+H, pad:pad+W]
# Non-maximum suppression
edge = np.sqrt(np.power(fx, 2) + np.power(fy, 2))
fx[fx == 0] = 1e-5
tan = np.arctan(fy / fx)
## Angle quantization
angle = np.zeros_like(tan, dtype=np.uint8)
angle[np.where((tan > -0.4142) & (tan <= 0.4142))] = 0
angle[np.where((tan > 0.4142) & (tan < 2.4142))] = 45
angle[np.where((tan >= 2.4142) | (tan <= -2.4142))] = 95
angle[np.where((tan > -2.4142) & (tan <= -0.4142))] = 135
out = angle.astype(np.uint8)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
項目特點:
該項目最大的特點就是 100 題按部就班,基本涵蓋了 OpenCV 的關鍵知識點,目前已經更新了前 60 題,後續的會陸續發佈。
惟一的缺點是項目語言是日語,稍有不便。可是問題不大,筆者推薦一個方法,可使用谷歌瀏覽器自動翻譯網頁便可。並且,全部的代碼都是英文的,不妨礙理解。
若是你正在入門 CV,正在學習 OpenCV,那麼這個項目將會是一個不錯的從入門到進階的教程。上手代碼,親自跑一跑結果,但願對你們有所幫助!
【推薦閱讀】