OpenCV基于(边缘检测)和(轮廓角点)实战获取嵌套轮廓得边缘角坐标

基于本人写的前两篇得内容:
https://blog.csdn.net/m0_51503599/article/details/120516411
https://blog.csdn.net/m0_51503599/article/details/120522003
添加一个新函数sorted

sorted(iterable, *, key=None, reverse=False)

sorted属于接收可迭代类型详情看python文档传送门
① iterable例如字典、列表、
② key=None可迭代类型中某个属性,依照这个元素的每一项进行排序
③ 降序或升序 bool Ture为降序 Flase为升序
代码:

import cv2
import numpy as np

webcam = False
path = '12.png'
filter=4
minArea=1000
while True:
    if webcam:success,img = cap.read()# 选择实时与否
    else: img = cv2.imread(path)

    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)
    imgCanny = cv2.Canny(imgBlur,100,100)
    kernel = np.ones((5,5))
    imgDial = cv2.dilate(imgCanny,kernel,iterations=3)
    imgThre = cv2.erode(imgDial,kernel,iterations=2)
    cv2.imshow('Canny',imgThre)
    contours,hiearchy = cv2.findContours(imgThre,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    finalCountours = []#创建最终列表
    for i in contours:
        area = cv2.contourArea(i)
        if area > minArea:
            peri = cv2.arcLength(i,True)
            approx = cv2.approxPolyDP(i,0.02*peri,True)
            bbox = cv2.boundingRect(approx)#左上角得坐标以及宽和高即得到边框
            if filter > 0:
                if len(approx) == filter:
                    finalCountours.append([len(approx), area, approx, bbox, i])
            else:
                finalCountours.append([len(approx), area, approx, bbox, i])
        finalCountours = sorted(finalCountours, key=lambda x: x[2], reverse=True)#依照面积排轮廓最大轮廓到最小
        for con in finalCountours:
            cv2.drawContours(img, con[4], -1, (0, 0, 255), 3)#con[4]是bbx
            cv2.imshow("1",img)
            # print(bbox)
            # print(len(finalCountours))
            biggest = finalCountours[0][2]#已经降序排列过了[0]为最大轮廓,即最大轮廓得四个角的坐标print(approx)
            # print(biggest)
            print(approx)

原图:
在这里插入图片描述
结果:
在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值