gpt4 book ai didi

python - 使用Python/计算机视觉等获取图像中的顶点? (附图片)

转载 作者:行者123 更新时间:2023-12-03 08:09:21 25 4
gpt4 key购买 nike

大家好,我需要找到图像中给定形状的顶点(x 和 y 坐标),在进行分割和边缘提取后,以下是获得的图像:[![在此处输入图像描述][1]][1]

以下是我需要查找坐标的顶点:[![在此处输入图像描述][2]][2]

最佳答案

使用Contour detection和近似,你可以得到外部顶点,并计算它们:

res

[1737 197][616 199][225 596][ 226 1708][ 610 2102][1717 2121][2118 1732][2134 601]

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np

img = cv2.imread("input.png", 0)

def fillhole(input_image):
'''
input gray binary image get the filled image by floodfill method
Note: only holes surrounded in the connected regions will be filled.
:param input_image:
:return:
'''
im_flood_fill = input_image.copy()
h, w = input_image.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
im_flood_fill = im_flood_fill.astype("uint8")
cv2.floodFill(im_flood_fill, mask, (0, 0), 255)
im_flood_fill_inv = cv2.bitwise_not(im_flood_fill)
img_out = input_image | im_flood_fill_inv
return img_out

res = fillhole(img)

contours = cv2.findContours(res, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

peri = cv2.arcLength(contours[945], True)
approx = cv2.approxPolyDP(contours[945], 0.04 * peri, True)

im = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
s = 10
for p in approx:
p = p[0]
print(p)
im[p[1]-s:p[1]+s, p[0]-s:p[0]+s] = (255, 255, 0)

cv2.drawContours(im, contours, 945, (0, 200, 255), 3)

cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img", im)
cv2.waitKey(0)

关于python - 使用Python/计算机视觉等获取图像中的顶点? (附图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71282527/

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