寻找最合适的图片

时间:2019-04-01 07:45:26

标签: opencv computer-vision

给出一批图像,我必须找到最合适的图像,如下面的示例所示,但是我的解决方案不起作用:

左图

https://i.stack.imgur.com/YrVmR.png

右图

https://i.stack.imgur.com/AfWwL.png

我首先尝试使用Google Cloud Vision API,但效果不佳,然后我使用路德维希训练了一个模型,但是要永久尝试所有可能的图像组合,因为我有2500张左图和2500张正确的图像。

有没有一种方法可以找出答案或减少可能的情况,以便我可以在模型中使用它。

1 个答案:

答案 0 :(得分:0)

此解决方案着眼于一对图像。该算法评估图像中的形状是否会像钥匙和锁一样啮合。我的答案不是尝试对齐图像。

第一步是在图像中找到轮廓:

left= cv2.imread('/home/stephen/Desktop/left.png')
right = cv2.imread('/home/stephen/Desktop/right.png')
# Resize
left = cv2.resize(left, (320,320))
gray = cv2.cvtColor(left, cv2.COLOR_BGR2GRAY)
_, left_contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Approximate
left_contour = left_contours[0]
epsilon = 0.005*cv2.arcLength(left_contour,True)
left_contour = cv2.approxPolyDP(left_contour,epsilon,True)

left_contours right_contours

什么是轮廓?轮廓只是形状外围上的点的列表。三角形的轮廓将包含3个点,长度为3。这些点之间的距离将是三角形中每条腿的长度。

类似地,峰和谷之间的距离将在您的图像中匹配。为了计算该距离,我找到了轮廓点之间的距离。由于图像对齐的方式,我只使用了水平距离。

left_dx = []
for point in range(len(left_contour)-1):
    a = left_contour[point][0]
    b = left_contour[point+1][0]
    dist = a[0]-b[0]
    left_dx.append(dist)
right_dx = []
for point in range(len(right_contour)-1):
    a = right_contour[point][0]
    b = right_contour[point+1][0]
    # Use the - of the distance becuase this is the key hole, not the key
    dist = -distance(a,b)
    right_dx.append(dist)
# Reverse so they will fit
right_dx.reverse()

dx in contours

这时,您可以看到轮廓对齐。如果您有更好的图像,轮廓将在此步骤中对齐。我使用Scipy进行迭代并检查功能是否对齐。如果两个功能确实对齐,则图像中的对象将啮合。

left_x_values = []
for i in range(len(left_dx)): left_x_values.append(i)
x = np.array(left_x_values)
y = np.array(left_dx)
left_x_new = np.linspace(x.min(), x.max(),500)
f = interp1d(x, y, kind='quadratic')
left_y_smooth=f(left_x_new)
plt.plot (left_x_new, left_y_smooth,c = 'g')

fitting

我再次对自己生成的图像进行了尝试:

right left

轮廓:

right_contorus test left_contours test

轮廓点之间的距离:

distances

拟合轮廓:

enter image description here