【发布时间】:2021-08-12 03:21:26
【问题描述】:
我正在使用 python3 和 图像处理 库(例如 OpenCV)开发公司徽标图像搜索系统。
到目前为止,我已经设法从给定图像中提取单个对象,这些对象被提取为二进制图像,因此它们可以轻松用作蒙版。
这是通过 K-Means 对图像进行聚类,使用具有 4 路连接性的 cv2.connectedComponents,然后应用 Watershed 来分离对象来完成的。
# 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)
例子:
输入图像
输出对象之一
我现在的目标是在我的数据集中找到包含与上面的玫瑰相似的对象(不一定是完全相同的玫瑰)的图像。这可以通过 R-CNN 实现吗?还有哪些匹配方式有用?
【问题讨论】:
标签: python numpy opencv image-processing