根据图像的空白对图像进行分区 [英] Partitioning images based on their white space

查看:91
本文介绍了根据图像的空白对图像进行分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多三个物体的图像,这些物体的白色背景被空白隔开.例如,

I have lots of images of three objects with a white background separated by white space. For example,

是否可以将这张图片(以及类似的图片)自动分割成三张图片?如果这也可以从命令行运行,那就太好了.

Is it possible to split this image (and ones like it) into three images automatically? It would be great if this also worked from the command line.

推荐答案

正如@ypnos所说,您希望通过求和或求平均来折叠行.这将使您获得图像宽度的矢量.接下来,将所有内容剪裁到高阈值以下,并记住高数字对应高亮度.这将选择空白:

As @ypnos said, you want to collapse the rows by summation, or averaging. That will leave you with a vector the width of the image. Next clip everything below a high threshold, remembering that high numbers correspond to high brightness. This will select the white space:

然后,您只需对其余索引进行聚类,然后选择中间的两个聚类(因为外部两个聚类属于边界空白).在python中,它看起来像这样:

Then you simply cluster the remaining indices and select the middle two clusters (since the outer two belong to the bordering white space). In python this looks like so:

import sklearn.cluster, PIL.Image, numpy, sys, os.path
# import matplotlib.pyplot as plt

def split(fn, thresh=200):

    img = PIL.Image.open(fn)
    dat = numpy.array(img.convert(mode='L'))
    h, w = dat.shape
    dat = dat.mean(axis=0)
    # plt.plot(dat*(dat>thresh);

    path, fname = os.path.split(fn)
    fname = os.path.basename(fn)
    base, ext = os.path.splitext(fname)

    guesses = numpy.matrix(numpy.linspace(0, len(dat), 4)).T
    km = sklearn.cluster.KMeans(n_clusters=2, init=guesses)
    km.fit(numpy.matrix(numpy.nonzero(dat>thresh)).T)
    c1, c2 = map(int, km.cluster_centers_[[1,2]])

    img.crop((0, 0, c1, h)).save(path + '/' + base + '_1' + ext)
    img.crop((c1, 0, c2, h)).save(path + '/' + base + '_2' + ext)
    img.crop((c2, 0, w, h)).save(path + '/' + base + '_3' + ext)

if __name__ == "__main__":
    split(sys.argv[1], int(sys.argv[2]))

该方法的一个缺点是,它可能会绊倒在带有明亮物体的图像上(无法正确识别空白区域),或者没有被清晰的垂直线隔开(例如,在合成物中重叠).在这种情况下,不限于垂直线的线检测会更好.我将其实施给其他人.

One shortcoming of this method is that it may stumble on images with bright objects (failing to properly identify the white space), or are not separated by a clean vertical line (e.g., overlapping in the composite). In such cases line detection, which is not constrained to vertical lines, would work better. I leave implementing that to someone else.

这篇关于根据图像的空白对图像进行分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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