图像边检检测定义,边缘提取

本文详细介绍使用OpenCV和scikit-image进行图像轮廓检测和边缘提取的方法,包括查找图像轮廓函数、绘制图像轮廓函数的应用,以及逼近多边形曲线的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/sundanping_123/article/details/86509536

https://blog.youkuaiyun.com/abcjennifer/article/details/7427033(边缘特征提取方法)

图像轮廓并不是图像边缘。
边缘检测可能是断断续续的点 ,把这些点连起来才是轮廓。
注意问题:

1. 查找图像轮廓函数
 

 

mode有四种,只要是检测外轮廓1和检测所有轮廓234 。最常用4 。
在这里插入图片描述 

method也有四种,重点关注前两点。
在这里插入图片描述 

2. 绘制图像轮廓函数

在这里插入图片描述
在这里插入图片描述

(B G R)三个数值的范围均为0-255
(255,0,0)蓝色 (0,255,0)绿色(0,0,255)红色

可以绘制全部,也可以选定。颜色(G B R)格式,粗度随意。需要注意,绘制轮廓会改变原图像,必须复制一份。且原图片必须二值化才能寻找边缘。

import cv2
o=cv2.imread('image\\contours.bmp')
gray=cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)    #边缘检测处理的是二值图片。故先将BGR转为灰度,再二值化。
ret,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)     #二值阈值化
image,contours,hierarchy=cv2.findContours(binary,
                                          cv2.RETR_TREE,
                                          cv2.CHAIN_APPROX_SIMPLE)
co=o.copy()                              #会改变原图片,所以通常复制一份
co1=o.copy()
r=cv2.drawContours(co,contours,-1,(0,255,0),2)   # -1代表显示所有边缘  颜色(0,255,0) 粗度6
r1=cv2.drawContours(co1,contours,0,(255,0,0),6)   #绘制第0个边缘   颜色(255,0,0) 粗度16
cv2.imshow('original',o)
cv2.imshow('r',r)
cv2.imshow('r1',r1)
cv2.waitKey()
cv2.destroyAllWindows()


 

https://www.cnblogs.com/denny402/p/5160955.html

本篇我们讲解一些其它方法来检测轮廓。

1、查找轮廓(find_contours)

measure模块中的find_contours()函数,可用来检测二值图像的边缘轮廓。

函数原型为:

skimage.measure.find_contours(arraylevel)

array: 一个二值数组图像

level: 在图像中查找轮廓的级别值

返回轮廓列表集合,可用for循环取出每一条轮廓。

例1:

复制代码

import numpy as np
import matplotlib.pyplot as plt
from skimage import measure,draw 

#生成二值测试图像
img=np.zeros([100,100])
img[20:40,60:80]=1  #矩形
rr,cc=draw.circle(60,60,10)  #小圆
rr1,cc1=draw.circle(20,30,15) #大圆
img[rr,cc]=1
img[rr1,cc1]=1

#检测所有图形的轮廓
contours = measure.find_contours(img, 0.5)

#绘制轮廓
fig, (ax0,ax1) = plt.subplots(1,2,figsize=(8,8))
ax0.imshow(img,plt.cm.gray)
ax1.imshow(img,plt.cm.gray)
for n, contour in enumerate(contours):
    ax1.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax1.axis('image')
ax1.set_xticks([])
ax1.set_yticks([])
plt.show()

复制代码

结果如下:不同的轮廓用不同的颜色显示

例2:

复制代码

import matplotlib.pyplot as plt
from skimage import measure,data,color

#生成二值测试图像
img=color.rgb2gray(data.horse())

#检测所有图形的轮廓
contours = measure.find_contours(img, 0.5)

#绘制轮廓
fig, axes = plt.subplots(1,2,figsize=(8,8))
ax0, ax1= axes.ravel()
ax0.imshow(img,plt.cm.gray)
ax0.set_title('original image')

rows,cols=img.shape
ax1.axis([0,rows,cols,0])
for n, contour in enumerate(contours):
    ax1.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax1.axis('image')
ax1.set_title('contours')
plt.show()

复制代码

 

2、逼近多边形曲线

逼近多边形曲线有两个函数:subdivide_polygon()和 approximate_polygon()

subdivide_polygon()采用B样条(B-Splines)来细分多边形的曲线,该曲线通常在凸包线的内部。

函数格式为:

skimage.measure.subdivide_polygon(coordsdegree=2preserve_ends=False)

coords: 坐标点序列。

degree: B样条的度数,默认为2

preserve_ends: 如果曲线为非闭合曲线,是否保存开始和结束点坐标,默认为false

返回细分为的坐标点序列。

approximate_polygon()是基于Douglas-Peucker算法的一种近似曲线模拟。它根据指定的容忍值来近似一条多边形曲线链,该曲线也在凸包线的内部。

函数格式为:

skimage.measure.approximate_polygon(coordstolerance)

coords: 坐标点序列

tolerance: 容忍值

返回近似的多边形曲线坐标序列。

例:

复制代码

import numpy as np
import matplotlib.pyplot as plt
from skimage import measure,data,color

#生成二值测试图像
hand = np.array([[1.64516129, 1.16145833],
                 [1.64516129, 1.59375],
                 [1.35080645, 1.921875],
                 [1.375, 2.18229167],
                 [1.68548387, 1.9375],
                 [1.60887097, 2.55208333],
                 [1.68548387, 2.69791667],
                 [1.76209677, 2.56770833],
                 [1.83064516, 1.97395833],
                 [1.89516129, 2.75],
                 [1.9516129, 2.84895833],
                 [2.01209677, 2.76041667],
                 [1.99193548, 1.99479167],
                 [2.11290323, 2.63020833],
                 [2.2016129, 2.734375],
                 [2.25403226, 2.60416667],
                 [2.14919355, 1.953125],
                 [2.30645161, 2.36979167],
                 [2.39112903, 2.36979167],
                 [2.41532258, 2.1875],
                 [2.1733871, 1.703125],
                 [2.07782258, 1.16666667]])

#检测所有图形的轮廓
new_hand = hand.copy()
for _ in range(5):
    new_hand =measure.subdivide_polygon(new_hand, degree=2)

# approximate subdivided polygon with Douglas-Peucker algorithm
appr_hand =measure.approximate_polygon(new_hand, tolerance=0.02)

print("Number of coordinates:", len(hand), len(new_hand), len(appr_hand))

fig, axes= plt.subplots(2,2, figsize=(9, 8))
ax0,ax1,ax2,ax3=axes.ravel()

ax0.plot(hand[:, 0], hand[:, 1],'r')
ax0.set_title('original hand')
ax1.plot(new_hand[:, 0], new_hand[:, 1],'g')
ax1.set_title('subdivide_polygon')
ax2.plot(appr_hand[:, 0], appr_hand[:, 1],'b')
ax2.set_title('approximate_polygon')

ax3.plot(hand[:, 0], hand[:, 1],'r')
ax3.plot(new_hand[:, 0], new_hand[:, 1],'g')
ax3.plot(appr_hand[:, 0], appr_hand[:, 1],'b')
ax3.set_title('all')

复制代码

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值