首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何减少MSER中创建的框/区域数

如何减少MSER中创建的框/区域数
EN

Stack Overflow用户
提问于 2018-11-08 10:49:13
回答 1查看 1.1K关注 0票数 2

我一直试图获得更少的框与MSER,因为我有太多的框创建在同一元素上反复与非常小的像素差异。我的代码如下:

代码语言:javascript
复制
 ## 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)

然后

代码语言:javascript
复制
 ## 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盒创建。其中的参数可以调到较少的区域(因为它们是重叠的)。帮忙好吗?

EN

回答 1

Stack Overflow用户

发布于 2018-11-28 15:07:51

_delta是减少盒数的最重要的参数。试着把它提高到25。_delta越高,你得到的气泡就越少。

  • _min_area -最小的水珠
  • _max_area -最大的水珠
  • _min_diversity -提高以减少重叠小泡的数量
  • _max_variation -提高以减少高差异的面积

了解更多信息

在那之后,我会检查bboxes来过滤掉研磨的小气泡。

代码示例

代码语言:javascript
复制
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')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53206165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档