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

2024-05-28 18:10:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我可以通过以下步骤获得边界框,但如何保存/提取/导出每个边界框中的图像?在

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

提前谢谢


Tags: iorect图像imageimportimgsearchif

热门问题