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

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

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

什么是 OpenCV?

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

讀取和顯示圖像

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

讀取圖像:

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 個(gè)通道。原始圖像是 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 混合兩個(gè)或多個(gè)圖像。圖像只不過(guò)是數(shù)字,你可以對(duì)數(shù)字進(jìn)行加、減、乘、除運(yùn)算,從而得到圖像。需要注意的一件事是圖像的大小應(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'])

乘法圖像幾乎為白色,分割圖像為黑色,這是因?yàn)榘咨硎?55,黑色表示0。當(dāng)我們將圖像的兩個(gè)像素值相乘時(shí),我們得到的數(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

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

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

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

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

雙邊濾波器:邊緣保留和降噪平滑。簡(jiǎn)單來(lái)說(shuō),過(guò)濾器有助于減少或去除亮度或顏色隨機(jī)變化的噪聲,這稱為平滑。

特征檢測(cè)

特征檢測(cè)是一種通過(guò)計(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è)算子,它使用多階段算法來(lái)檢測(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ì)象的邊界,與侵蝕相反

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

聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wè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)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

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

暫無(wú)評(píng)論

暫無(wú)評(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)