gpt4 book ai didi

opencv - 找到轮廓属性

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:21 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2012 和 OpenCV。我想找到具有以下规范的两点坐标。

  1. 第一个点:最高最左边的点。

  2. 第二个点:最高最右边的点。

就像你在图片中看到的那样。

enter image description here

enter image description here

enter image description here

enter image description here

最佳答案

首先我想指出这个答案是用 Python 写的,它的目的是给出一个关于如何解决这个问题的想法。

其次,定义最高点和最左点(与最高点和最右点相同)是一个问题,因为轮廓的形状变化很大。这意味着左右的最极端值并不总是与最高值互补。以下图为例:

enter image description here

右上角的红点是北向最高的但不是最右的(它是所有 4 个点中最左边的)而下红点是最右的点但它不是最高的(它实际上是是北向最低的!)。还有另外两点介于两者之间,可以符合上述标准。

话虽这么说,我已经用 Python 为您的图片中描述的最相似的解决方案制作了一个脚本,以展示我将如何尝试处理该问题。它可能不是您想要的,并且可能有更好的解决方案,但这可能会给您一个想法或弄清楚您实际需要什么。

我冒昧地下载了您的示例图片并对其进行了一些编辑,以便我可以举个例子。这是我的“原件”:

enter image description here

enter image description here

我提取了图像的轮廓并选择最大的一个 - 返回一个点数组。然后我提取了 X 坐标中的最大值(最右边的点)和最低的 Y 坐标(最北点)。因为轮廓没有直线,所以我已经为最极端点的显着点可以变化多少设置了一个阈值(在你的情况下,数字可能不同 - 我的情况是 +- 3)。然后我在一个列表中附加了阈值中所有最正确的点,在另一个列表中附加了所有最北点。然后对于最左边,您从列表中选择 X 坐标最低的点,并在另一个列表中选择 Y 坐标最低的点作为最右边的点。

结果如下:

enter image description here

enter image description here

希望对您有所帮助。干杯!

Python 示例:

import cv2
import numpy as np

img = cv2.imread('uplupr.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,170,255,cv2.THRESH_BINARY)
im, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
mask = np.zeros(img.shape, dtype="uint8")
cnt = max(contours, key=cv2.contourArea)
cv2.drawContours(mask, [cnt], -1, (255,255,255), 1)
top_right_append = 0
top_up_append = 1000
point_up = []
point_right = []

for i in cnt:
top_up = int(i[:,1])
top_right = int(i[:, 0])
if top_right > top_right_append:
top_right_append = int(top_right)
if top_up < top_up_append:
top_up_append = int(top_up)

for i in cnt:
if int(i[:,1]) < top_up_append+3:
up = (int(i[:,0]), int(i[:,1]))
point_up.append(up)

for i in cnt:
if int(i[:,0]) > top_right_append-3:
right = (int(i[:,0]), int(i[:,1]))
point_right.append(right)

point_up.sort(key=lambda tup: tup[0])
point_right.sort(key=lambda tup: tup[1])

cv2.circle(mask,(point_up[0]), 4, (0,0,255), -1)
cv2.circle(mask,(point_right[0]), 4, (0,0,255), -1)

cv2.imshow('image', mask)

关于opencv - 找到轮廓属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com