訂閱
糾錯(cuò)
加入自媒體

2022最新計(jì)算機(jī)視覺學(xué)習(xí)路線(入門篇)


圖像變換

圖像變換包括平移、旋轉(zhuǎn)、縮放、裁剪和翻轉(zhuǎn)圖像。

img=cv.imread('../input/images-for-computer-vision/tiger1.jpg')

width, height, _=img.shape

# Translating

M_translate=np.float32([[1,0,200],[0,1,100]]) # 200=> Translation along x-axis and 100=>translation along y-axis

img_translate=cv.warpAffine(img,M_translate,(height,width))

# Rotating

center=(width/2,height/2)

M_rotate=cv.getRotationMatrix2D(center, angle=90, scale=1)

img_rotate=cv.warpAffine(img,M_rotate,(width,height))

# Scaling

scale_percent = 50

width = int(img.shape[1] * scale_percent / 100)

height = int(img.shape[0] * scale_percent / 100)

dim = (width, height)

img_scale = cv.resize(img, dim, interpolation = cv.INTER_AREA)

# Flipping

img_flip=cv.flip(img,1) # 0:Along horizontal axis, 1:Along verticle axis, -1: first along verticle then horizontal

# Shearing

srcTri = np.a(chǎn)rray( [[0, 0], [img.shape[1] - 1, 0], [0,img.shape[0] - 1]] ).a(chǎn)stype(np.float32)

dstTri = np.a(chǎn)rray( [[0, img.shape[1]*0.33], [img.shape[1]*0.85, img.shape[0]*0.25], [img.shape[1]*0.15, img.shape[0]*0.7]] ).a(chǎn)stype(np.float32)

warp_mat = cv.getAffineTransform(srcTri, dstTri)

img_warp = cv.warpAffine(img, warp_mat, (height, width))

myplot([img, img_translate, img_rotate, img_scale, img_flip, img_warp],

      ['Original Image', 'Translated Image', 'Rotated Image', 'Scaled Image', 'Flipped Image', 'Sheared Image'])

圖像預(yù)處理

閾值處理:在閾值處理中,小于閾值的像素值變?yōu)?0(黑色),大于閾值的像素值變?yōu)?255(白色)。

我將閾值設(shè)為 150,但你也可以選擇任何其他數(shù)字。

# For visualising the filters

import plotly.graph_objects as go

from plotly.subplots import make_subplots

def plot_3d(img1, img2, titles):

   fig = make_subplots(rows=1, cols=2,
                   specs=[[{'is_3d': True}, {'is_3d': True}]],
                   subplot_titles=[titles[0], titles[1]],
                   )

   x, y=np.mgrid[0:img1.shape[0], 0:img1.shape[1]]

   fig.a(chǎn)dd_trace(go.Surface(x=x, y=y(tǒng), z=img1[:,:,0]), row=1, col=1)

   fig.a(chǎn)dd_trace(go.Surface(x=x, y=y(tǒng), z=img2[:,:,0]), row=1, col=2)

   fig.update_traces(contours_z=dict(show=True, usecolormap=True,
                                 highlightcolor="limegreen", project_z=True))

   fig.show()

img=cv.imread('../input/images-for-computer-vision/simple_shapes.png')

# Pixel value less than threshold becomes 0 and more than threshold becomes 255

_,img_threshold=cv.threshold(img,150,255,cv.THRESH_BINARY)

plot_3d(img, img_threshold, ['Original Image', 'Threshold Image=150'])

應(yīng)用閾值后,150 的值變?yōu)榈扔?255

過濾: 圖像過濾是通過改變像素的值來改變圖像的外觀。每種類型的過濾器都會(huì)根據(jù)相應(yīng)的數(shù)學(xué)公式更改像素值。我不會(huì)在這里詳細(xì)介紹數(shù)學(xué),但我將通過在 3D 中可視化它們來展示每個(gè)過濾器的工作原理。

limg=cv.imread('../input/images-for-computer-vision/simple_shapes.png')


# Gaussian Filter

ksize=(11,11) # Both should be odd numbers

img_guassian=cv.GaussianBlur(img, ksize,0)

plot_3d(img, img_guassian, ['Original Image','Guassian Image'])

# Median Filter

ksize=11

img_medianblur=cv.medianBlur(img,ksize)

plot_3d(img, img_medianblur, ['Original Image','Median blur'])

# Bilateral Filter

img_bilateralblur=cv.bilateralFilter(img,d=5, sigmaColor=50, sigmaSpace=5)

myplot([img, img_bilateralblur],['Original Image', 'Bilateral blur Image'])

plot_3d(img, img_bilateralblur, ['Original Image','Bilateral blur'])

高斯濾波器:通過去除細(xì)節(jié)和噪聲來模糊圖像。

中值濾波器:非線性過程可用于減少脈沖噪聲或椒鹽噪聲

雙邊濾波器:邊緣保留和降噪平滑。

簡(jiǎn)單來說,過濾器有助于減少或去除亮度或顏色隨機(jī)變化的噪聲,這稱為平滑

特征檢測(cè)

特征檢測(cè)是一種通過計(jì)算圖像信息的抽象,在每個(gè)圖像點(diǎn)上做出局部決策的方法。例如,對(duì)于一張臉的圖像,特征是眼睛、鼻子、嘴唇、耳朵等,我們嘗試識(shí)別這些特征。

讓我們首先嘗試識(shí)別圖像的邊緣。

邊緣檢測(cè)

img=cv.imread('../input/images-for-computer-vision/simple_shapes.png')

img_canny1=cv.Canny(img,50, 200)

# Smoothing the img before feeding it to canny

filter_img=cv.GaussianBlur(img, (7,7), 0)

img_canny2=cv.Canny(filter_img,50, 200)

myplot([img, img_canny1, img_canny2],

      ['Original Image', 'Canny Edge Detector(Without Smoothing)', 'Canny Edge Detector(With Smoothing)'])

這里我們使用 Canny 邊緣檢測(cè)器,它是一種邊緣檢測(cè)算子,它使用多階段算法來檢測(cè)圖像中的各種邊緣。它由 John F. Canny 于 1986 年開發(fā)。我不會(huì)詳細(xì)介紹 Canny 的工作原理,但這里的關(guān)鍵點(diǎn)是它用于提取邊緣。

在使用 Canny 邊緣檢測(cè)方法檢測(cè)邊緣之前,我們平滑圖像以去除噪聲。正如你從圖像中看到的,平滑后我們得到清晰的邊緣。

輪廓

img=cv.imread('../input/images-for-computer-vision/simple_shapes.png')

img_copy=img.copy()

img_gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)

_,img_binary=cv.threshold(img_gray,50,200,cv.THRESH_BINARY)

#Edroing and Dilating for smooth contours

img_binary_erode=cv.erode(img_binary,(10,10), iterations=5)

img_binary_dilate=cv.dilate(img_binary,(10,10), iterations=5)

contours,hierarchy=cv.findContours(img_binary,cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

cv.drawContours(img, contours,-1,(0,0,255),3) # Draws the contours on the original image just like draw function

myplot([img_copy, img], ['Original Image', 'Contours in the Image'])

侵蝕,使用用于探測(cè)和降低包含在圖像中的形狀的結(jié)構(gòu)元素的侵蝕操作。

膨脹:將像素添加到圖像中對(duì)象的邊界,與侵蝕相反

Hull

simg=cv.imread('../input/images-for-computer-vision/simple_shapes.png',0)

_,threshold=cv.threshold(img,50,255,cv.THRESH_BINARY)

contours,hierarchy=cv.findContours(threshold,cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

hulls=[cv.convexHull(c) for c in contours]

img_h(yuǎn)ull=cv.drawContours(img, hulls,-1,(0,0,255),2) #Draws the contours on the original image just like draw function

plt.imshow(img)

總結(jié)

我們看到了如何讀取和顯示圖像、在圖像上繪制形狀、文本、混合兩個(gè)圖像、旋轉(zhuǎn)、縮放、平移等變換圖像,使用高斯模糊、中值模糊、雙邊模糊過濾圖像,以及檢測(cè)使用 Canny 邊緣檢測(cè)和在圖像中查找輪廓的特征。


       原文標(biāo)題 : 2022最新計(jì)算機(jī)視覺學(xué)習(xí)路線(入門篇)

<上一頁  1  2  
聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無評(píng)論

暫無評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)