@@ -32,7 +32,7 @@ def add_weighted_simsimd(img1: np.ndarray, weight1: float, img2: np.ndarray, wei
32
32
original_dtype = img1 .dtype
33
33
34
34
if img2 .dtype != original_dtype :
35
- img2 = clip (img2 .astype (original_dtype ), original_dtype )
35
+ img2 = clip (img2 .astype (original_dtype ), original_dtype , inplace = True )
36
36
37
37
return np .frombuffer (
38
38
ss .wsum (img1 .reshape (- 1 ), img2 .astype (original_dtype ).reshape (- 1 ), alpha = weight1 , beta = weight2 ),
@@ -96,7 +96,7 @@ def apply_lut(
96
96
97
97
num_channels = img .shape [- 1 ]
98
98
luts = create_lut_array (dtype , value , operation )
99
- return cv2 .merge ([sz_lut (img [:, :, i ], clip (luts [i ], dtype ), inplace ) for i in range (num_channels )])
99
+ return cv2 .merge ([sz_lut (img [:, :, i ], clip (luts [i ], dtype , inplace = False ), inplace ) for i in range (num_channels )])
100
100
101
101
102
102
def prepare_value_opencv (
@@ -212,7 +212,7 @@ def multiply(img: np.ndarray, value: ValueType, inplace: bool = False) -> np.nda
212
212
213
213
214
214
@preserve_channel_dim
215
- def add_opencv (img : np .ndarray , value : np .ndarray | float ) -> np .ndarray :
215
+ def add_opencv (img : np .ndarray , value : np .ndarray | float , inplace : bool = False ) -> np .ndarray :
216
216
value = prepare_value_opencv (img , value , "add" )
217
217
218
218
# Convert to float32 if:
@@ -225,7 +225,9 @@ def add_opencv(img: np.ndarray, value: np.ndarray | float) -> np.ndarray:
225
225
if needs_float :
226
226
return cv2 .add (img .astype (np .float32 ), value if isinstance (value , (int , float )) else value .astype (np .float32 ))
227
227
228
- return cv2 .add (img , value )
228
+ # Use img as the destination array if inplace=True
229
+ dst = img if inplace else None
230
+ return cv2 .add (img , value , dst = dst )
229
231
230
232
231
233
def add_numpy (img : np .ndarray , value : float | np .ndarray ) -> np .ndarray :
@@ -237,20 +239,20 @@ def add_lut(img: np.ndarray, value: np.ndarray | float, inplace: bool) -> np.nda
237
239
238
240
239
241
@clipped
240
- def add_constant (img : np .ndarray , value : float ) -> np .ndarray :
241
- return add_opencv (img , value )
242
+ def add_constant (img : np .ndarray , value : float , inplace : bool = False ) -> np .ndarray :
243
+ return add_opencv (img , value , inplace )
242
244
243
245
244
246
@clipped
245
247
def add_vector (img : np .ndarray , value : np .ndarray , inplace : bool ) -> np .ndarray :
246
248
if img .dtype == np .uint8 :
247
249
return add_lut (img , value , inplace )
248
- return add_opencv (img , value )
250
+ return add_opencv (img , value , inplace )
249
251
250
252
251
253
@clipped
252
- def add_array (img : np .ndarray , value : np .ndarray ) -> np .ndarray :
253
- return add_opencv (img , value )
254
+ def add_array (img : np .ndarray , value : np .ndarray , inplace : bool = False ) -> np .ndarray :
255
+ return add_opencv (img , value , inplace )
254
256
255
257
256
258
def add (img : np .ndarray , value : ValueType , inplace : bool = False ) -> np .ndarray :
@@ -264,9 +266,9 @@ def add(img: np.ndarray, value: ValueType, inplace: bool = False) -> np.ndarray:
264
266
if img .dtype == np .uint8 :
265
267
value = int (value )
266
268
267
- return add_constant (img , value )
269
+ return add_constant (img , value , inplace )
268
270
269
- return add_vector (img , value , inplace ) if value .ndim == 1 else add_array (img , value )
271
+ return add_vector (img , value , inplace ) if value .ndim == 1 else add_array (img , value , inplace )
270
272
271
273
272
274
def normalize_numpy (img : np .ndarray , mean : float | np .ndarray , denominator : float | np .ndarray ) -> np .ndarray :
@@ -371,11 +373,17 @@ def add_weighted_numpy(img1: np.ndarray, weight1: float, img2: np.ndarray, weigh
371
373
372
374
@preserve_channel_dim
373
375
def add_weighted_opencv (img1 : np .ndarray , weight1 : float , img2 : np .ndarray , weight2 : float ) -> np .ndarray :
374
- return cv2 .addWeighted (img1 . astype ( np . float32 ) , weight1 , img2 . astype ( np . float32 ) , weight2 , 0 )
376
+ return cv2 .addWeighted (img1 , weight1 , img2 , weight2 , 0 )
375
377
376
378
377
379
@preserve_channel_dim
378
- def add_weighted_lut (img1 : np .ndarray , weight1 : float , img2 : np .ndarray , weight2 : float ) -> np .ndarray :
380
+ def add_weighted_lut (
381
+ img1 : np .ndarray ,
382
+ weight1 : float ,
383
+ img2 : np .ndarray ,
384
+ weight2 : float ,
385
+ inplace : bool = False ,
386
+ ) -> np .ndarray :
379
387
dtype = img1 .dtype
380
388
max_value = MAX_VALUES_BY_DTYPE [dtype ]
381
389
@@ -389,15 +397,15 @@ def add_weighted_lut(img1: np.ndarray, weight1: float, img2: np.ndarray, weight2
389
397
return np .zeros_like (img1 )
390
398
391
399
if weight1 == 1 and weight2 == 1 :
392
- return add_array (img1 , img2 )
400
+ return add_array (img1 , img2 , inplace )
393
401
394
402
lut1 = np .arange (0 , max_value + 1 , dtype = np .float32 ) * weight1
395
403
result1 = cv2 .LUT (img1 , lut1 )
396
404
397
405
lut2 = np .arange (0 , max_value + 1 , dtype = np .float32 ) * weight2
398
406
result2 = cv2 .LUT (img2 , lut2 )
399
407
400
- return add_opencv (result1 , result2 )
408
+ return add_opencv (result1 , result2 , inplace )
401
409
402
410
403
411
@clipped
@@ -437,7 +445,7 @@ def multiply_add_lut(img: np.ndarray, factor: ValueType, value: ValueType, inpla
437
445
num_channels = get_num_channels (img )
438
446
439
447
if isinstance (factor , (float , int )) and isinstance (value , (float , int )):
440
- lut = clip (np .arange (0 , max_value + 1 , dtype = np .float32 ) * factor + value , dtype )
448
+ lut = clip (np .arange (0 , max_value + 1 , dtype = np .float32 ) * factor + value , dtype , inplace = False )
441
449
return sz_lut (img , lut , inplace )
442
450
443
451
if isinstance (factor , np .ndarray ) and factor .shape != ():
@@ -446,7 +454,7 @@ def multiply_add_lut(img: np.ndarray, factor: ValueType, value: ValueType, inpla
446
454
if isinstance (value , np .ndarray ) and value .shape != ():
447
455
value = value .reshape (- 1 , 1 )
448
456
449
- luts = clip (np .arange (0 , max_value + 1 , dtype = np .float32 ) * factor + value , dtype )
457
+ luts = clip (np .arange (0 , max_value + 1 , dtype = np .float32 ) * factor + value , dtype , inplace = True )
450
458
451
459
return cv2 .merge ([sz_lut (img [:, :, i ], luts [i ], inplace ) for i in range (num_channels )])
452
460
@@ -641,7 +649,7 @@ def to_float(img: np.ndarray, max_value: float | None = None) -> np.ndarray:
641
649
def from_float_numpy (img : np .ndarray , target_dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
642
650
if max_value is None :
643
651
max_value = get_max_value (target_dtype )
644
- return clip (np .rint (img * max_value ), target_dtype )
652
+ return clip (np .rint (img * max_value ), target_dtype , inplace = True )
645
653
646
654
647
655
@preserve_channel_dim
0 commit comments