Skip to content

Commit

Permalink
Added SSE, improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 24, 2024
1 parent aed0049 commit c09e7e5
Show file tree
Hide file tree
Showing 48 changed files with 4,197 additions and 253 deletions.
58 changes: 9 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ repository = "https://github.com/awxkee/fast_morphology.git"
exclude = ["*.jpg", "*.png"]

[dependencies]
colorutils-rs = {path = "../../RustroverProjects/colorutils-rs"}
nalgebra = "0.33.0"
colorutils-rs = "0.5.12"
num-traits = "0.2.19"
rayon = "1.10.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ M3 Pro, NEON dilation RGB image 2731x4096 with specified kernel size

| SE | 9x9 | 15x15 | 21x21 | 41x41 | 61x61 |
|--------|---------|---------|----------|----------|----------|
| FM | 41.85ms | 71.51ms | 90.88ms | 213.45ms | 377.64ms |
| FM | 35.41ms | 68.53ms | 85.31ms | 208.47ms | 377.04ms |
| OpenCV | 20.65ms | 54.43ms | 107.58ms | 418.66ms | 905.21ms |

M3 Pro, NEON dilation RGBA image 2731x4096 with specified kernel size

| SE | 9x9 | 15x15 | 21x21 | 41x41 | 61x61 |
|--------|---------|---------|----------|----------|----------|
| FM | 47.47ms | 84.64ms | 111.79ms | 274.01ms | 515.54ms |
| FM | 48.25ms | 81.12ms | 111.79ms | 274.01ms | 515.54ms |
| OpenCV | 30.22ms | 72.63ms | 138.69ms | 555.51ms | 1.19s |

This project is licensed under either of
Expand Down
1 change: 0 additions & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ image = "0.25.2"
fast_morphology = {path = "../"}
imageproc = "0.25.0"
opencv = {version = "0.93.0", features = ["clang-runtime", "imgproc"]}
nalgebra = "0.33.0"

[dev-dependencies]
criterion = {version = "0.5.1", features = ["html_reports"]}
Expand Down
18 changes: 12 additions & 6 deletions app/benches/dilation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
*/

use criterion::{criterion_group, criterion_main, Criterion};
use fast_morphology::{dilate_rgb, dilate_rgba, BorderMode, ImageSize, KernelShape, MorphologyThreadingPolicy};
use fast_morphology::{
dilate_rgb, dilate_rgba, BorderMode, ImageSize, KernelShape, MorphologyThreadingPolicy,
};
use image::{EncodableLayout, GenericImageView, ImageReader};
use opencv::core::{Mat, MatTrait, Point, Scalar, BORDER_REPLICATE, CV_8U, CV_8UC3, CV_8UC4};
use opencv::imgproc;
Expand Down Expand Up @@ -162,7 +164,7 @@ fn exec_bench_rgba(c: &mut Criterion, size: usize) {
CV_8U,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
.unwrap();
unsafe {
for (index, &byte) in structuring_element_15.iter().enumerate() {
kernel_15.data_mut().add(index).write(byte);
Expand All @@ -183,7 +185,7 @@ fn exec_bench_rgba(c: &mut Criterion, size: usize) {
BorderMode::default(),
MorphologyThreadingPolicy::Adaptive,
)
.unwrap();
.unwrap();
})
},
);
Expand All @@ -194,7 +196,7 @@ fn exec_bench_rgba(c: &mut Criterion, size: usize) {
CV_8UC4,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
.unwrap();
unsafe {
for (index, &byte) in rgb_bytes.iter().enumerate() {
mat.data_mut().add(index).write(byte);
Expand All @@ -206,7 +208,7 @@ fn exec_bench_rgba(c: &mut Criterion, size: usize) {
"OpenCV, RGBA Image dilation: SE {}x{}",
se_size_15, se_size_15
)
.as_str(),
.as_str(),
|b| {
b.iter(|| {
let mut dst_mat = Mat::default();
Expand All @@ -219,13 +221,17 @@ fn exec_bench_rgba(c: &mut Criterion, size: usize) {
BORDER_REPLICATE,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
.unwrap();
})
},
);
}

pub fn criterion_benchmark(c: &mut Criterion) {
opencv::core::set_use_opencl(false).expect("Failed to disable OpenCL");
opencv::core::set_use_ipp(true).expect("Failed to disable IPP");
opencv::core::set_use_optimized(false).expect("Failed to disable opts");

exec_bench_rgb(c, 4);
exec_bench_rgb(c, 7);
exec_bench_rgb(c, 10);
Expand Down
104 changes: 53 additions & 51 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use fast_morphology::{dilate, dilate_rgb, dilate_rgba, BorderMode, ImageSize, KernelShape, MorphologyThreadingPolicy};
use fast_morphology::{
dilate, dilate_rgb, dilate_rgba, BorderMode, ImageSize, KernelShape, MorphologyThreadingPolicy,
};
use image::{EncodableLayout, GenericImageView, ImageReader};
use opencv::core::{
Mat, MatTrait, MatTraitConstManual, Point, Scalar, BORDER_CONSTANT, BORDER_ISOLATED,
Expand Down Expand Up @@ -64,12 +66,12 @@ fn gaussian_kernel(size: usize, sigma: f32) -> Vec<Vec<f32>> {
}

fn main() {
let radius_size = 15;
let radius_size = 5;
let mut structuring_element = circle_se(radius_size);

opencv::core::set_use_opencl(false).expect("Failed to disable OpenCL");
opencv::core::set_use_ipp(true).expect("Failed to disable IPP");
opencv::core::set_use_optimized(false).expect("Failed to disable opts");
// opencv::core::set_use_opencl(false).expect("Failed to disable OpenCL");
// opencv::core::set_use_ipp(true).expect("Failed to disable IPP");
// opencv::core::set_use_optimized(false).expect("Failed to disable opts");

let se_size = radius_size * 2 + 1;
let full_size = se_size;
Expand All @@ -78,7 +80,7 @@ fn main() {
println!("{:?}", Vec::from(elements));
}

let img = ImageReader::open("./assets/fruits.jpg")
let img = ImageReader::open("./assets/ebelhard.jpg")
.unwrap()
.decode()
.unwrap();
Expand Down Expand Up @@ -161,7 +163,7 @@ fn main() {
a[2] = dst_3;
}

// let rgba_image = transient_rgba.as_bytes();
let rgba_image = transient_rgba.as_bytes();
let mut dst = vec![0u8; saved_origin.len()];

let exec_time = Instant::now();
Expand All @@ -178,46 +180,46 @@ fn main() {

println!("rgb exec time {:?}", exec_time.elapsed());

let mut mat = Mat::new_rows_cols_with_default(
dimensions.1 as i32,
dimensions.0 as i32,
CV_8UC3,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
unsafe {
for (index, &byte) in saved_origin.iter().enumerate() {
mat.data_mut().add(index).write(byte);
}
}
let mut kernel = Mat::new_rows_cols_with_default(
full_size as i32,
full_size as i32,
CV_8U,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
unsafe {
for (index, &byte) in structuring_element.iter().enumerate() {
kernel.data_mut().add(index).write(byte);
}
}
// let mut mat = Mat::new_rows_cols_with_default(
// dimensions.1 as i32,
// dimensions.0 as i32,
// CV_8UC3,
// Scalar::new(0., 0., 0., 0.),
// )
// .unwrap();
// unsafe {
// for (index, &byte) in saved_origin.iter().enumerate() {
// mat.data_mut().add(index).write(byte);
// }
// }
// let mut kernel = Mat::new_rows_cols_with_default(
// full_size as i32,
// full_size as i32,
// CV_8U,
// Scalar::new(0., 0., 0., 0.),
// )
// .unwrap();
// unsafe {
// for (index, &byte) in structuring_element.iter().enumerate() {
// kernel.data_mut().add(index).write(byte);
// }
// }

let exec_time = Instant::now();

let mut dst_mat = Mat::default();
imgproc::dilate(
&mat,
&mut dst_mat,
&kernel,
Point::new(-1, -1),
1,
BORDER_REPLICATE,
Scalar::new(0., 0., 0., 0.),
)
.unwrap();
// let mut dst_mat = Mat::default();
// imgproc::dilate(
// &mat,
// &mut dst_mat,
// &kernel,
// Point::new(-1, -1),
// 1,
// BORDER_REPLICATE,
// Scalar::new(0., 0., 0., 0.),
// )
// .unwrap();

let open_cv_bytes = dst_mat.data_bytes().unwrap();
// let open_cv_bytes = dst_mat.data_bytes().unwrap();

println!("opencv exec time {:?}", exec_time.elapsed());

Expand All @@ -239,12 +241,12 @@ fn main() {
)
.unwrap();

image::save_buffer(
"converted_opencv.png",
&open_cv_bytes,
dimensions.0,
dimensions.1,
image::ColorType::Rgb8,
)
.unwrap();
// image::save_buffer(
// "converted_opencv.png",
// &open_cv_bytes,
// dimensions.0,
// dimensions.1,
// image::ColorType::Rgb8,
// )
// .unwrap();
}
Loading

0 comments on commit c09e7e5

Please sign in to comment.