栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python OpenCV查找图像中的极端外部点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python OpenCV查找图像中的极端外部点

这是一种潜在的方法:

  • 将图像转换为灰度和高斯模糊
  • 获取二进制图像的阈值
  • 查找轮廓
  • 获取外部坐标

转换为灰度并模糊图像后,我们阈值以获取二进制图像

现在我们使用找到轮廓

cv2.findContours()
。由于OpenCV使用Numpy数组对图像进行编码,因此轮廓只是一个Numpy
(x,y)
坐标数组。我们可以对Numpy数组进行切片并使用
argmin()
argmax()
确定像这样的外部左,右,上和下坐标

left = tuple(c[c[:, :, 0].argmin()][0])right = tuple(c[c[:, :, 0].argmax()][0])top = tuple(c[c[:, :, 1].argmin()][0])bottom = tuple(c[c[:, :, 1].argmax()][0])

这是结果

左:(162,527)

右:(463,467)

最高:(250,8)

底部:(381,580)

import cv2import numpy as np# Load image, grayscale, Gaussian blur, thresholdimage = cv2.imread('1.png')gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)blur = cv2.GaussianBlur(gray, (3,3), 0)thresh = cv2.threshold(blur, 220, 255, cv2.THRESH_BINARY_INV)[1]# Find contourscnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] if len(cnts) == 2 else cnts[1]c = max(cnts, key=cv2.contourArea)# Obtain outer coordinatesleft = tuple(c[c[:, :, 0].argmin()][0])right = tuple(c[c[:, :, 0].argmax()][0])top = tuple(c[c[:, :, 1].argmin()][0])bottom = tuple(c[c[:, :, 1].argmax()][0])# Draw dots onto imagecv2.drawContours(image, [c], -1, (36, 255, 12), 2)cv2.circle(image, left, 8, (0, 50, 255), -1)cv2.circle(image, right, 8, (0, 255, 255), -1)cv2.circle(image, top, 8, (255, 50, 0), -1)cv2.circle(image, bottom, 8, (255, 255, 0), -1)print('left: {}'.format(left))print('right: {}'.format(right))print('top: {}'.format(top))print('bottom: {}'.format(bottom))cv2.imshow('thresh', thresh)cv2.imshow('image', image)cv2.waitKey()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/612066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号