python独立图形_如何检测图像中的独立图形?

对图像中的数字进行分割和检测,其主要思想如下:使用cv2.cvtColor()将图像转换为灰度

使用cv2.GaussianBlur()模糊图像

使用cv2.Canny()查找边

使用cv2.findContours()查找轮廓

迭代每个轮廓

{cd5>使用边界^获得矩形

用Numpy切片法找到每个轮廓的ROI

使用cv2.Rectangle()绘制边界框矩形

模糊的

igAqN.png

Canny边缘检测

vC8OC.png

检测到的轮廓

9aaK3.png

输出contours detected: 2import numpy as np

import cv2

original_image = cv2.imread("1.png")

image = original_image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (3, 3), 0)

canny = cv2.Canny(blurred, 120, 255, 1)

cv2.imshow("canny", canny)

# Find contours in the image

cnts = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

contours = []

for c in cnts:

# Obtain bounding rectangle for each contour

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

# Find ROI of the contour

roi = image[y:y+h, x:x+w]

# Draw bounding box rectangle

cv2.rectangle(original_image,(x,y),(x+w,y+h),(0,255,0),3)

contours.append(c)

cv2.imshow("detected", original_image)

print('contours detected: {}'.format(len(contours)))

cv2.waitKey(0)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值