okaneco / kmeans-colors Goto Github PK
View Code? Open in Web Editor NEWk-means clustering library and binary to find dominant colors in images
License: Apache License 2.0
k-means clustering library and binary to find dominant colors in images
License: Apache License 2.0
This should look like the main loop functions. It might be possible to split these out into their own functions to be able to reduce/reuse code since they do very similar things.
The main loops in general should be able to be simplified due to generics and similar strategies of calculating results.
Hello, communicating again through this because not sure how to!
Inspired by your create, and because I'm super into GPU compute right now for some reason, I've been working on my own version that leverage the GPU to do the heavy lifting.
https://github.com/redwarp/k-means-gpu
It's still quite limited but I wanted to share, because you are totally the inspiration there! It's a lot of fun and I, like, understand 80% of what I'm doing :-D
Does not do kmean++ yet, does only do rgb colors, but it's fast!
Cheers and thanks again.
I couldn't find a better way to contact you, so have an issue.
Thanks for this project โ I've had a Python implementation of kmeans on images for a while, but I wanted to switch to Rust to make it go faster. I thought I might have to implement it myself, then I found your library and decided to try that. It was super easy to get working and saved me a ton of work, thanks!
If you're interested, this is where I'm using it: https://github.com/alexwlchan/dominant_colours
There exists another library on Rust, lab
, that has a Lab
struct nearly identical to palette
s, the main difference being the L*a*b* parameters are f32
's rather than generic types and there is no white_point
. The same also applies to the LCh
struct. I would like to be able to use this struct over the one palette
provides, if possible. Cheers.
Hi @okaneco,
first of all: I love the library and cli, really having some fun playing around with it right now, thanks a lot for putting this together!
I found one issue with regards to parsing hex numbers which start with a leading 0 (Zero).
The leading 0 seems to be cut off when parsed as an input given via the '-c' flag.
f.e. '035951' becomes '35951' when parsed as seen in the following screenshot:
Hello, I've noticed an interesting bug when attempting to use this library with the palette feature enabled. Everything appears to work great when using palette 0.6 but when I attempt to use palette 0.5 I get trait not implemented for Calculate and Hamerly on Lab.
This is main.rs
use kmeans_colors::get_kmeans_hamerly;
use palette::{Lab, Pixel, Srgb};
fn main() {
let bytes = [0u8, 0, 255, 0, 255, 128];
let lab: Vec<Lab> = Srgb::from_raw_slice(&bytes)
.iter()
.map(|x| x.into_format().into())
.collect();
let run_result = get_kmeans_hamerly(8, 20, 5.0, true, &lab, 0);
println!("{:?}", run_result)
}
and Cargo.toml
[package]
name = "test-kmeans"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.kmeans_colors]
version = "0.4.0"
features = ["palette_color"]
default-features = false
[dependencies.palette]
version = "0.5"
default-features = false
features = ["std"]
This works well, but once you change the palette version to 0.6 and update the code for generating the vector of colors it no longer functions as expected.
First, this is a great Implementation. I haven't seen any other implementations of the Hamerly method.
Couple questions:
In trying to use get_kmeans()
, I attempted to run it on a list of image::Rgb
pixels, but it seems to only take palette::lab::Lab
or palette::rgb::rgb::Rgb
. Is it possible this could be implemented for image::Rgb
so I can skip using the palette
library?
Allow for saving an image that has the k-means colors in swatches. Additionally, there should be an option to append it to the image output, and allow for the swatch sizes to be proportional to their percentage representation in the image (--pct
).
This should speed up the calculation since it eliminates some of the distance checks.
It would be nice to add benchmarks with this change to see the difference.
Would be nice to update to the latest version.
Wanting to try your tool, I ran cargo install kmeans_colors
.
It started to download and build the version 0.3.3 from crate.io.
But it ultimately failed, with the following errors:
(...)
Compiling approx v0.3.2
Compiling image v0.23.11
Compiling palette_derive v0.5.0
Compiling structopt-derive v0.4.13
Compiling structopt v0.3.20
Compiling kmeans_colors v0.3.3
error[E0433]: failed to resolve: could not find `CompressionType` in `png`
--> /Users/bvermont/.cargo/registry/src/github.com-1ecc6299db9ec823/kmeans_colors-0.3.3/src/bin/kmeans_colors/utils.rs:89:25
|
89 | image::png::CompressionType::Best,
| ^^^^^^^^^^^^^^^ could not find `CompressionType` in `png`
error[E0433]: failed to resolve: could not find `FilterType` in `png`
--> /Users/bvermont/.cargo/registry/src/github.com-1ecc6299db9ec823/kmeans_colors-0.3.3/src/bin/kmeans_colors/utils.rs:90:25
|
90 | image::png::FilterType::NoFilter,
| ^^^^^^^^^^ could not find `FilterType` in `png`
error[E0433]: failed to resolve: could not find `CompressionType` in `png`
--> /Users/bvermont/.cargo/registry/src/github.com-1ecc6299db9ec823/kmeans_colors-0.3.3/src/bin/kmeans_colors/utils.rs:127:25
|
127 | image::png::CompressionType::Best,
| ^^^^^^^^^^^^^^^ could not find `CompressionType` in `png`
error[E0433]: failed to resolve: could not find `FilterType` in `png`
--> /Users/bvermont/.cargo/registry/src/github.com-1ecc6299db9ec823/kmeans_colors-0.3.3/src/bin/kmeans_colors/utils.rs:128:25
|
128 | image::png::FilterType::NoFilter,
| ^^^^^^^^^^ could not find `FilterType` in `png`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0433`.
error: failed to compile `kmeans_colors v0.3.3`, intermediate artifacts can be found at `/var/folders/s7/jfsvj8x90tq9yf84p62t5vqd4y5v7f/T/cargo-installBCF7Ud`
Caused by:
could not compile `kmeans_colors`.
To learn more, run the command again with --verbose.
If I check out the repo, building works fine though. So my hunch is that what is published on crate.io might be broken?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.