脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - OpenCV图像轮廓的绘制方法

OpenCV图像轮廓的绘制方法

2021-12-22 00:02404_久夏青 Python

这篇文章主要为大家详细介绍了OpenCV图像轮廓的绘制方法,以及测试几何图形、花朵图形轮廓,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了检测几何图形轮廓和检测花朵图形轮廓,供大家参考,具体内容如下

opencv绘制图像轮廓

绘制轮廓的一般步骤:

1、读取图像

?
1
image = cv2.imread('image_path')

2、将原图转化为灰度图像

?
1
image_gray  = cv.cvtcolor(image, cv.color_bgr2gray)

3、将灰度图像进行二值化阈值处理

?
1
2
# 这里将阈值设置为127为例,最大阈值为255
t, binary = cv.threshold(image_gray, 127, 255, cv.thresh_binary)

4、检测二值化图像中边缘轮廓

?
1
2
# 这里以检测所有轮廓,不建立层次关系为例
contours, hierarchy = cv2.findcontours(binary, cv2.retr_list, cv2.chain_approx_none)

5、在原图上绘制图像

?
1
2
# 这里将轮廓索引设置为-1,绘制出所有轮廓,颜色设置为红色,宽度为2为例
cv2.drawcontours(image, contours, -1, (0, 0, 255), 2)

6、显示图像

?
1
cv2.imshow('image', image)

测试检测几何图形轮廓:

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# @time    : 2021/8/17
# @author  : zyh
"""
introduction:
图像轮廓检测:
    opencv提供的findcontours()方法可以通过计算图像梯度来判断出图像的边缘,然后将边缘的点封装成数组返回
    contours, hierarchy = cv2.findcontours(image, mode, mothode)
    参数:
        image->被检测图像必须是8位单通道二值图像。如果原图是rgb图像,必须转为灰度图像,在进行二值化阈值处理
        mode->轮廓的检索模式
            参数值                     含义
            cv2.retr_external       只检测外轮廓
            cv2.retr_list           检测所有轮廓,但不建立层次关系
            cv2.retr_ccomp          检测所有轮廓,并建立两级层次关系
            cv2.retr_tree           检测所有轮廓,并建立树状结构的层次关系
        mothode->检测轮廓时使用的方法
            参数值                     含义
            cv2.chain_none                储存轮廓上的所有点
            cv2.chain_approx_simple       只保存水平、垂直或对角线轮廓的端点
            cv2.chain_approx_tc89_l1      ten_chinl近似算法的一种
            cv2.chain_approx_tc89_kcos    ten_chinl近似算法的一种
    retval:
        contours->检测出的所有轮廓,list类型,每一个元素都是某个轮廓的像素坐标数组
        hierarchy->轮廓之间的层次关系
图像轮廓绘制:
    image = cv2.drawcontours(image, contours, contouridx, color, thickness, linetypee, hierarchy,
        maxlevel, offse)
    参数:
        image->被绘制轮廓的原始图像,可以是多通道图像
        contours->findcontours()方法得出的轮廓列表
        contouridx->绘制轮廓的索引,如果为-1则绘制所有轮廓
        color:绘制颜色,bgr格式
        thickness->可选参数,画笔的粗细,如果为-1则绘制实心轮廓
        linetypee->可选参数,绘制轮廓的线型
        hierarchy->可选参数,findcontours()方法得出的层次关系
        maxlevel->可选参数,绘制轮廓的层次深度,最深绘制第maxlevel层
        offse->可选参数,偏移量,可以改变绘制结果的位置
"""
import cv2 as cv
# 读取加载图像
image1 = cv.imread('shape1.png')
image2 = cv.imread('shape1.png')
# 将图像由rgb格式转为灰度图像
gray1 = cv.cvtcolor(image1, cv.color_bgr2gray)
gray2 = cv.cvtcolor(image2, cv.color_bgr2gray)
# 将图像进行二值化阈值处理, 返回t是处理时采用的阈值,binary是阈值处理后的图像
t1, binary1 = cv.threshold(gray1, 127, 255, cv.thresh_binary)
t2, binary2 = cv.threshold(gray2, 127, 255, cv.thresh_binary)
# 检测图像中出现的所有轮廓,记录轮廓的每一个点
contours1, hierarchy1 = cv.findcontours(binary1, cv.retr_list, cv.chain_approx_none)
# 显示原图
cv.imshow('image', image1)
# 绘制所有轮廓,宽度为3,颜色为红色
cv.drawcontours(image1, contours1, -1, (0, 0, 255), 3)
cv.imshow('cv.retr_list', image1)
 
# 检测图像中的外轮廓,记录轮廓的每一个点
contours2, hierarchy2 = cv.findcontours(binary2, cv.retr_external, cv.chain_approx_none)
# 使用cv2.retr_external做参数绘制外轮廓,宽度为3,颜色为蓝色
cv.drawcontours(image2, contours2, -1, (255, 0, 0), 3)
cv.imshow('cv.retr_external', image2)
 
cv.waitkey()
cv.destroyallwindows()

运行结果:

OpenCV图像轮廓的绘制方法

测试检测花朵图形轮廓:

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
# @time    : 2021/8/18
# @author  : zyh
"""
introduction:
绘制花朵所有轮廓
"""
import  cv2 as cv
 
image_flower = cv.imread('flower.png')
# 显示原图
cv.imshow('flower1', image_flower)
# 对图像进行中值滤波处理,去除噪声
image_flower = cv.medianblur(image_flower, 5)
cv.imshow('flower2', image_flower)
# 将图像从rgb转为单通道灰度图像
gray_flower = cv.cvtcolor(image_flower, cv.color_bgr2gray)
# 灰度图像进行二值化阈值处理
t, binary = cv.threshold(gray_flower, 127, 255, cv.thresh_binary)
# 显示二值化图像
cv.imshow('binary', binary)
# 获取二值化图像中的轮廓以及轮廓层次
contours, hierarchy = cv.findcontours(binary, cv.retr_list, cv.chain_approx_none)
# 在原图中绘制轮廓
cv.drawcontours(image_flower, contours, -1, (0, 255, 255), 2)
# 显示绘制轮廓后的图像
cv.imshow('cv.retr_list', image_flower)
 
cv.waitkey()
cv.destroyallwindows()

运行结果:

OpenCV图像轮廓的绘制方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_53703628/article/details/119779014

延伸 · 阅读

精彩推荐