-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
compute_histogram
python bindings (#64)
* rename kornia-py * include python bindings
- Loading branch information
Showing
26 changed files
with
104 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import timeit | ||
|
||
import cv2 | ||
import kornia_rs as K | ||
import numpy as np | ||
|
||
image_path = "tests/data/dog.jpeg" | ||
N = 5000 # number of iterations | ||
|
||
img = K.read_image_jpeg(image_path)[..., :1] | ||
|
||
|
||
# 0.04 ms :) | ||
def hist_opencv(image: np.ndarray) -> np.ndarray: | ||
return cv2.calcHist([image], [0], None, [256], [0, 256]) | ||
|
||
|
||
# 0.17 ms :( | ||
def hist_kornia(image: np.ndarray) -> np.ndarray: | ||
return K.compute_histogram(image, num_bins=256) | ||
|
||
|
||
tests = [ | ||
{ | ||
"name": "OpenCV", | ||
"stmt": "hist_opencv(image)", | ||
"setup": "from __main__ import hist_opencv", | ||
"globals": {"image": img}, | ||
}, | ||
{ | ||
"name": "Kornia", | ||
"stmt": "hist_kornia(image)", | ||
"setup": "from __main__ import hist_kornia", | ||
"globals": {"image": img}, | ||
}, | ||
] | ||
|
||
for test in tests: | ||
timer = timeit.Timer( | ||
stmt=test["stmt"], setup=test["setup"], globals=test["globals"] | ||
) | ||
print(f"{test['name']}: {timer.timeit(N)/ N * 1e3:.2f} ms") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use pyo3::prelude::*; | ||
|
||
use crate::image::{FromPyImage, PyImage}; | ||
use kornia_rs::image::Image; | ||
|
||
/// Compute the pixel-wise histogram of an image. | ||
/// -- | ||
/// | ||
/// This function computes the histogram of an image with a given number of bins. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `image` - The input image to compute the histogram with shape (H, W, 1) and dtype uint8. | ||
/// * `num_bins` - The number of bins to use for the histogram. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A vector of size `num_bins` containing the histogram. | ||
#[pyfunction] | ||
pub fn compute_histogram(image: PyImage, num_bins: usize) -> PyResult<Vec<usize>> { | ||
let image = Image::from_pyimage(image) | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(format!("{}", e)))?; | ||
|
||
let histogram = kornia_rs::histogram::compute_histogram(&image, num_bins) | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(format!("{}", e)))?; | ||
|
||
Ok(histogram) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import annotations | ||
import kornia_rs as K | ||
|
||
import numpy as np | ||
|
||
|
||
def test_histogram(): | ||
# load an image with libjpeg-turbo | ||
img = np.array([0, 2, 4, 128, 130, 132, 254, 255, 255], dtype=np.uint8).reshape( | ||
3, 3, 1 | ||
) | ||
|
||
img_histogram: list[int] = K.compute_histogram(img, num_bins=3) | ||
|
||
assert len(img_histogram) == 3 | ||
assert img_histogram[0] == 3 | ||
assert img_histogram[1] == 3 | ||
assert img_histogram[2] == 3 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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