从边界框中提取图像 - 选择性搜索

时间:2017-08-14 03:05:20

标签: python computer-vision

我正在学习如何正确使用选择性搜索算法在图像周围创建边界框,在边界框内提取较小的图像,然后对较小的图像进行进一步分析。

我可以通过以下方式获取边界框,但如何在每个边界框内保存/提取/导出图像?

import skimage.data
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import selectivesearch
import time
import io
import PIL
import scipy.misc

from skimage.io import imread
from PIL import Image
from skimage.transform import rescale, resize, downscale_local_mean


def main():

    # loading astronaut image


    # image = skimage.io.imread('/Users/vivek/Desktop/IMG_3350.JPG')
    # img = resize(image, (500,500), mode = 'reflect')

    img = skimage.io.imread('/Users/vivek/Downloads/IMG_3350_640x480.JPG')

    print ('image loaded')

    # perform selective search
    print ('initializing selective search')
    start = time.time()
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=600, sigma=0.9, min_size=10)




    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])
    print ('selective search complete')

    end = time.time()
    totalTime = end - start
    print ('time taken to run this is : ' + str(totalTime))

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print x, y, w, h
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)
        #plt.imsave("testerimage.jpg", None)

    plt.show()



if __name__ == "__main__":
    main()

Here is a screenshot of my resulting image with bounding boxes

提前致谢

1 个答案:

答案 0 :(得分:1)

您知道如何使用线

获取每个矩形
for x, y, w, h in candidates:

要获取此矩形中的图像,请执行以下操作:

imgRect = img[y:y+h,x:x+w]