您现在的位置:首页 > 课程体系 > 大数据与人工智能 > 机器学习-深度学习-图像处理
基于OpenCV计算机视觉识别程序

OpenCV是用于计算机视觉、机器学习和图像处理的大型开源库,现在它在实时操作中发挥着重要作用,这在当今的系统中非常重要。通过使用它,人们可以处理图像和视频来识别物体、人脸,甚至人类的笔迹。本文的重点是检测对象。

 

有关详细信息,请参阅中培IT学院机器学习、深度学习、计算机图像处理及知识图谱应用与核心技术实战课程中对OpenCV的讲解。

 

目标检测

目标检测是一种与计算机视觉、图像处理和深度学习相关的计算机技术,用于检测图像和视频中的对象实例。在本文中,我们将使用称为haar级联的东西来进行对象检测。

Haar级联分类器                     

Haar级联分类器是一种有效的目标检测方法。该方法由Paul Viola和Michael Jones在他们的论文《使用简单特征的提升级联快速目标检测》中提出。Haar Cascade是一种基于机器学习的方法,其中使用大量正负图像来训练分类器。

正图像–这些图像包含我们希望分类器识别的图像。

Negative Images–其他一切的图像,不包含我们要检测的对象。

下载以下需求的步骤

在终端中运行以下命令以安装opencv。

Pip install opencv python

运行以下命令以在终端中安装Matplotlib。

Pip  matplotlib

注意:将XML文件和PNG图像放在与Python脚本相同的文件夹中。

 

具体实施

使用的图像:

 

 

1)打开图像

Python代码:

import cv2

from matplotlib import pyplot as plt

   

# Opening image

img = cv2.imread("image.jpg")

   

# OpenCV opens images as BRG

# but we want it as RGB and

# we also need a grayscale

# version

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

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

   

# Creates the environment

# of the picture and shows it

plt.subplot(1, 1, 1)

plt.imshow(img_rgb)

plt.show()

 

输出:

 

2)识别

我们将使用OpenCV的detectMultiScale()函数来识别大符号和小符号:

 

 

# Use minSize because for not

# bothering with extra-small

# dots that would look like STOP signs

stop_data = cv2.CascadeClassifier('stop_data.xml')

found = stop_data.detectMultiScale(img_gray,

                                   minSize =(20, 20))

# Don't do anything if there's

# no sign

amount_found = len(found)

     

if amount_found != 0:

       

    # There may be more than one

    # sign in the image

    for (x, y, width, height) in found:

           

        # We draw a green rectangle around

        # every recognized sign

        cv2.rectangle(img_rgb, (x, y),

                      (x + height, y + width),

                      (0, 255, 0), 5)

 

下面是完整脚本:

import cv2

from matplotlib import pyplot as plt

   

# Opening image

img = cv2.imread("image.jpg")

   

# OpenCV opens images as BRG

# but we want it as RGB We'll

# also need a grayscale version

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

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

   

   

# Use minSize because for not

# bothering with extra-small

# dots that would look like STOP signs

stop_data = cv2.CascadeClassifier('stop_data.xml')

   

found = stop_data.detectMultiScale(img_gray,

                                   minSize =(20, 20))

   

# Don't do anything if there's

# no sign

amount_found = len(found)

   

if amount_found != 0:

       

    # There may be more than one

    # sign in the image

    for (x, y, width, height) in found:

           

        # We draw a green rectangle around

        # every recognized sign

        cv2.rectangle(img_rgb, (x, y),

                      (x + height, y + width),

                      (0, 255, 0), 5)

           

# Creates the environment of

# the picture and shows it

plt.subplot(1, 1, 1)

plt.imshow(img_rgb)

plt.show()

 

输出:


 

无论你是在准备你的第一次求职面试,还是在这个不断发展的科技景观中追求更高的技能,中培IT学院机器学习、深度学习、计算机图像处理及知识图谱应用与核心技术实战课程将是是你成功的关键。本次培训从实战的角度对深度学习技术进行了全面的剖析,并结合实际案例分析和探讨深度学习的应用场景,给深度学习相关从业人员以指导和启迪。中培IT学院累计培训10万+人次,期待您的加入。

[1]

 
网络安全热度最高的6本证书...
系统分析师VS系统架构设计...
项目经理考NPDP还是软考高...
盘点五个IT领域下证快的证...
CBA与TOGAF:探寻企业架构...
【收藏】软考电子证书下载...
项目经理任选两本证书,年...
DAMA中国推出“一考两证”...
数据分析具体指的是什么,...
数据分析师需要具备什么数...
CDA认证带你了解数据分析的...
敏捷与DevOps协同工作的注...
DevOps自动化测试的注意事...
DevOps五个好用的工具列表...
IT项目管理实现落地有哪些...
IT项目需求分析重点是建立...


中培IT学院 Copyright@2006-2024  北京中培伟业管理咨询有限公司.ALL Rights Reseved 备案号:京ICP备13024721号-2