def selective_search(<EOL>im_orig, scale=<NUM_LIT:1.0>, sigma=<NUM_LIT>, min_size=<NUM_LIT:50>):
assert im_orig.shape[<NUM_LIT:2>] == <NUM_LIT:3>, "<STR_LIT>"<EOL>img = _generate_segments(im_orig, scale, sigma, min_size)<EOL>if img is None:<EOL><INDENT>return None, {}<EOL><DEDENT>imsize = img.shape[<NUM_LIT:0>] * img.shape[<NUM_LIT:1>]<EOL>R = _extract_regions(img)<EOL>neighbours = _extract_neighbours(R)<EOL>S = {}<EOL>for (ai, ar), (bi, br) in neighbours:<EOL><INDENT>S[(ai, bi)] = _calc_sim(ar, br, imsize)<EOL><DEDENT>while S != {}:<EOL><INDENT>i, j = sorted(S.items(), key=lambda i: i[<NUM_LIT:1>])[-<NUM_LIT:1>][<NUM_LIT:0>]<EOL>t = max(R.keys()) + <NUM_LIT:1.0><EOL>R[t] = _merge_regions(R[i], R[j])<EOL>key_to_delete = []<EOL>for k, v in list(S.items()):<EOL><INDENT>if (i in k) or (j in k):<EOL><INDENT>key_to_delete.append(k)<EOL><DEDENT><DEDENT>for k in key_to_delete:<EOL><INDENT>del S[k]<EOL><DEDENT>for k in [a for a in key_to_delete if a != (i, j)]:<EOL><INDENT>n = k[<NUM_LIT:1>] if k[<NUM_LIT:0>] in (i, j) else k[<NUM_LIT:0>]<EOL>S[(t, n)] = _calc_sim(R[t], R[n], imsize)<EOL><DEDENT><DEDENT>regions = []<EOL>for k, r in list(R.items()):<EOL><INDENT>regions.append({<EOL>'<STR_LIT>': (<EOL>r['<STR_LIT>'], r['<STR_LIT>'],<EOL>r['<STR_LIT>'] - r['<STR_LIT>'], r['<STR_LIT>'] - r['<STR_LIT>']),<EOL>'<STR_LIT:size>': r['<STR_LIT:size>'],<EOL>'<STR_LIT>': r['<STR_LIT>']<EOL>})<EOL><DEDENT>return img, regions<EOL>
Selective Search

    Parameters
    ----------
        im_orig : ndarray
            Input image
        scale : int
            Free parameter. Higher means larger clusters in felzenszwalb segmentation.
        sigma : float
            Width of Gaussian kernel for felzenszwalb segmentation.
        min_size : int
            Minimum component size for felzenszwalb segmentation.
    Returns
    -------
        img : ndarray
            image with region label
            region label is stored in the 4th value of each pixel [r,g,b,(region)]
        regions : array of dict
            [
                {
                    'rect': (left, top, width, height),
                    'labels': [...],
                    'size': component_size
                },
                ...
            ]