Skip to content

Commit

Permalink
Added new ops
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 26, 2024
1 parent cc34e1b commit 2e34c81
Show file tree
Hide file tree
Showing 10 changed files with 663 additions and 50 deletions.
34 changes: 12 additions & 22 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 @@ -2,7 +2,7 @@ workspace = { members = ["app"] }

[package]
name = "fast_morphology"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Fast morphological operations for images"
readme = "README.md"
Expand All @@ -16,7 +16,6 @@ repository = "https://github.com/awxkee/fast_morphology.git"
exclude = ["*.jpg", "*.png"]

[dependencies]
colorutils-rs = "0.5.12"
num-traits = "0.2.19"
rayon = "1.10.0"
image = { version = "0.25.0", optional = true, default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
image = "0.25.2"
fast_morphology = {path = "../", features = ["image"]}
imageproc = "0.25.0"
opencv = {version = "0.93.0", features = ["imgproc"]}
opencv = {version = "0.93.0", features = ["imgproc", "clang-runtime"]}

[dev-dependencies]
criterion = {version = "0.5.1", features = ["html_reports"]}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use opencv::core::{
Mat, MatTrait, MatTraitConstManual, Point, Scalar, BORDER_REPLICATE, CV_8U, CV_8UC3,
};
use opencv::imgproc;
use opencv::imgproc::{MORPH_BLACKHAT, MORPH_TOPHAT};
use std::time::Instant;

fn circle_se(radius: usize) -> Vec<u8> {
Expand Down Expand Up @@ -66,7 +67,7 @@ fn gaussian_kernel(size: usize, sigma: f32) -> Vec<Vec<f32>> {
}

fn main() {
let radius_size = 35;
let radius_size = 67;
let mut structuring_element = circle_se(radius_size);

opencv::core::set_use_opencl(false).expect("Failed to disable OpenCL");
Expand Down Expand Up @@ -208,9 +209,10 @@ fn main() {
let exec_time = Instant::now();

let mut dst_mat = Mat::default();
imgproc::erode(
imgproc::morphology_ex(
&mat,
&mut dst_mat,
MORPH_TOPHAT,
&kernel,
Point::new(-1, -1),
1,
Expand All @@ -227,7 +229,7 @@ fn main() {

let new_image = morphology_image(
img,
MorphExOp::Closing,
MorphExOp::TopHat,
&structuring_element,
KernelShape::new(se_size, se_size),
BorderMode::default(),
Expand Down
70 changes: 70 additions & 0 deletions src/difference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) Radzivon Bartoshyk. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use num_traits::SaturatingSub;
use std::ops::Sub;

pub trait MorphGradient<T> {
fn morph_gradient(dilation: &[T], erosion: &[T], dst: &mut [T]);
}

fn make_morph_gradient_sat<T>(dilation: &[T], erosion: &[T], dst: &mut [T])
where
T: SaturatingSub + Default + Clone + Copy,
{
for ((dilation, erosion), dst) in dilation.iter().zip(erosion.iter()).zip(dst.iter_mut()) {
*dst = dilation.saturating_sub(erosion);
}
}

fn make_morph_gradient<T>(dilation: &[T], erosion: &[T], dst: &mut [T])
where
T: Sub<Output = T> + Default + Clone + Copy,
{
for ((dilation, erosion), dst) in dilation.iter().zip(erosion.iter()).zip(dst.iter_mut()) {
*dst = *dilation - *erosion;
}
}

impl MorphGradient<u8> for u8 {
fn morph_gradient(dilation: &[u8], erosion: &[u8], dst: &mut [u8]) {
make_morph_gradient_sat(dilation, erosion, dst)
}
}

impl MorphGradient<u16> for u16 {
fn morph_gradient(dilation: &[u16], erosion: &[u16], dst: &mut [u16]) {
make_morph_gradient_sat(dilation, erosion, dst)
}
}

impl MorphGradient<f32> for f32 {
fn morph_gradient(dilation: &[f32], erosion: &[f32], dst: &mut [f32]) {
make_morph_gradient(dilation, erosion, dst)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod se_scan;
mod structuring_element;
mod thread_policy;
mod unsafe_slice;
mod difference;

pub use border_mode::BorderMode;
#[cfg(feature = "image")]
Expand Down
Loading

0 comments on commit 2e34c81

Please sign in to comment.