轮廓检测及功能

一、实验介绍

  1. 实验内容

本实验将学习轮廓检测及功能。
实验要点

  1. 生成二进制图像来查找轮廓
  2. 找到并画出轮廓
  3. 轮廓特征
  4. 边界矩形

实验环境

Python 3.6.6
numpy
matplotlib
cv2

二、实验步骤

1 导入资源并显示图像

import numpy as np
import matplotlib.pyplot as plt
import cv2

%matplotlib inline

# 读入图像
image = cv2.imread('images/thumbs_up_down.jpg')

# 将颜色更改为RGB(从BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.imshow(image)
<matplotlib.image.AxesImage at 0x7feef4377160>

在这里插入图片描述

2 生成二进制图像来查找轮廓

# 转换为灰度
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

# 创建一个二进制阈值图像
retval, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)

plt.imshow(binary, cmap='gray')

<matplotlib.image.AxesImage at 0x7feea070c400>

在这里插入图片描述
3 找到并画出轮廓

# 从带阈值的二进制图像中查找轮廓

retval,contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 在原始图像的副本上绘制所有轮廓
contours_image = np.copy(image)
contours_image = cv2.drawContours(contours_image, contours, -1, (0,255,0), 3)

plt.imshow(contours_image)

<matplotlib.image.AxesImage at 0x7feea0676518>

在这里插入图片描述

三、实验任务

任务一:轮廓特征
每个轮廓都有许多可以计算的特征,包括轮廓的面积,它的方向(大部分轮廓指向的方向),它的周长,以及OpenCV documentation, here中概述的许多其他属性。

在下一个单元格中,要求标识左右轮廓的方向。明确手的方向,让你知道哪只手的拇指向上,哪只手的拇指向下!

方向:

对象的方向是对象指向的角度。 要找到轮廓的角度,首先应找到适合轮廓的椭圆,然后从该形状中提取角度。

# Fit an ellipse to a contour and extract the angle from that ellipse
(x,y), (MA,ma), angle = cv2.fitEllipse(selected_contour)

练习一: 找到每个轮廓的方向

## TODO: 完成此功能,以便
## 返回轮廓列表的方向
## 列表应与轮廓顺序相同
## 即第一个角度应该是第一个轮廓的方向
def orientations(contours):
    """
    方向 
    :参数轮廓: 轮廓列表
    :返回值: 角度,轮廓的方向
    """
    
    # 创建一个空列表以存储角度
    # 提示:使用angles.append(value)将值添加到此列表中
    angles = []
    for contour in contours:
        # 拟合椭圆
        ellipse = cv2.fitEllipse(contour)
        # 提取角度
        angle = ellipse[2]
        angles.append(angle)

    return angles


# ---------------------------------------------------------- #
# 打印方向值
angles = orientations(contours)
print('Angles of each contour (in degrees): ' + str(angles))
Angles of each contour (in degrees): [61.35833740234375, 82.27550506591797]

任务二:边界矩形
在下一个单元格中,系统将要求您在* left *手轮廓周围找到边界矩形,该轮廓已将其拇指向上,然后使用该边界矩形裁剪图像并更好地集中在那只手上!

# 查找选定轮廓的边界矩形
x,y,w,h = cv2.boundingRect(selected_contour)

# 将边界矩形绘制为紫色框
box_image = cv2.rectangle(contours_image, (x,y), (x+w,y+h), (200,0,200),2)

要裁剪图像,请选择要包含的图像的正确宽度和高度。

# 使用边界矩形(x,y,w,h)的尺寸进行裁剪
cropped_image = image[y: y + h, x: x + w] 

练习二: 围绕轮廓裁剪图像

## TODO: 完成此功能,以便
## 它会返回原始图像的新裁剪版本
def left_hand_crop(image, selected_contour):
    """
    Left hand crop 
    :参数图像:原始图像
    :参数selectec_contour:将用于裁剪的轮廓
    :返回值: cropped_image, 左手周围的裁剪图像
    """
    
    ## TODO: 检测左手轮廓的边界矩形
    
    x, y, w, h = cv2.boundingRect(selected_contour)
    box_image = cv2.rectangle(contours_image, (x,y), (x+w,y+h), (200,0,200),2)
    
    ## TODO: 使用边界矩形的尺寸裁剪图像
    # 复制图像进行裁剪
    
    cropped_image = np.copy(box_image)
    cropped_image = box_image[y: y + h, x: x + w]
    
    return cropped_image


## TODO: 从列表中选择左侧轮廓
## 替换此值
selected_contour = contours[1]


# ---------------------------------------------------------- #
# 如果选择了轮廓
if(selected_contour is not None):
    # 调用带有该轮廓的裁剪函数作为参数
    cropped_image = left_hand_crop(image, selected_contour)
    plt.imshow(cropped_image)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值