logo

Python OpenCV | mètode cv2.imread().

OpenCV-Python és una biblioteca d'enllaços Python dissenyada per resoldre problemes de visió per ordinador. El mètode cv2.imread() carrega una imatge del fitxer especificat. Si la imatge no es pot llegir (a causa del fitxer que falta, permisos inadequats o format no compatible o no vàlid), aquest mètode retorna una matriu buida.

Exemple:



Python 3








import> cv2> # Load the image> image>=> cv2.imread(>'jg.webp'>)> # Display the image> cv2.imshow(>'Image'>, image)> # Wait for the user to press a key> cv2.waitKey(>0>)> # Close all windows> cv2.destroyAllWindows()>

>

>

Sortida:

Python OpenCV

Sintaxi del mètode OpenCV cv2.imread().

Sintaxi: cv2.imread(nom del fitxer, bandera)

Paràmetres:

  1. nom de fitxer: el camí al fitxer d'imatge.
  2. bandera: la bandera especifica la manera com s'ha de llegir la imatge.
  • cv2.IMREAD_COLOR – Especifica carregar una imatge en color. Es descuidarà qualsevol transparència de la imatge. És la bandera per defecte. Alternativament, podem passar un valor enter 1 per aquesta bandera.
  • cv2.IMREAD_GRAYSCALE – Especifica carregar una imatge en mode d'escala de grisos. Alternativament, podem passar un valor enter 0 per aquesta bandera.
  • cv2.IMREAD_UNCHANGED – Especifica carregar una imatge com a tal inclòs el canal alfa. Alternativament, podem passar un valor enter -1 per aquesta bandera.

Valor de retorn:

La funció cv2.imread() retorna una matriu NumPy si la imatge es carrega correctament.

reaccionar a l'estil en línia

Exemples del mètode OpenCV cv2.imread().

imatge cv2.imread().

Imatge d'entrada

Imatge de lectura de Python OpenCV – cv2 imread()

En aquest exemple, estem llegint la imatge com una imatge en color.

Python 3




import> cv2> # Load the image> image>=> cv2.imread(>'gfg.webp'>)> # Display the image> cv2.imshow(>'Image'>, image)> # Wait for the user to press a key> cv2.waitKey(>0>)> # Close all windows> cv2.destroyAllWindows()>

>

>

Sortida:

imatge cv2.imread().

imatge

Python OpenCV Llegeix la imatge en escala de grisos

En aquest exemple, estem llegint la imatge com una imatge en escala de grisos. Les imatges en color i en escala de grisos són acceptables com a entrada.

Python 3




import> cv2> # Load the image> image>=> cv2.imread(>'gfg.webp'>,cv2.IMREAD_GRAYSCALE)> # Display the image> cv2.imshow(>'Image'>, image)> # Wait for the user to press a key> cv2.waitKey(>0>)> # Close all windows> cv2.destroyAllWindows()>

>

>

Sortida:

imatge cv2.imread().

Imatge

Python OpenCV | Llegiu la imatge PNG amb transparència

En aquest exemple, estem llegint la imatge amb el canal de transparència.

Python 3




import> cv2> # Load the image> image>=> cv2.imread(>'gfg.webp'>,cv2.IMREAD_UNCHANGED)> # Display the image> cv2.imshow(>'Image'>, image)> # Wait for the user to press a key> cv2.waitKey(>0>)> # Close all windows> cv2.destroyAllWindows()>

>

>

Sortida:

imatge cv2.imread().

imatge

imread() i canals de color

Una matriu NumPy NumPy.

r1 = image[:,:,0] # get blue channel g1 = image[:,:,1] # get green channel b1 = image[:,:,2] # get red channel>