我需要从下图中生成交集矩阵:
这张图片是一个迷宫4x4网格板,其中一些红色和蓝色点随机分布,图像将从迷你无人机中取出,我需要能够找到红色和蓝色点的位置以提供此信息一个线跟随者,需要通过蓝点,避免红色点。
我已经对此图像应用了一些滤镜来生成轮廓,但我不知道如何将其转换为坐标。 这是我做的:
import numpy as np
import cv2
import random
original = cv2.imread('laberinto.jpg',1)
img = cv2.resize(original, (600,600), interpolation=cv2.INTER_NEAREST)
# cv2.imshow("Original",img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3),0)
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 205, 1)
# cv2.imshow("Binary",thresh)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))
filtered = []
for c in contours:
if cv2.contourArea(c) < 1000:continue
filtered.append(c)
print(len(filtered))
objects = np.zeros([img.shape[0],img.shape[1],3], 'uint8')
for c in filtered:
col = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
cv2.drawContours(objects,[c], -1, col, -1)
area = cv2.contourArea(c)
p = cv2.arcLength(c,True)
print(area,p)
cv2.imshow("Contours",objects)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: