Python 在numpy中查找单色投影矩形的角点
我有一个矩形的图像,它是投影的。整个矩形用一种普通颜色着色。与此图像类似:Python 在numpy中查找单色投影矩形的角点,python,python-3.x,numpy,opencv,image-processing,Python,Python 3.x,Numpy,Opencv,Image Processing,我有一个矩形的图像,它是投影的。整个矩形用一种普通颜色着色。与此图像类似: 我试图有效地找到它的4个角,可能使用numpy和cv2,如果多边形是矩形,你只能使用numpy.where,但它们是四边形,我不知道用numpy怎么做 下面是一种方法(我将您的图像保存为greens.png): 我不确定哈里斯是否是最快的角点检测。我见过opencv有一个FastFeatureDetection,但我从未使用过它。如果多边形是矩形,你只能使用numpy.where,但它们是四边形,我不知道用numpy怎
我试图有效地找到它的4个角,可能使用numpy和cv2,如果多边形是矩形,你只能使用numpy.where,但它们是四边形,我不知道用numpy怎么做 下面是一种方法(我将您的图像保存为greens.png):
我不确定哈里斯是否是最快的角点检测。我见过opencv有一个FastFeatureDetection,但我从未使用过它。如果多边形是矩形,你只能使用numpy.where,但它们是四边形,我不知道用numpy怎么做 下面是一种方法(我将您的图像保存为greens.png):
我不确定哈里斯是否是最快的角点检测。我见过opencv有一个FastFeatureDetection,但我从未使用过它。我最终使用的代码是:
# image is the original image, color is the polygon's color as BGR
width = image.shape[1]
height = image.shape[0]
coords = np.argwhere(np.ones([height, width]))
coords_x = coords[:, 1]
coords_y = coords[:, 0]
r,g,b = image[:,:,2], image[:,:,1], image[:,:,0]
color_mask = (r[coords_y, coords_x] == color[2]) & (g[coords_y, coords_x] == color[1]) & (b[coords_y, coords_x] == color[0])
coords_x_filtered = np.extract(color_mask, coords_x)
coords_y_filtered = np.extract(color_mask, coords_y)
max_br = np.argmax(coords_x_filtered + coords_y_filtered) # Find the pixel furthest along y=x direction
max_tr = np.argmax(coords_x_filtered - coords_y_filtered) # ...
max_tl = np.argmax(-coords_x_filtered - coords_y_filtered)
max_bl = np.argmax(-coords_x_filtered + coords_y_filtered)
x, y = int(coords_x_filtered[max_tl]), int(coords_y_filtered[max_tl])
# Continue to other corners
关键是要找到4个方向上沿一个方向最远的像素。它确实需要您定义什么是“最远的”,特别是我使用的是
y=x
和y=-x
方向,这并不完美,但足够好了我最终使用的代码是:
# image is the original image, color is the polygon's color as BGR
width = image.shape[1]
height = image.shape[0]
coords = np.argwhere(np.ones([height, width]))
coords_x = coords[:, 1]
coords_y = coords[:, 0]
r,g,b = image[:,:,2], image[:,:,1], image[:,:,0]
color_mask = (r[coords_y, coords_x] == color[2]) & (g[coords_y, coords_x] == color[1]) & (b[coords_y, coords_x] == color[0])
coords_x_filtered = np.extract(color_mask, coords_x)
coords_y_filtered = np.extract(color_mask, coords_y)
max_br = np.argmax(coords_x_filtered + coords_y_filtered) # Find the pixel furthest along y=x direction
max_tr = np.argmax(coords_x_filtered - coords_y_filtered) # ...
max_tl = np.argmax(-coords_x_filtered - coords_y_filtered)
max_bl = np.argmax(-coords_x_filtered + coords_y_filtered)
x, y = int(coords_x_filtered[max_tl]), int(coords_y_filtered[max_tl])
# Continue to other corners
关键是要找到4个方向上沿一个方向最远的像素。它确实需要您定义什么是“最远的”,特别是我使用的是
y=x
和y=-x
方向,这并不完美,但已经足够好了您不想尝试Harris-cormer检测吗?使用np.where。我在手机里,一到笔记本电脑我就可以发布解决方案。你不想试试哈里斯·科默检测吗?使用np.where。我在手机里,一到笔记本电脑我就可以发布解决方案。为什么不为两个矩形都找个角呢?当然,两个矩形都可以。只需更改np.where clauseThanks for share+1为什么不为两个矩形查找角点?当然,您也可以使用两个矩形。只需将np.where子句更改为共享+1