当前位置: 首页 > news >正文

python -opencv 边缘检测

python -opencv 边缘检测

边缘检测步骤:

第一步:读取图像为灰度图
第二步:进行二值化处理
第三步:使用cv2.findContours对二值化图像提取轮廓
第三步:将轮廓绘制到图中

代码如下:

from ctypes.wintypes import SIZE
from multiprocessing.pool import IMapUnorderedIterator
import cv2
import copy
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import ospath=r'D:\learn\photo\cv\res\ballon.png'img=cv2.imread(path,1)
img_gray=cv2.imread(path,0)def cv_show(name,img):cv2.imshow(name,img)#cv2.waitKey(0),接收0,表示窗口暂停cv2.waitKey(0)#销毁所有窗口cv2.destroyAllWindows()#cv_show('img_gray',img_gray)#进行二值化处理
ret,binary=cv2.threshold(img_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)#cv_show('dist',dist)def BGR_TO_RGB(img):return img[:,:, ::-1];
#检测轮廓countourClose,hierrachyclose=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)print("len(countourclose) is",len(countourClose))#countourClose 轮廓坐标信息
#hierrachyclose 轮廓之间的层次结构#背景图
result=np.zeros(img.shape,np.uint8)#绘制轮廓边框
for  i in range(len(countourClose)):cnt=countourClose[i]#近似多边形#第一个参数,表示精度,单位是像素#第二个像素表示是否闭合approx=cv2.approxPolyDP(cnt,3,True)#绘制轮廓#-1表示绘制所有轮廓#5线条粗细cv2.drawContours(result,[approx],-1,(0,0,255),5)#绘制矩形x,y,w,h=cv2.boundingRect(cnt)cv2.rectangle(result,(x,y),(x+w,y+h),(0,255,0),5)#绘制最小外接圆(x,y),r=cv2.minEnclosingCircle(cnt)center=(int(x),int(y))r=int(r)cv2.circle(result,center,r,(255,255,255),5)#plt.figure(figsize=(400,600))print(img_gray.shape)
print(img_gray[0][0])
plt.subplot(221)
#img_gray=BGR_TO_RGB(img_gray,'gray')
plt.imshow(img_gray,'gray')
plt.title('img_gray')plt.subplot(222)
plt.imshow(binary,'gray')
plt.title('binary')
plt.subplot(223)
plt.imshow(result,'gray')
plt.title('result')plt.show()
os.system("pause")

运行结果如下:

在这里插入图片描述

http://www.lryc.cn/news/238124.html

相关文章:

  • Hadoop-- hdfs
  • 《论文阅读》CAB:认知、情感和行为的共情对话生成 DASFAA 2023
  • 审计dvwa高难度命令执行漏洞的代码,编写实例说明如下函数的用法
  • 国科大数据挖掘期末复习——聚类分析
  • 【经验之谈·高频PCB电路设计常见的66个问题】
  • 科大讯飞 vue.js 语音听写流式实现 全网首发
  • 局域网文件共享神器:Landrop
  • 如何使用Docker部署Apache+Superset数据平台并远程访问?
  • 【阿里云】图像识别 摄像模块 语音模块
  • 一文读懂 Linux 网络 IO 模型
  • Arduino库之U8g2lib
  • fiddler 手机抓包
  • 基于知识问答的上下文学习中的代码风格11.20
  • opencv-形态学处理
  • 手把手设计C语言版循环队列(力扣622:设计循环队列)
  • 数据仓库及ETL的理论基础
  • 5-4计算一串字符的空格数字字符其他
  • leetcode面试经典150题——30 长度最小的子数组
  • 学习计划计划执行记录
  • 【紫光同创PCIE教程】——使用WinDriver驱动紫光PCIE
  • MT8735/MTK8735安卓核心板规格参数介绍
  • NSSCTF web刷题记录6
  • 米哈游大数据云原生实践
  • 移动端适配-(postcss-pxtorem)
  • 【PostgreSQL】解决PostgreSQL时区(TimeZone)问题
  • Vue Router的使用
  • 海外IP代理科普——API代理
  • 详解Python安装requests库的实例代码
  • Flutter 使用 device_info_plus 遇到的问题
  • 论文阅读:“基于特征检测与深度特征描述的点云粗对齐算法”