Last active
August 31, 2022 07:30
-
-
Save zacky1972/d3f590b7ce9ba9de1f8653b4fdffbf95 to your computer and use it in GitHub Desktop.
Vertical Image Division
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.put_env("EVISION_PREFER_PRECOMPILED", "true") # Remove if you use a platform on which Evision does not provide a pre-compiled library. | |
System.put_env("EVISION_PRECOMPILED_CACHE_DIR", "#{System.user_home!()}/.cache") | |
Mix.install( | |
[ | |
{:nx, "~> 0.3"}, | |
{:exla, "~> 0.3"}, | |
{:evision, "~> 0.1.2", github: "cocoa-xu/evision", tag: "v0.1.2"} | |
], | |
config: [ | |
nx: [default_backend: EXLA.Backend] | |
] | |
) | |
src_file = "ZACKY-3000.jpg" | |
div_size = 256 | |
dst_file_ext = Path.extname(src_file) | |
dst_file_basename = Path.basename(src_file, dst_file_ext) | |
dst_files = | |
Stream.unfold(0, fn counter -> {counter, counter + 1} end) | |
|> Stream.map(& "#{dst_file_basename}_#{&1}#{dst_file_ext}") | |
div_img = | |
Evision.imread!(src_file) | |
|> Evision.Nx.to_nx() | |
|> Nx.transpose(axes: [1, 0, 2]) # here is the most important | |
|> Nx.to_batched_list(div_size) | |
|> Enum.map(& Nx.transpose(&1, axes: [1, 0, 2])) # here is the most important | |
|> Enum.map(& Evision.Nx.to_mat!(&1)) | |
Enum.zip(div_img, dst_files) | |
|> Enum.map(fn {img, dst_file} -> Evision.imwrite!(dst_file, img) end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment