Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code to create the squarest mosaic #3

Open
davidssmith opened this issue Jul 6, 2018 · 1 comment
Open

Code to create the squarest mosaic #3

davidssmith opened this issue Jul 6, 2018 · 1 comment

Comments

@davidssmith
Copy link

davidssmith commented Jul 6, 2018

This is a very nice package.

A long, long time ago I wrote the code to tile images into a mosaic that was as square as possible. I don't have time to create a PR, so I'm offering the code here so that you can use the algorithm if you're interested. Maybe it could be a useful default?

function mosaic{T,N}(img::Array{T,N})
  # Create a 2-D mosaic of images from an n-D stack.
  # The first two dimensons are preserved and all additional
  # dimensions are lumped together as tiles
  imsize = size(img)
  n1 = imsize[1]
  n2 = imsize[2]
  n3 = prod(imsize[3:end])
  img = reshape(img, n1, n2, n3)
  n = int(round(sqrt(n3)))  # starting point: integer nearest to the square root
  # find largest integer less than or equal to sqrt that evenly divides the
  # number of images in the stack
  k = 0
  for k in n:-1:1
    if n3 % k == 0
      break
    end
  end
  # figure out the best dimensions
  j = int(n3 / k)
  ny = int(min(j, k))
  nx = int(max(j, k))
  # stick them together
  M = zeros(eltype(img), ny*n1, nx*n2)
  for k in 1:ny, j in 1:nx
    M[1 + n1*(k-1):n1*k, 1 + n2*(j-1):n2*j] = img[:, :, j+(k-1)*nx]
  end
  M
end
@Evizero
Copy link
Member

Evizero commented Jul 7, 2018

Thank you for sharing. I agree that its worth considering as a default setting

@johnnychen94 johnnychen94 mentioned this issue Mar 9, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants