码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 图像轮廓opencv


     

    1. import cv2
    2. import numpy as np
    3. def drawShape(src, points):
    4. i = 0
    5. while i < len(points):
    6. if(i == len(points) - 1):
    7. x,y = points[i][0]
    8. x1,y1 = points[0][0]
    9. cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 3)
    10. else:
    11. x,y = points[i][0]
    12. x1,y1 = points[i+1][0]
    13. cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 3)
    14. i = i + 1
    15. #读文件
    16. #img = cv2.imread('./contours1.jpeg')
    17. #img = cv2.imread('./hand.jpeg')
    18. img = cv2.imread('./hello.jpeg')
    19. print(img.shape)
    20. #转变成单通道
    21. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    22. #二值化
    23. ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
    24. #轮廓查找
    25. contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    26. #绘制轮廓
    27. # cv2.drawContours(img, contours, 1, (0, 255, 0), 1)
    28. #计算面积
    29. # area = cv2.contourArea(contours[0])
    30. # print("area=%d"%(area))
    31. # #计算周长
    32. # len = cv2.arcLength(contours[0], False)
    33. # print("len=%d"%(len))
    34. # e = 5
    35. # approx = cv2.approxPolyDP(contours[1], e, True)
    36. # drawShape(img, approx)
    37. # hull = cv2.convexHull(contours[1])
    38. # drawShape(img, hull)
    39. r = cv2.minAreaRect(contours[1])
    40. box = cv2.boxPoints(r)
    41. box = np.int0(box)
    42. cv2.drawContours(img, [box], 0, (0,0, 255), 2)
    43. x,y,w,h = cv2.boundingRect(contours[1])
    44. cv2.rectangle(img, (x, y), (x+w,y+h), (255,0,0), 2)
    45. cv2.imshow('img', img)
    46. cv2.waitKey(0)

  • 相关阅读:
    为什么网络掩码一定是255.255.255.0(/24)
    Vite创建Vue2项目
    软件设计师(八)算法设计与分析
    模型分类model
    数据库管理和开发:Navicat 17 for MongoDB mac 激活版
    【开源】基于Vue.js的智能教学资源库系统
    Spring Boot:开启Java开发的新篇章
    【安卓】UI开发入门
    分享云安全实践,透视2022亚马逊云科技re:Inforce全球安全大会
    【C语言刷LeetCode】395. 至少有 K 个重复字符的最长子串(M)
  • 原文地址:https://blog.csdn.net/2201_75867204/article/details/134448673
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号