在大图像中绘制边界框 [英] drawing a bounding box in large images

查看:110
本文介绍了在大图像中绘制边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的二进制图像(4k x 7k pix),我想从中提取整个黄色部分作为单个矩形.我尝试使用二进制腐蚀来使黄色区域内的特征均匀.然后,我使用了skimage.regionpropsbbox方法,但是对于具有一个大bbox的大图像来说,它似乎运行得不够快.你有什么建议吗?

I have a large binary image (4k x 7k pix) from which I want to extract the entire yellow portion as a single rectangle. I tried binary erosion to even out features inside the yellow region. Then I used the bbox method of skimage.regionprops but it does not seem to work fast enough for large image with one large bbox. Do you have any suggestion?

推荐答案

由于您提供的图像包含分散注意力的轴,并且颜色错误且太小,因此我使用 ImageMagick 在终端中像这样:

As the image you provided includes distracting axes, and is the wrong colour and too small, I created as realistic a version as I could with ImageMagick like this in Terminal:

convert bbox.png -alpha off -crop 120x215+40+13 -colorspace gray -normalize -threshold 50% -scale 4200x7200\! bbox.png

完整尺寸的版本为4200x7200.

The full-size version is 4200x7200.

然后我写了一个基于numpybbox版本,如下所示

I then wrote a numpy-based version of bbox as follows

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

def bbox(image):
    """Find bounding box of image"""
    # Project all columns into row same width as image
    proj=np.any(image,axis=0)
    # Find first non-zero value from Left
    L=np.argmax(proj)
    # And right
    R=image.shape[1]-np.argmax(np.flipud(proj))-1
    # Project all rows into column same height as image
    proj=np.any(image,axis=1)
    # Find first non-zero value from Top
    T=np.argmax(proj)
    # And Bottom
    B=image.shape[0]-np.argmax(np.flipud(proj))-1
    return T,L,B,R

image=np.array(Image.open("a.png").convert("L"))
print(bbox(image))

在Mac上运行时间为5.3毫秒.只是为了好玩,我将其穿线并在单独的平行线程上运行了水平投影和垂直投影,结果下降到3.6ms,结果相同.

That runs in 5.3ms on my Mac. Just for fun, I threaded it and ran the horizontal projection and vertical projection on separate parallel threads and it came down to 3.6ms with the same results.

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

import threading
import queue

def DoOneDim(image,axis,q):
    """Find bounding box of image"""
    proj=np.any(image,axis=axis)
    # Find first non-zero value
    A=np.argmax(proj)
    # And and last
    B=image.shape[1-axis]-np.argmax(np.flipud(proj))-1
    q.put({axis:(A,B)})


def bboxTh(image):
    """Threaded version of bbox() that does vertical and horizontal on their own theads"""
    q = queue.Queue()
    Hthread=threading.Thread(target=DoOneDim, args=(image,0,q))
    Vthread=threading.Thread(target=DoOneDim, args=(image,1,q))
    Hthread.start()
    Vthread.start()
    Hthread.join()
    Vthread.join()
    results=dict()
    while not q.empty():
       results.update(q.get())
    return results

image=np.array(Image.open("a.png").convert("L"))
print(bboxTh(image))

已识别的框如下所示:

这篇关于在大图像中绘制边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆