我正在设置一个计算机视觉应用程序,但是我受制于必须应用于坐标数组的控件。我想从坐标数组中检索全部个可能的正方形。
image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(image, 20, 0.01, 15)
corners = np.int0(corners)
print("Points")
for corner in corners:
x, y = corner.ravel()
cv2.circle(image, (x, y), 5, (0, 0, 255), -1)
print(corners)
corners = corners.tolist()
corners = flatten(corners)
这只是点数组的一部分,我必须使用该点来检索图像中的所有正方形:
[[10,50],[420,188],[177,425],[225,425],[176,220],[225,221],[10,170],[21,50],[21,170]]
答案 0 :(得分:0)
执行此操作的一种丑陋方法是遍历4个点的所有组合并检查它们是否定义了正方形。
具有检查正方形状态的功能:
def is_square_func(a, b, c, d):
is_square = a[0] == b[0]
is_square = (is_square and c[0] == d[0])
is_square = (is_square and a[1] == c[1])
is_square = (is_square and b[1] == d[1])
# Condition to find a square only once
is_square = (is_square and a[0] > c[0])
is_square = (is_square and c[1] > d[1])
return is_square
这里是要应用于坐标列表的函数:
def find_squares(point_array):
n = len(point_array)
squares = []
for i in range(n):
for j in range(n):
if i == j:
continue
for k in range(n):
if i == k or j == k:
continue
for l in range(n):
if i == l or j == l or k == l:
continue
if is_square_func(point_array[i], point_array[j], point_array[k], point_array[l]):
print("New square")
print(point_array[i])
print(point_array[j])
print(point_array[k])
print(point_array[l])
squares += [point_array[i], point_array[j], point_array[k], point_array[l]]
return squares