From 22790053ebb4586315000f152464827b2f34a46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bosch?= Date: Sun, 18 Feb 2024 11:27:45 +0100 Subject: [PATCH] refactor: using opencv for faster convolution --- detectree/image_descriptor.py | 4 ++-- detectree/pixel_features.py | 7 +++++-- pyproject.toml | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/detectree/image_descriptor.py b/detectree/image_descriptor.py index d614987..3f426c3 100644 --- a/detectree/image_descriptor.py +++ b/detectree/image_descriptor.py @@ -1,7 +1,7 @@ """Compute image descriptors.""" +import cv2 import numpy as np from PIL import Image -from scipy import ndimage as ndi from skimage import color from skimage.util import shape from sklearn import preprocessing @@ -60,7 +60,7 @@ def compute_image_descriptor(img_rgb, kernels, response_bins_per_axis, num_color ) for i, kernel in enumerate(kernels): - filter_response = ndi.convolve(img_gray, kernel) + filter_response = cv2.filter2D(img_gray, ddepth=-1, kernel=kernel) response_bins = shape.view_as_blocks(filter_response, block_shape) bin_sum = response_bins.sum(axis=(2, 3)).flatten() gist_descr[i * num_blocks : (i + 1) * num_blocks] = bin_sum diff --git a/detectree/pixel_features.py b/detectree/pixel_features.py index 6070d2e..225fa15 100644 --- a/detectree/pixel_features.py +++ b/detectree/pixel_features.py @@ -1,8 +1,8 @@ """Build pixel features.""" - import glob from os import path +import cv2 import dask import numpy as np from dask import diagnostics @@ -180,7 +180,10 @@ def build_features_from_arr(self, img_rgb): # theta = orientation / num_orientations * np.pi theta = orientation * 180 / self.num_orientations oriented_kernel_arr = ndi.interpolation.rotate(base_kernel_arr, theta) - img_filtered = ndi.convolve(img_lab_l, oriented_kernel_arr) + # img_filtered = ndi.convolve(img_lab_l, oriented_kernel_arr) + img_filtered = cv2.filter2D( + img_lab_l, ddepth=-1, kernel=oriented_kernel_arr + ) img_filtered_vec = img_filtered.flatten() X[ :, self.num_color_features + i * self.num_orientations + j diff --git a/pyproject.toml b/pyproject.toml index 0f3b113..0f1bdfb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ dependencies = [ "joblib", "laspy >= 2.0.0", "numpy >= 1.15", + "opencv-python >= 4.0.0", "pandas >= 0.23", "pymaxflow >= 1.0.0", "rasterio >= 1.0.0",