2022最新计算机视觉学习路线(入门篇)

磐创AI
关注

如果你有兴趣或计划做与图像或视频相关的事情,你绝对应该考虑使用计算机视觉。计算机视觉 (CV) 是人工智能 (AI) 的一个分支,它使计算机能够从图像、视频和其他视觉输入中提取有意义的信息,并采取必要的行动。例如自动驾驶汽车、自动交通管理、监控、基于图像的质量检查等等。

什么是 OpenCV?

OpenCV 是一个主要针对计算机视觉的库。它拥有你在使用计算机视觉 (CV) 时所需的所有工具。“Open”代表开源,“CV”代表计算机视觉。

我会学到什么?

本文包含使用 OpenCV 库开始使用计算机视觉所需的全部内容。你会在计算机视觉方面感到更加自信和高效。

读取和显示图像

首先让我们了解如何读取图像并显示它,这是CV的基础知识。读取图像

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 数组形式的图像。让我们打印它的类型和形状

print(type(img))

print(img.shape)

numpy 数组的形状为 (667, 1200, 3),其中,

667 – 图像高度,1200 – 图像宽度,3 – 通道数,

在这种情况下,有 RGB 通道,所以我们有 3 个通道。原始图像是 RGB 的形式,但 OpenCV 默认将图像读取为 BGR,因此我们必须在显示之前将其转换回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 混合两个或多个图像。图像只不过是数字,你可以对数字进行加、减、乘、除运算,从而得到图像。需要注意的一件事是图像的大小应该相同。

# 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.add(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.addWeighted(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。当我们将图像的两个像素值相乘时,我们得到的数字更大,因此其颜色变为白色或接近白色,与分割图像相反。

声明: 本文由入驻OFweek维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。
侵权投诉

下载OFweek,一手掌握高科技全行业资讯

还不是OFweek会员,马上注册
打开app,查看更多精彩资讯 >
  • 长按识别二维码
  • 进入OFweek阅读全文
长按图片进行保存