您好,我需要在影像中找到給定形狀的頂點(x 和 y 坐標),在進行分割和邊緣提取之后,得到的影像如下:

以下是我需要找到其坐標的頂點:

uj5u.com熱心網友回復:
您可以嘗試找到輪廓,然后一旦有了輪廓,您就可以從那里查看一個輪廓的x和y位置是否與該輪廓的x和y位置以及前一個輪廓的寬度和高度相匹配。
示例:假設一個輪廓的位置為 (0,0),寬度為 10,高度為 20。假設另一個輪廓的位置為 (10,20),寬度 x 和高度 y 在這里您應該能夠搜索找到的輪廓并匹配任何具有 x width 和 y height 位置的輪廓。這將匹配此示例中的第一個和第二個輪廓,并且從那里您知道如果您有匹配項,則您找到了一個頂點。
uj5u.com熱心網友回復:
我想你可能想先使用霍夫線變換來找到線條。然后,您可以從檢測到的線中獲取交叉點。
代碼:
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,以弧度為單位回傳 theta,它們對應于線方程:

編輯 1:rho、theta 和 m、c 之間的簡單轉換:
m = tan(θ PI/2)
c = rho / sin(theta)
圖片來自
[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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/433592.html
上一篇:從影像的特定部分提取HUE值
下一篇:如何在matlab中繪制函式?
