使图像的对象彼此最接近

布鲁诺·米格尔·贡萨尔维斯

我在PIL方面没有太多经验,我从一堆显微镜图像单元中编辑了这些图像,每个图像蒙版的图像尺寸均为30x30。我一直在努力将这些单元格放置在尽可能彼此不重叠的黑色背景中。

我的代码如下:

def spread_circles(circles, rad, iterations,step):
    radsqr = rad**2
    for i in range(iterations):
        for ix,c in enumerate(circles):
            vecs = c-circles
            dists = np.sum((vecs)**2,axis=1)
            if len(dists)>0:
                push = (vecs[dists<radsqr,:].T*dists[dists<radsqr]).T
                push = np.sum(push,axis=0)
                pushmag = np.sum(push*push)**0.5
                if pushmag>0:
                    push = push/pushmag*step
                    circles[ix]+=push
    return circles



def gen_image(sample,n_iter, height=850, width = 850, max_shape=30, num_circles=150):
    circles = np.random.uniform(low=max_shape,high=height-max_shape,size=(num_circles,2))   
    circles = spread_circles(circles, max_shape, n_iter, 1).astype(int)
    img = Image.new(mode='F',size=(height,width),color=0).convert('RGBA')
    final1 = Image.new("RGBA", size=(height,width))
    final1.paste(img, (0,0), img)
    for n,c in enumerate(circles):
        foreground = sample[n]       
        final1.paste(foreground, (c[0],c[1]), foreground)     
    return final1 

但是,如果我进行几次迭代,就很难避免重叠,如果我增加迭代次数,它们将变得过于稀疏,如下所示:

在此处输入图片说明

我想要的是类似于我绘制的红色圆圈内的内容:

在此处输入图片说明

我需要它们尽可能接近它们,就像瓷砖一样。我怎样才能做到这一点?

马克·谢切尔

我已经开始考虑这一点,并且已经实施了一些策略。任何其他想获得一些乐趣的人都非常欢迎借用,窃取,使用或破解我可以使用的任何代码!明天我可能会再玩些。

#!/usr/bin/env python3

from PIL import Image, ImageOps
import numpy as np
from glob import glob
import math

def checkCoverage(im):
   """Determines percentage of image that is cells rather than background"""
   N = np.count_nonzero(im)
   return N * 100 / im.size


def loadImages():
   """Load all cell images in current directory into list of trimmed Numpy arrays"""
   images = []
   for filename in glob('*.png'):
      # Open and convert to greyscale
      im = Image.open(filename).convert('L')
      # Trim to bounding box
      im = im.crop(im.getbbox())
      images.append(np.array(im))
   return images

def Strategy1():
   """Get largest image and pad all images to that size - at least it will tesselate perfectly"""
   images = loadImages()
   N = len(images)
   # Find height of tallest image and width of widest image
   maxh = max(im.shape[0] for im in images)
   maxw = max(im.shape[1] for im in images)
   # Determine how many images we will pack across and down the output image - could be improved
   Nx = int(math.sqrt(N))+1
   Ny = int(N/Nx)+1
   print(f'Padding {N} images each to height:{maxh} x width:{maxw}')
   # Create output image
   res = Image.new('L', (Nx*maxw,Ny*maxh), color=0)
   # Pack all images from list onto regular grid
   x, y = 0, 0
   for im in images:
      this = Image.fromarray(im)
      h, w = im.shape
      # Pack this image into top-left of its grid-cell, unless
      # a) in first row, in which case pack to bottom
      # b) in first col, in which case pack to right
      thisx = x*maxw
      thisy = y*maxh
      if y==0:
         thisy += maxh - h
      if x==0:
         thisx += maxw - w
      res.paste(this, (thisx,thisy))
      x += 1
      if x==Nx:
         x  = 0
         y += 1

   # Trim extraneous black edges
   res = res.crop(res.getbbox())
   # Save as JPEG so we don't find it as a PNG in next strategy
   res.save('strategy1.jpg')
   cov = checkCoverage(np.array(res))
   print(f'Strategy1 coverage: {cov}')


def Strategy2():
   """Rotate all images to portrait (tall rather than wide) and order by height so we tend to stack equal height images side-by-side"""
   tmp = loadImages()
   # Recreate list with all images in portrait format, i.e. tall
   portrait = []
   for im in tmp:
      if im.shape[0] >= im.shape[1]:
         # Already portrait, add as-is
         portrait.append(im)
      else:
         # Landscape, so rotate
         portrait.append(np.rot90(im))
   images = sorted(portrait, key=lambda x: x.shape[0], reverse=True)
   N = len(images)
   maxh, maxw = 31, 31
   # Determine how many images we will pack across and down the output image
   Nx = int(math.sqrt(N))+1
   Ny = int(N/Nx)+1
   print(f'Packing images by height')
   # Create output image
   resw, resh = Nx*maxw, Ny*maxh
   res = Image.new('L', (resw,resh), color=0)
   # Pack all from list 
   xpos, ypos = 0, 0
   # Pack first row L->R, second row R->L and alternate
   packToRight = True
   for im in images:
      thisw, thish = im.shape
      this = Image.fromarray(im)
      if packToRight:
         if xpos+thisw < resw:
            # If it fits to the right, pack it there
            res.paste(this,(xpos,ypos))
            xpos += thisw
         else:
            # Else start a new row, pack at right end and continue packing to left
            packToRight = False
            res.paste(this,(resw-thisw,ypos))
            ypos = res.getbbox()[3]
      else:
         if xpos>thisw:
            # If it fits to the left, pack it there
            res.paste(this,(xpos-thisw,ypos))
            xpos -= thisw
         else:
            # Else start a new row, pack at left end and continue packing to right
            ypos = res.getbbox()[3]
            packToRight = True
            res.paste(this,(0,ypos))

   # Trim any black edges
   res = res.crop(res.getbbox())
   # Save as JPEG so we don't find it as a PNG in next strategy
   res.save('strategy2.jpg')
   cov = checkCoverage(np.array(res))
   print(f'Strategy2 coverage: {cov}')

Strategy1()
Strategy2()

Strategy1的覆盖率为42%:

在此处输入图片说明

Strategy2提供了64%的覆盖率:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何淡入最接近的图像?

来自分类Dev

如何淡入最接近的图像?

来自分类Dev

将数字分解为彼此最接近的加数

来自分类Dev

查找具有彼此最接近的K个元素的子集

来自分类Dev

查找具有彼此最接近的K个元素的子集

来自分类Dev

以3个元素组成的数组彼此最接近的合并点

来自分类Dev

将数字分解为彼此最接近的加数

来自分类Dev

获取最接近的图像attr(“ src”)jQuery

来自分类Dev

在Java集合中查找最接近的对象

来自分类Dev

比较Javascript数组对象并返回最接近的对

来自分类Dev

jQuery根据最接近的图像的宽度更改div的宽度吗?

来自分类Dev

尝试选择最接近按钮的图像,但Jquery返回Undefined

来自分类Dev

查找图像中每个像素最接近的RGB颜色

来自分类Dev

在Vuforia中获取最接近的目标图像

来自分类Dev

Android中哪种字体最接近该图像?

来自分类Dev

jQuery最接近特定div内图像的查找

来自分类Dev

将图像源放入最接近的输入值

来自分类Dev

获取彼此最接近的两个单词之间的字符串

来自分类Dev

如何比较Color对象并获取Color []中最接近的Color?

来自分类Dev

查找包含最接近的属性值的对象的List索引

来自分类Dev

如何从数组对象获取最近或最接近的日期?

来自分类Dev

解析云查询以获取最接近GeoPoint的对象

来自分类Dev

AngularJS:在多值对象中查找最接近的值

来自分类Dev

检查对象中的URL,并返回最接近的同级

来自分类Dev

AngularJS:在多值对象中查找最接近的值

来自分类Dev

从最接近日期的对象获取标题,django 模型

来自分类Dev

最接近的整数

来自分类Dev

近似最接近对算法

来自分类Dev

获取最接近的元素

Related 相关文章

热门标签

归档