【问题标题】:Extracting image from bounding box - selective search从边界框中提取图像 - 选择性搜索
【发布时间】:2017-08-14 03:05:20
【问题描述】:

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

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

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()

提前致谢

【问题讨论】:

    标签: python computer-vision


    【解决方案1】:

    你知道如何使用线条得到每个矩形

    for x, y, w, h in candidates:
    

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

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

    【讨论】:

    • 谢谢,这样做会创建一个图像。有没有一种方法可以创建多个图形,每次 for 循环运行时一个图形
    • 如果要显示每个矩形的图形,只需在使用 plt.show(imgRect) 之前执行 plt.figure()
    • @Veejay 你能更新你的代码并说明你是如何解决这个问题的吗?
    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多