你好,我需要找到图像中给定形状的顶点(x &y坐标),在进行分割和边缘提取之后,得到的图像如下:
以下是我需要找到的坐标的顶点:
发布于 2022-02-27 07:21:22
我想你可能想先用霍夫线变换来找到线。然后,你可以从检测到的线路中得到交叉口。您可以找到OpenCV关于Hough线变换这里的教程。
下面是使用Hough线性变换的结果:
代码:
import numpy as np
import cv2 as cv2
import math
img_path = 'hSAdf.png'
# Read the original image
img = cv2.imread(img_path)
# Convert to graycsale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)[1]
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLines(dst, 1, np.pi / 180, 180, None, 0, 0)
# Drawing the lines
if lines is not None:
for i in range(0, len(lines)):
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 10000*(-b)), int(y0 + 10000*(a)))
pt2 = (int(x0 - 10000*(-b)), int(y0 - 10000*(a)))
cv2.line(cdst, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)
cv2.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
cv2.imwrite("output.png", cdst)
cv2.waitKey(0)
这里我没有使用Canny边缘检测,因为我认为图像本身是非常清晰的,这使得边缘检测变得多余。
函数HoughLines()
返回以像素为单位的rho和以弧度表示的θ,它们对应于直线方程:
编辑1: rho,theta和m,c之间的简单转换:
M=tan(θ+ PI/2)
C= rho /sin(θ)
来自索克雷特·李的图像
我认为您可以继续调整线路检测功能。您可以手动调整阈值,甚至在函数中限制直线的梯度。然后,可以通过裁剪和限制梯度来瞄准一行。
或者你可以拒绝有90度差的线的交点。然后,你会得到你需要的分数。
发布于 2022-02-27 10:45:13
使用轮廓检测和近似,您可以得到外部顶点,并计算它们:
173197 225 596 610 2102 2118 1732
#!/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)
https://stackoverflow.com/questions/71282527
复制相似问题