在不同的图像中寻找相似的对象

2024-06-03 03:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python3和OpenCV等图像处理库开发一个公司徽标图像搜索系统

到目前为止,我已经成功地从给定的图像中提取了单个对象,这些对象被提取为二值图像,因此它们可以很容易地用作遮罩

这是通过K-Means对图像进行聚类,使用具有4向连通性的cv2.connectedComponents,然后将分水岭应用于分离对象来实现的

# Making a binary version of the kmeans clustered image
gray = cv2.cvtColor(masked_image, cv2.COLOR_BGR2GRAY);
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)

# Euclidean Distance and Distance Mapping
D = ndimage.distance_transform_edt(binary)
localMax = peak_local_max(D, indices = False, min_distance = 25, labels = binary)

# Connected Component Analysis on local peaks and watershed algorithm
markers = ndimage.label(localMax, structure=np.ones((3,3)))[0]
labels = watershed(-D, markers, mask=binary)
print('{} components found'.format(len(np.unique(labels))- 1))

# Loop over unique labels
for label in np.unique(labels):
  if label == 0:
    continue
  
  # Draw label on the mask
  mask = np.zeros(gray.shape, dtype="uint8")
  mask[labels == label] = 255

  # Detect contours in the mask 
  cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = imutils.grab_contours(cnts)
  c = max(cnts, key=cv2.contourArea)
 
  # Draw all contours
  image = cv2.drawContours(mask, cnts, -1, (0, 255, 0), 1)
  cv2_imshow(image)
  cv2.imwrite("/content/watershed/contour{}.png".format(label), image)

例如:

输入图像

enter image description here

其中一个输出对象

enter image description here

我现在的目标是在我的数据集中找到包含类似于上面玫瑰的对象(不一定是完全相同的玫瑰)的图像。用R-CNN能做到这一点吗?还有什么其他匹配方法会有用


Tags: the对象图像imagelabelsnpmaskcv2
1条回答
网友
1楼 · 发布于 2024-06-03 03:23:32

你有很多选择。我列出了一些想法:

  1. 距离直方图-您可以计算RGB通道中的图像直方图,当每种颜色的每个箱子都是特征向量的组成部分时,您可以测量遮罩图像的距离,并找到低于阈值的所有图像
  2. 使用CNN作为特征提取器-采用预先训练的模型(例如,在imageNet上训练的VGG16)。将遮罩图像调整为固定形状,并将模型输出用作特征向量。然后你可以用欧几里得距离或其他度量对它进行分类
  3. 模板匹配-您可以将所有遮罩对象调整为特定大小,并使用旋转N个角度的玫瑰进行模板匹配或MSE(或其他技术)。在这里,您也需要设置阈值

有很多解决方案,您应该尝试其中一些,并将其与验证集进行比较。
希望有帮助

相关问题 更多 >