Skip to content

Commit

Permalink
refactor: using opencv for faster convolution
Browse files Browse the repository at this point in the history
  • Loading branch information
martibosch committed Feb 18, 2024
1 parent f8e24ca commit 96455c0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions detectree/image_descriptor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions detectree/pixel_features.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 96455c0

Please sign in to comment.