Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

作者:南洲. 时间:2021-03-27 05:34:56 

1、概述

经常用到轮廓查找和多边形拟合等opencv操作,因此记录以备后续使用。本文代码中的阈值条件对图片没有实际意义,仅仅是为了测试。

原图为:

Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

2、测试代码:


import cv2
import numpy as np

img = cv2.imread('/home/yasin/coffe.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

_, contours, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img, contours, -1, (255, 0, 255), 1)
cv2.namedWindow('Result of drawContours', 0)
cv2.imshow('Result of drawContours', img)
cv2.waitKey()

cnt = 0
for i in range(len(contours)):
arclen = cv2.arcLength(contours[i], True)
epsilon = max(3, int(arclen * 0.02)) # 拟合出的多边形与原轮廓最大距离,可以自己设置,这里根据轮廓周长动态设置
approx = cv2.approxPolyDP(contours[i], epsilon, False) # 轮廓的多边形拟合
area = cv2.contourArea(contours[i]) # 计算面积
rect = cv2.minAreaRect(contours[i])
box = np.int0(cv2.boxPoints(rect)) # 计算最小外接矩形顶点
h = int(rect[1][0])
w = int(rect[1][1])
if min(h, w) == 0:
ration = 0
else:
ration = max(h,w) /min(h,w) # 长宽比

# 对长宽比,轮廓面积,拟合出的多边形顶点数做筛选
if ration < 10 and area > 20 and area < 4000 and approx.shape[0] > 3 :
# 对满足条件的轮廓画出轮廓的拟合多边形
cv2.polylines(img, [approx], True, (0, 255, 0), 1)

cv2.namedWindow('Result of filtered', 0)
cv2.imshow('Result of filtered', img)
cv2.waitKey()

画出的所有轮廓:

Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

在原轮廓基础上画出筛选后的轮廓(绿色部分,没有实际意义):

Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

补充知识:OpenCV python 轮廓(连通域)最小外接圆形

原图:[cc.jpg]

Python实现图片查找轮廓、多边形拟合、最小外接矩形代码


import cv2
import numpy as np

def main():

# 1.导入图片
 img_src = cv2.imread("cc.jpg")

# 2.灰度化,二值化
 img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
 ret, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)

# 3.连通域分析
 img_bin, contours, hierarchy = cv2.findContours(img_bin,
                cv2.RETR_LIST,
                cv2.CHAIN_APPROX_SIMPLE)

# 4.获取最小外接圆 圆心 半径
 center, radius = cv2.minEnclosingCircle(contours[0])
 center = np.int0(center)

# 5.绘制最小外接圆
 img_result = img_src.copy()
 cv2.circle(img_result, tuple(center), int(radius), (255, 255, 255), 2)

# 6.显示结果图片
 cv2.imshow("img_src", img_src)
 cv2.imshow("img_result", img_result)

cv2.waitKey()
 cv2.destroyAllWindows()

if __name__ == '__main__':
 main()

处理结果:[img_sult.jpg]

Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

来源:https://blog.csdn.net/zhou4411781/article/details/97632808

标签:Python,图片,轮廓,多边形,矩形
0
投稿

猜你喜欢

  • 使用 prometheus python 库编写自定义指标的方法(完整代码)

    2021-03-03 06:03:58
  • Python高级特性 切片 迭代解析

    2022-03-28 14:32:49
  • Python松散正则表达式用法分析

    2021-03-12 02:51:36
  • pycharm 批量修改变量名称的方法

    2021-09-07 22:48:29
  • python中的五种异常处理机制介绍

    2022-05-05 13:02:01
  • Go获取与设置环境变量的方法详解

    2023-06-21 04:35:10
  • 超简单的Python HTTP服务

    2023-01-22 06:19:15
  • 一段ASP的HTTP_REFERER判断代码

    2011-03-25 10:41:00
  • python每天定时运行某程序代码

    2023-11-03 07:03:52
  • python程序 线程队列queue使用方法解析

    2021-11-16 18:43:02
  • asp如何做一个树状展开视图来显示自己的记录结构?

    2010-07-12 18:56:00
  • Python数据正态性检验实现过程

    2022-07-10 15:46:14
  • python使用pymongo操作mongo的完整步骤

    2023-07-12 20:31:21
  • 如何利用PyQt5美化你的GUI界面

    2023-06-18 19:58:55
  • 浅谈Python描述数据结构之KMP篇

    2022-06-09 19:36:23
  • numpy.insert用法及内插插0的方法

    2023-03-28 10:06:13
  • Python imageio读取视频并进行编解码详解

    2021-02-20 07:32:53
  • Python脚本提取fasta文件单序列信息实现

    2023-03-06 12:55:02
  • Python去除字符串前后空格的几种方法

    2021-12-22 02:10:02
  • 在php7中MongoDB实现模糊查询的方法详解

    2023-09-04 12:30:00
  • asp之家 网络编程 m.aspxhome.com