訂閱
糾錯
加入自媒體

什么是 OpenCV?計算機視覺基本任務(wù)入門

如果你有興趣或計劃做與圖像或視頻相關(guān)的事情,你絕對應(yīng)該考慮使用計算機視覺。計算機視覺 (CV) 是人工智能 (AI) 的一個分支,它使計算機能夠從圖像、視頻和其他視覺輸入中提取有意義的信息,并采取必要的行動。例如自動駕駛汽車、自動交通管理、監(jiān)控、基于圖像的質(zhì)量檢查等等。

什么是 OpenCV?

OpenCV 是一個主要針對計算機視覺的庫。它擁有你在使用計算機視覺 (CV) 時所需的所有工具!癘pen”代表開源,“CV”代表計算機視覺。我會學(xué)到什么?本文包含使用 OpenCV 庫開始使用計算機視覺所需的全部內(nèi)容。你會在計算機視覺方面感到更加自信和高效。

讀取和顯示圖像

首先讓我們了解如何讀取圖像并顯示它,這是CV的基礎(chǔ)知識。

讀取圖像:

import numpy as np

import cv2 as cv

import matplotlib.pyplot as plt

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

'img' 包含 numpy 數(shù)組形式的圖像。讓我們打印它的類型和形狀

print(type(img))

print(img.shape)

numpy 數(shù)組的形狀為 (667, 1200, 3),其中,

667 – 圖像高度,1200 – 圖像寬度,3 – 通道數(shù),

在這種情況下,有 RGB 通道,所以我們有 3 個通道。原始圖像是 RGB 的形式,但 OpenCV 默認(rèn)將圖像讀取為 BGR,因此我們必須在顯示之前將其轉(zhuǎn)換回RGB

顯示圖像:

# Converting image from BGR to RGB for displaying

img_convert=cv.cvtColor(img, cv.COLOR_BGR2RGB)

plt.imshow(img_convert)

在圖像上繪圖

我們可以繪制線條、形狀和文本圖像。

# Rectangle

color=(240,150,240) # Color of the rectangle

cv.rectangle(img, (100,100),(300,300),color,thickness=10, lineType=8) ## For filled rectangle, use thickness = -1

## (100,100) are (x,y) coordinates for the top left point of the rectangle and (300, 300) are (x,y) coordinates for the bottom right point

# Circle

color=(150,260,50)

cv.circle(img, (650,350),100, color,thickness=10) ## For filled circle, use thickness = -1

## (250, 250) are (x,y) coordinates for the center of the circle and 100 is the radius

# Text

color=(50,200,100)

font=cv.FONT_HERSHEY_SCRIPT_COMPLEX

cv.putText(img, 'Save Tigers',(200,150), font, 5, color,thickness=5, lineType=20)

# Converting BGR to RGB

img_convert=cv.cvtColor(img, cv.COLOR_BGR2RGB)

plt.imshow(img_convert)

混合圖像

我們還可以使用 OpenCV 混合兩個或多個圖像。圖像只不過是數(shù)字,你可以對數(shù)字進行加、減、乘、除運算,從而得到圖像。需要注意的一件事是圖像的大小應(yīng)該相同。

# For plotting multiple images at once

def myplot(images,titles):
          fig, axs=plt.subplots(1,len(images),sharey=True)
          fig.set_figwidth(15)
          for img,ax,title in zip(images,axs,titles):
               if img.shape[-1]==3:
                   img=cv.cvtColor(img, cv.COLOR_BGR2RGB) # OpenCV reads images as BGR, so converting back them to RGB
               else:
                   img=cv.cvtColor(img, cv.COLOR_GRAY2BGR)
               ax.imshow(img)
               ax.set_title(title)

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

img2 = cv.imread('../input/images-for-computer-vision/horse.jpg')

# Resizing the img1

img1_resize = cv.resize(img1, (img2.shape[1], img2.shape[0]))

# Adding, Subtracting, Multiplying and Dividing Images

img_add = cv.a(chǎn)dd(img1_resize, img2)

img_subtract = cv.subtract(img1_resize, img2)

img_multiply = cv.multiply(img1_resize, img2)

img_divide = cv.divide(img1_resize, img2)

# Blending Images

img_blend = cv.a(chǎn)ddWeighted(img1_resize, 0.3, img2, 0.7, 0) ## 30% tiger and 70% horse

myplot([img1_resize, img2], ['Tiger','Horse'])

myplot([img_add, img_subtract, img_multiply, img_divide, img_blend], ['Addition', 'Subtraction', 'Multiplication', Division', 'Blending'])

乘法圖像幾乎為白色,分割圖像為黑色,這是因為白色表示255,黑色表示0。當(dāng)我們將圖像的兩個像素值相乘時,我們得到的數(shù)字更大,因此其顏色變?yōu)榘咨蚪咏咨,與分割圖像相反。

圖像變換

圖像變換包括平移、旋轉(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

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

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é)和噪聲來模糊圖像。

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

雙邊濾波器:邊緣保留和降噪平滑。簡單來說,過濾器有助于減少或去除亮度或顏色隨機變化的噪聲,這稱為平滑。

特征檢測

特征檢測是一種通過計算圖像信息的抽象,在每個圖像點上做出局部決策的方法。例如,對于一張臉的圖像,特征是眼睛、鼻子、嘴唇、耳朵等,我們嘗試識別這些特征。讓我們首先嘗試識別圖像的邊緣。

邊緣檢測

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

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

輪廓

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'])

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

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

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

聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權(quán)或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內(nèi)容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

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

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