Seite 1 von 1

numpy.add Fehler

Verfasst: Mittwoch 7. Dezember 2022, 20:31
von nikh22
Hallo Leute,

Warum ergibt die Addition von Array G und B nicht 406 sondern 150 ? Hat das vielleicht was damit zu tun, dass sie über die split() Funktion erstellt wurden, weil im RGB Farbraum geht es ja von 0-255. Das heißt die 255 wären die Obergrenze und weil Python bei 0 anfängt zu zählen geht es bis 150, somit käme man im Ergebnis wieder auf 406.
Ist an der absurden Theorie was dran ? Weil wenn ich eigene Arrrays erzeuge funktioniert die elementweise Addition ohne Probleme.

Code: Alles auswählen

# Load image
import cv2
import numpy as np
from matplotlib import pyplot as plt


# Load image as BGR
name= 'bright-cloud-mean'
image_bgr = cv2.imread(r'bilder/test-patches/cloudy_sun_BRIGHT_CLOUD.jpg', cv2.IMREAD_COLOR)

(B, G, R) = cv2.split(image_bgr)
B_median = np.mean(B)
G_median = np.mean(G)
R_median = np.mean(R)
print('Typ von G Array = ', type(G),'Groeße= ',G.shape)
print('Typ von B Array = ', type(B),'Groeße= ',B.shape)

print('Matrix B erstes Element = ',  B[0][0])
print('Matrix G erstes Element = ',  G[0][0])
addition =  np.add(G, B)
print('Addition von B und G = ', addition.shape, addition[0][0])


array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[6, 7, 8], [9, 10, 11]])
print('Typ Array 1 = ',type(array1), array1.shape)
print('Typ Array 2 = ',type(array2), array2.shape)
addition_test = np.add(array1, array2)
print('Addition von Arr1 und Arr2 = ', addition_test.shape, addition_test)

Output:

Code: Alles auswählen

Typ von G Array =  <class 'numpy.ndarray'> Groeße=  (100, 100)
Typ von B Array =  <class 'numpy.ndarray'> Groeße=  (100, 100)
Matrix B erstes Element =  196
Matrix G erstes Element =  210
Addition von B und G =  (100, 100) 150
Typ Array 1 =  <class 'numpy.ndarray'> (2, 3)
Typ Array 2 =  <class 'numpy.ndarray'> (2, 3)
Addition von Arr1 und Arr2 =  (2, 3) [[ 7  9 11]
 [13 15 17]]

Re: numpy.add Fehler

Verfasst: Mittwoch 7. Dezember 2022, 20:59
von Sirius3
Das ist keine absurde Theorie, sondern das ganz normale Verhalten für Ganzzahltypen mit fixer Bit-Anzahl.
Wenn Du einen größeren Zahlenraum brauchst, mußt Du den Typ konvertieren (astype). Sich type(array) anzuschauen ist wenig hilfreich, was interessanter ist, ist array.dtype
Statt np.add nimmt man normalerweise +.

Re: numpy.add Fehler

Verfasst: Mittwoch 7. Dezember 2022, 21:08
von nikh22
Vielen lieben Dank! Werde ich direkt nachschauen.