gpt4 book ai didi

opencv - 查找最适合的图像

转载 作者:行者123 更新时间:2023-12-02 17:31:24 25 4
gpt4 key购买 nike

给定一批图像,我必须找到最合适的图像,如下面的示例所示,但是我的解决方案无法正常工作:

左图

/image/YrVmR.png

正确的图像

/image/AfWwL.png

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

有没有办法找出来或减少可能的情况,以便我可以在我的模型中使用它。

最佳答案

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

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

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

关于opencv - 查找最适合的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55450247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com