OpenCV 機器視覺入門精選 100 題(附 Python 代碼)

OpenCV 機器視覺入門精選 100 題(附 Python 代碼)


點擊上方“AI有道”,選擇“星標”公衆號

重磅乾貨,第一時間送達圖片

image.png


如今深度學習的快速發展給計算機視覺注入了前所未有的新活力!其中在計算機圖形學和計算機視覺裏面最流行的一個庫就是 OpenCV。OpenCV 在自動駕駛和仿生機器人當中的應用非常廣泛。


而在 2018 年 11 月份,OpenCV 通過 GITHUB 正式發佈了 OpenCV 又一個重要里程碑版本 OpenCV 4.0。


今天給大家推薦一個乾貨滿滿的 GitHub 項目。該項目包含了 CV 領域,OpenCV 圖像處理入門 100 題實例解析,並配備完整的 Pyhon 代碼。


項目地址:


https://github.com/yoyoyo-yo/Gasyori100knock


image.png


極簡安裝:


作者推薦了 OpenCV 的極簡安裝方法:


1. 安裝 MiniConda


地址: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:

image.png


我們首先來看一個簡單的例子。


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()

















image.png

例如 Q41-50:

image.png


我們來看一個稍微複雜的例子。


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()































































image.png


項目特色:


該項目最大的特色就是 100 題循序漸進,基本涵蓋了 OpenCV 的關鍵知識點,目前已經更新了前 60 題,後續的會陸續發佈。


唯一的缺點是項目語言是日語,稍有不便。但是問題不大,筆者推薦一個方法,可以使用谷歌瀏覽器自動翻譯網頁即可。而且,所有的代碼都是英文的,不妨礙理解。


如果你正在入門 CV,正在學習 OpenCV,那麼這個項目將會是一個不錯的從入門到進階的教程。上手代碼,親自跑一跑結果,希望對大家有所幫助!



【推薦閱讀】

乾貨 | 公衆號歷史文章精選(附資源)

我的深度學習入門路線

我的機器學習入門路線圖




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章