Commit c15cc83f authored by John's avatar John

Now doing k-means clustering

parent 3598ee4e
...@@ -127,6 +127,7 @@ version = "0.1.0" ...@@ -127,6 +127,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"palette 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "palette 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
...@@ -164,6 +165,14 @@ name = "lzw" ...@@ -164,6 +165,14 @@ name = "lzw"
version = "0.10.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "matrixmultiply"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rawpointer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.2.1" version = "0.2.1"
...@@ -174,6 +183,16 @@ name = "nodrop" ...@@ -174,6 +183,16 @@ name = "nodrop"
version = "0.1.12" version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.2.2" version = "0.2.2"
...@@ -307,6 +326,16 @@ dependencies = [ ...@@ -307,6 +326,16 @@ dependencies = [
"proc-macro2 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "rand"
version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.4.3" version = "0.4.3"
...@@ -317,6 +346,11 @@ dependencies = [ ...@@ -317,6 +346,11 @@ dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "rawpointer"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.0.2" version = "1.0.2"
...@@ -338,6 +372,25 @@ dependencies = [ ...@@ -338,6 +372,25 @@ dependencies = [
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "rulinalg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"matrixmultiply 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rusty-machine"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
"rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "scoped_threadpool" name = "scoped_threadpool"
version = "0.1.9" version = "0.1.9"
...@@ -414,8 +467,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -414,8 +467,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084"
"checksum matrixmultiply 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cac1a66eab356036af85ea093101a14223dc6e3f4c02a59b7d572e5b93270bf7"
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
"checksum num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d2c31b75c36a993d30c7a13d70513cb93f02acafdd5b7ba250f9b0e18615de7" "checksum num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d2c31b75c36a993d30c7a13d70513cb93f02acafdd5b7ba250f9b0e18615de7"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124" "checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124"
...@@ -431,9 +486,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -431,9 +486,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f54b9600d584d3b8a739e1662a595fab051329eff43f20e7d8cc22872962145b" "checksum png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f54b9600d584d3b8a739e1662a595fab051329eff43f20e7d8cc22872962145b"
"checksum proc-macro2 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ee5697238f0d893c7f0ecc59c0999f18d2af85e424de441178bcacc9f9e6cf67" "checksum proc-macro2 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ee5697238f0d893c7f0ecc59c0999f18d2af85e424de441178bcacc9f9e6cf67"
"checksum quote 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ed7d650913520df631972f21e104a4fa2f9c82a14afc65d17b388a2e29731e7c" "checksum quote 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ed7d650913520df631972f21e104a4fa2f9c82a14afc65d17b388a2e29731e7c"
"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1"
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
"checksum rawpointer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebac11a9d2e11f2af219b8b8d833b76b1ea0e054aa0e8d8e9e4cbde353bdf019"
"checksum rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "df7a791f788cb4c516f0e091301a29c2b71ef680db5e644a7d68835c8ae6dbfa" "checksum rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "df7a791f788cb4c516f0e091301a29c2b71ef680db5e644a7d68835c8ae6dbfa"
"checksum rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b055d1e92aba6877574d8fe604a63c8b5df60f60e5982bf7ccbb1338ea527356" "checksum rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b055d1e92aba6877574d8fe604a63c8b5df60f60e5982bf7ccbb1338ea527356"
"checksum rulinalg 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5670ce3717deb2d4436bdb29f07c549b56595f1fd8af1ca4682f1c1b1ac57f86"
"checksum rusty-machine 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "dee5358236bbd0835aeee9a8b04c8b9b0aaea243a1a1a71253957b4606fb9171"
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
......
...@@ -14,3 +14,4 @@ path = "src/pi.rs" ...@@ -14,3 +14,4 @@ path = "src/pi.rs"
[dependencies] [dependencies]
palette = "0.4.1" palette = "0.4.1"
image = "0.19.0" image = "0.19.0"
rusty-machine = "0.5.4"
extern crate image; extern crate image;
extern crate rusty_machine;
use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::{KMeansClassifier};
use rusty_machine::learning::UnSupModel;
//extern crate palette; //extern crate palette;
use image::{GenericImage, ImageBuffer}; use image::{GenericImage, ImageBuffer};
...@@ -13,25 +19,71 @@ fn modify(val:u8, percent:u32)-> u8{ ...@@ -13,25 +19,71 @@ fn modify(val:u8, percent:u32)-> u8{
ret.min(255) as u8 ret.min(255) as u8
} }
fn convert(centroids:&Matrix<f64>){
for i in 0..centroids.rows() {
println!("{:?}",centroids.get_row(i).unwrap())
}
}
fn main() { fn main() {
let img = image::open("img.jpg").unwrap(); let img = image::open("smalltest.jpg").unwrap();
let dimensions = img.dimensions(); let dimensions = img.dimensions();
let mut buff:image::RgbaImage = ImageBuffer::new(dimensions.0,dimensions.1); let mut buff:image::RgbaImage = ImageBuffer::new(dimensions.0,dimensions.1);
let pixeliter = img.pixels() ;
for pixel in pixeliter{ let mut pixelData = Vec::new();
let pixeliter = img.pixels() ;
for pixel in pixeliter {
//get_color(pixel.2); //get_color(pixel.2);
let mut newpix = pixel.clone(); let mut newpix = pixel.clone();
let mut red = newpix.2[0]; pixelData.push(newpix.2[0] as f64);
let mut green = newpix.2[1]; pixelData.push(newpix.2[1] as f64);
let mut blue = newpix.2[2]; pixelData.push(newpix.2[2] as f64);
newpix.2[0] = modify(green, 200);
newpix.2[1]=blue;
newpix.2[2]=red ;
buff.put_pixel(newpix.0,newpix.1,newpix.2);
} }
println!("{:?}",(dimensions.0*dimensions.1) as usize);
let a = Matrix::new((dimensions.0*dimensions.1) as usize,3,pixelData);
let mut model = KMeansClassifier::new(5);
model.train(&a).unwrap();
println!("{:?}",model);
let centroids = model.centroids().as_ref().unwrap();
println!("Model Centroids:\n{:.0}", centroids);
convert(centroids);
// let slice = MatrixSlice::from_matrix(&centroids, [0,0], 1, 3);
let slice = centroids.get_row(0).unwrap();
println!("{:?}", slice);
// let a = model.predict(&test_inputs).unwrap();
buff.save("test.png").unwrap(); // println!("{:?}", e);
// println!("{:?}", m.data());
//
// let pixeliter = img.pixels() ;
// for pixel in pixeliter{
// //get_color(pixel.2);
// let mut newpix = pixel.clone();
// let mut red = newpix.2[0];
// let mut green = newpix.2[1];
// let mut blue = newpix.2[2];
//
// newpix.2[0] = modify(green, 200);
// newpix.2[1]=blue;
// newpix.2[2]=red ;
//
// buff.put_pixel(newpix.0,newpix.1,newpix.2);
// }
// println!("test");
//
//
// buff.save("test.png").unwrap();
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment