訂閱
糾錯
加入自媒體

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

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

什么是 OpenCV?

OpenCV 是一個主要針對計算機(jī)視覺的庫。它擁有你在使用計算機(jī)視覺 (CV) 時所需的所有工具!癘pen”代表開源,“CV”代表計算機(jī)視覺。

我會學(xué)到什么?

本文包含使用 OpenCV 庫開始使用計算機(jī)視覺所需的全部內(nèi)容。你會在計算機(jī)視覺方面感到更加自信和高效。

讀取和顯示圖像

首先讓我們了解如何讀取圖像并顯示它,這是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ù)字進(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'])

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

1  2  下一頁>  
聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表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號