2

我一直在尝试使用 MSER 减少框,因为我在同一元素上重复创建了太多框,而像素差异很小。我的代码如下:

 ## Get mser, and set parameters

   _delta = 10 
_min_area = 250 
_max_area = 800
_max_variation = 10.0 
_min_diversity = 30.0
_max_evolution = 10 
_area_threshold = 12.0
_min_margin = 2.9 
_edge_blur_size = 3  

mser = cv2.MSER_create(_delta,_min_area, _max_area, _max_variation,
_min_diversity,_max_evolution, _area_threshold, _min_margin, _edge_blur_size)

接着

 ## Do mser detection, get the coodinates and bboxes on the original  image
      gray = cv2.cvtColor(final, cv2.COLOR_BGR2GRAY)
       coordinates, bboxes = mser.detectRegions(gray)

在此之后,我看到创建了 26K 个盒子。其中哪些参数可以针对较少的区域进行调整(因为它们重叠很多)。请帮忙?

4

1 回答 1

2

_delta 是减少框数的最重要参数。尝试将其提高到 25。_delta 越高,得到的 blob 就越少。

  • _min_area - 最小的 blob
  • _max_area - 最大的 blob
  • _min_diversity - 提高以减少重叠 blob 的数量
  • _max_variation - 提高以减少高方差区域

了解更多信息

之后,我会检查 bboxes 以过滤掉重叠的 blob

代码示例

import cv2
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
img = cv.imread('input_img.png')
iou_th = 0.95

mser = cv2.MSER_create(_delta=10, _min_area=1000, _max_area=int(0.1 * np.pi * (img.shape[0] /2)**2), _max_variation=0.1)
regions, bboxes = mser.detectRegions(img)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

# Debug plot
img_ = img.copy()
cv2.polylines(img_, hulls, 1, (255, 0, 0), thickness=1)
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(img_)
ax.set_title('MSER with overlapping regions')

size_dict = {k: len(region) for k, region in enumerate(regions)}
# Cull overlapping blobs
graph = nx.Graph()
graph.add_nodes_from(range(len(hulls)))
for i, cnt in enumerate(hulls):
    for j, cnt in enumerate(hulls):
        if i >= j:
            continue
        box_i = bboxes[i]
        box_j = bboxes[j]
        tl_i = box_i[:2]
        tl_j = box_j[:2]

        br_i = tl_i + box_i[2:]
        br_j = tl_j + box_j[2:]

        tl = np.maximum(tl_i, tl_j)
        br = np.minimum(br_i, br_j)
        intersected_rect = br - tl
        intersection = np.prod(intersected_rect) if intersected_rect[0] > 0 and intersected_rect[1] > 0 else 0
        union = np.prod(box_i[2:]) + np.prod(box_j[2:]) - intersection
        iou = intersection / union
        if iou > iou_th:
            graph.add_edge(i, j, iou=iou)

# make list of unique regions - pick the smallest region
trees = list(nx.connected_component_subgraphs(graph))
unique_blobs = []
for tree in trees:
    # Choose the smallest region
    smallest_idx = None
    smallest_blob = np.inf
    for node in tree.nodes():
        if size_dict[node] < smallest_blob:
            smallest_blob = size_dict[node]
            smallest_idx = node

        unique_blobs.append(smallest_idx)
unique_blobs = unique_blobs
hulls = [hulls[k] for k in unique_blobs]
regions = [regions[k] for k in unique_blobs]
bboxes = [bboxes[k] for k in unique_blobs]
size_dict = {k: len(region) for k, region in enumerate(regions)}

# debug plot
img_ = img.copy()
cv2.polylines(img_, hulls, 1, (255, 0, 0), thickness=1)
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(img_)
ax.set_title('MSER with unique regions')
于 2018-11-28T15:07:51.447 回答