python数据去噪声_Python实现轮廓去噪声

这篇博客展示了如何使用Python的OpenCV库进行边缘检测和轮廓处理,以去除图像中的噪声。通过Canny算子获取边缘,然后利用boundingRect和minEnclosingCircle等方法进行形状识别和噪声过滤,主要针对矩形和圆形。示例代码包括了轮廓绘制、最小外接矩形和最小覆盖圆的计算过程。
摘要由CSDN通过智能技术生成

完整的代码,实现了最小的矩形,圆形,随意矩形

来源

https://zhuanlan.zhihu.com/p/38739563

import cv2

import numpy as np

"""

REFER: https://hub.packtpub.com/opencv-detecting-edges-lines-shapes/

2018-06-30 Yonv1943

2018-07-01 comment to test.png

2018-07-01 gray in threshold, hierarchy

2018-07-01 draw_approx_hull_polygon() no [for loop]

2018-11-24

"""

def draw_contours(img, cnts): # conts = contours

img = np.copy(img)

img = cv2.drawContours(img, cnts, -1, (0, 255, 0), 2)

return img

def draw_min_rect_circle(img, cnts): # conts = contours

# img = np.copy(img)

img = np.zeros(img.shape, dtype=np.uint8)

for cnt in cnts:

x, y, w, h = cv2.boundingRect(cnt)

# cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

if w * h <= 16:

print(1)

for i in range(1, w):

for j in range(1, h):

print(x, y)

print(i, j)

img[x + i, y + j] = [0, 0, 0]

# print("success")

# msg1 = "定点x,y为" + str(x) + ' ' + str(y)

# msg2 = "长宽w,h为" + str(w) + " " + str(h)<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值