merge: R OpenCL UMat

This commit is contained in:
2026-05-04 22:42:48 +02:00
+51 -5
View File
@@ -50,6 +50,31 @@ N_BINS = 8 # default: orientamento mod π (no polarity)
N_BINS_POL = 16 # use_polarity=True: orientamento mod 2π (con polarity) N_BINS_POL = 16 # use_polarity=True: orientamento mod 2π (con polarity)
def opencl_available() -> bool:
"""Ritorna True se OpenCV ha backend OpenCL disponibile (GPU)."""
try:
return bool(cv2.ocl.haveOpenCL())
except Exception:
return False
def set_gpu_enabled(enabled: bool) -> bool:
"""Abilita/disabilita backend OpenCL globale di OpenCV.
Quando attivato, Sobel/dilate/warpAffine usano UMat con dispatch
automatico a kernel GPU (Intel UHD, AMD, NVIDIA via OpenCL ICD).
Speedup tipico: 1.5-3x su Sobel+dilate per scene 1920x1080,
overhead trascurabile per scene < 640px (transfer CPU<->GPU domina).
Halcon-equivalent: 'find_shape_model' con backend GPU integrato.
Ritorna True se l'attivazione e' riuscita.
"""
if not opencl_available():
return False
cv2.ocl.setUseOpenCL(bool(enabled))
return cv2.ocl.useOpenCL()
def _poly_iou(p1: np.ndarray, p2: np.ndarray) -> float: def _poly_iou(p1: np.ndarray, p2: np.ndarray) -> float:
"""IoU tra due poligoni convessi (4 vertici, float32) via cv2.intersectConvexConvex. """IoU tra due poligoni convessi (4 vertici, float32) via cv2.intersectConvexConvex.
@@ -150,6 +175,7 @@ class LineShapeMatcher:
top_score_factor: float = 0.5, top_score_factor: float = 0.5,
n_threads: int | None = None, n_threads: int | None = None,
use_polarity: bool = False, use_polarity: bool = False,
use_gpu: bool = False,
) -> None: ) -> None:
self.num_features = num_features self.num_features = num_features
self.weak_grad = weak_grad self.weak_grad = weak_grad
@@ -169,6 +195,11 @@ class LineShapeMatcher:
# template e' direzionale. # template e' direzionale.
self.use_polarity = use_polarity self.use_polarity = use_polarity
self._n_bins = N_BINS_POL if use_polarity else N_BINS self._n_bins = N_BINS_POL if use_polarity else N_BINS
# GPU offload per Sobel/dilate/warpAffine via cv2.UMat (OpenCL).
# Effettivo solo se opencl_available(); altrimenti silent fallback CPU.
self.use_gpu = bool(use_gpu and opencl_available())
if self.use_gpu:
cv2.ocl.setUseOpenCL(True)
self.variants: list[_Variant] = [] self.variants: list[_Variant] = []
self.template_size: tuple[int, int] = (0, 0) self.template_size: tuple[int, int] = (0, 0)
@@ -189,10 +220,15 @@ class LineShapeMatcher:
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img return img
def _gradient(self, gray: np.ndarray) -> tuple[np.ndarray, np.ndarray]: def _gradient(self, gray) -> tuple[np.ndarray, np.ndarray]:
# Accetta np.ndarray o cv2.UMat (per path GPU OpenCL).
gx = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3) gx = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3)
gy = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3) gy = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
mag = cv2.magnitude(gx, gy) mag = cv2.magnitude(gx, gy)
# Quantizzazione orientation richiede CPU array (np ops): scarica
# da GPU se necessario.
if isinstance(gx, cv2.UMat):
gx = gx.get(); gy = gy.get(); mag = mag.get()
ang = np.arctan2(gy, gx) # [-π, π] ang = np.arctan2(gy, gx) # [-π, π]
if self.use_polarity: if self.use_polarity:
# Mod 2π: bin 0..15 codifica direzione + polarity edge. # Mod 2π: bin 0..15 codifica direzione + polarity edge.
@@ -601,19 +637,29 @@ class LineShapeMatcher:
"""Spread bitmap: bit b acceso dove bin b è presente nel raggio. """Spread bitmap: bit b acceso dove bin b è presente nel raggio.
dtype: uint8 per N_BINS=8, uint16 per N_BINS_POL=16 (use_polarity). dtype: uint8 per N_BINS=8, uint16 per N_BINS_POL=16 (use_polarity).
Se use_gpu=True: Sobel + dilate via cv2.UMat (OpenCL kernel GPU).
""" """
mag, bins = self._gradient(gray) if self.use_gpu and not isinstance(gray, cv2.UMat):
gray_in = cv2.UMat(np.ascontiguousarray(gray))
else:
gray_in = gray
mag, bins = self._gradient(gray_in)
valid = mag >= self.weak_grad valid = mag >= self.weak_grad
k = 2 * self.spread_radius + 1 k = 2 * self.spread_radius + 1
kernel = np.ones((k, k), dtype=np.uint8) kernel = np.ones((k, k), dtype=np.uint8)
H, W = gray.shape H, W = (gray.shape if isinstance(gray, np.ndarray)
else (gray.get().shape[0], gray.get().shape[1]))
nb = self._n_bins nb = self._n_bins
dtype = np.uint16 if nb > 8 else np.uint8 dtype = np.uint16 if nb > 8 else np.uint8
spread = np.zeros((H, W), dtype=dtype) spread = np.zeros((H, W), dtype=dtype)
for b in range(nb): for b in range(nb):
mask_b = ((bins == b) & valid).astype(np.uint8) mask_b = ((bins == b) & valid).astype(np.uint8)
d = cv2.dilate(mask_b, kernel) if self.use_gpu:
spread |= (d.astype(dtype) << b) d = cv2.dilate(cv2.UMat(mask_b), kernel)
d_np = d.get()
else:
d_np = cv2.dilate(mask_b, kernel)
spread |= (d_np.astype(dtype) << b)
return spread return spread
@staticmethod @staticmethod