Comment comparer les différences entre les images en python

coldplay.xixi
Libérer: 2020-08-29 11:34:30
original
4377 Les gens l'ont consulté

Comment comparer des images en python : utilisez d'abord [pylab.imread] pour lire l'image ; puis utilisez [matplotlib.pylab - plt.imshow] pour afficher l'image, puis convertissez l'image en niveaux de gris en image RVB ; ; et enfin enregistrez-le. Les images suffisent.

Comment comparer les différences entre les images en python

[Recommandations d'apprentissage associées : tutoriel Python]

Comment python compare les images :

1. Lecture des images

pylab.imread et PIL.Image.open lisent les deux ordres RBG,

et cv2.imread se lisent dans l'ordre BGR, une attention particulière doit être portée en cas d'utilisation mixte

1 matplotlib.pylab

import pylab as plt
import numpy as np
img = plt.imread('examples.png')
print(type(img), img.dtype, np.min(img), np.max(img))
[out]
(<type &#39;numpy.ndarray&#39;>, dtype(&#39;float32&#39;), 0.0, 1.0)    # matplotlib读取进来的图片是float,0-1
Copier après la connexion

2 PIL.image open

from PIL import Image
import numpy as np
img = Image.open(&#39;examples.png&#39;)
print(type(img), np.min(img), np.max(img))
img = np.array(img)     # 将PIL格式图片转为numpy格式
print(type(img), img.dtype, np.min(img), np.max(img))
[out]
(<class &#39;PIL.PngImagePlugin.PngImageFile&#39;>, 0, 255)    # 注意,PIL是有自己的数据结构的,但是可以转换成numpy数组
(<type &#39;numpy.ndarray&#39;>, dtype(&#39;uint8&#39;), 0, 255)    # 和用matplotlib读取不同,PIL和matlab相同,读进来图片和其存储在硬盘的样子是一样的,uint8,0-255
Copier après la connexion

. 3 cv2.imread

import cv2
import numpy as np
img = cv2.imread(&#39;examples.png&#39;)    # 默认是读入为彩色图,即使原图是灰度图也会复制成三个相同的通道变成彩色图
img_gray = cv2.imread(&#39;examples.png&#39;, 0)    # 第二个参数为0的时候读入为灰度图,即使原图是彩色图也会转成灰度图
print(type(img), img.dtype, np.min(img), np.max(img))
print(img.shape)
print(img_gray.shape)
[out]
(<type &#39;numpy.ndarray&#39;>, dtype(&#39;uint8&#39;), 0, 255)    # opencv读进来的是numpy数组,类型是uint8,0-255
(824, 987, 3)    # 彩色图3通道
(824, 987)    # 灰度图单通道
Copier après la connexion
import cv2
import pylab as plt
from PIL import Image
import numpy as np
img_plt = plt.imread(&#39;examples.png&#39;)
img_pil = Image.open(&#39;examples.png&#39;)
img_cv = cv2.imread(&#39;examples.png&#39;)
print(img_plt[125, 555, :])
print(np.array(img_pil)[125, 555, :] / 255.0)
print(img_cv[125, 555, :] / 255.0)
[out]
[ 0.61176473  0.3764706   0.29019609]
[ 0.61176471  0.37647059  0.29019608]
[ 0.29019608  0.37647059  0.61176471]    # opencv的是BGR顺序
Copier après la connexion

2. Afficher l'image

1, matplotlib.pylab - plt.imshow, cette fonction convertit en fait un numpy L'image RVB au format tableau est affichée

import pylab as plt
import numpy as np
img = plt.imread(&#39;examples.png&#39;)
plt.imshow(img) 
plt.show()
Copier après la connexion
import pylab as plt
from PIL import Image
import numpy as np
img = Image.open(&#39;examples.png&#39;)
img_gray = img.convert(&#39;L&#39;)    #转换成灰度图像
img = np.array(img)
img_gray = np.array(img_gray)
plt.imshow(img)    # or plt.imshow(img / 255.0),matplotlib和matlab一样,如果是float类型的图像,范围是0-1才能正常imshow,如果是uint8图像,范围则需要是0-255
plt.show()
plt.imshow(img_gray, cmap=plt.gray())    # 显示灰度图要设置cmap参数
plt.show()
plt.imshow(Image.open(&#39;examples.png&#39;))    # 实际上plt.imshow可以直接显示PIL格式图像
plt.show()
Copier après la connexion
import pylab as plt
import cv2
import numpy as np
img = cv2.imread(&#39;examples.png&#39;)
plt.imshow(img[..., -1::-1])    # 因为opencv读取进来的是bgr顺序呢的,而imshow需要的是rgb顺序,因此需要先反过来
plt.show()
Copier après la connexion

2 cv2 afficher l'image

import cv2
image2=cv2.imread(r"test/aaa/0002/0002_0_1.jpg")
cv2.imshow("1",image2)
cv2.waitKey(0)
Copier après la connexion

3. Conversion mutuelle image en niveaux de gris-image RVB

1 image PIL

from PIL import Image
img = Image.open(&#39;examples.png&#39;)
img_gray = img.convert(&#39;L&#39;)    # RGB转换成灰度图像
img_rgb = img_gray.convert(&#39;RGB&#39;) # 灰度转RGB
print(img)
print(img_gray)
print(img_rgb)
[out]
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=987x824 at 0x7FC2CCAE04D0>
<PIL.Image.Image image mode=L size=987x824 at 0x7FC2CCAE0990>
<PIL.Image.Image image mode=RGB size=987x824 at 0x7FC2CCAE0250>
Copier après la connexion

2 cv2. (notez qu'opencv peut convertir les canaux de couleur via des paramètres lors de la lecture des images. Ce qui suit est une autre façon d'y parvenir)

import cv2
import pylab as plt
img = cv2.imread(&#39;examples.png&#39;)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    # BGR转灰度
img_bgr = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)    # 灰度转BRG
img_rgb = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)    # 也可以灰度转RGB
Copier après la connexion

4. Enregistrez les images

1 PIL.image - Enregistrer les images au format PIL

from PIL import Image
img = Image.open(&#39;examples.png&#39;)
img.save(&#39;examples2.png&#39;)
img_gray = img.convert(&#39;L&#39;)
img_gray.save(&#39;examples_gray.png&#39;)    # 不管是灰度还是彩色,直接用save函数保存就可以,但注意,只有PIL格式的图片能够用save函数
Copier après la connexion

2 cv2.imwrite - Enregistrer les images au format numpy

import cv2
img = cv2.imread(&#39;examples.png&#39;)    # 这是BGR图片
cv2.imwrite(&#39;examples2.png&#39;, img)    # 这里也应该用BGR图片保存,这里要非常注意,因为用pylab或PIL读入的图片都是RGB的,如果要用opencv存图片就必须做一个转换
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(&#39;examples_gray.png&#39;, img_gray)
Copier après la connexion

Vous voulez en savoir plus Pour un apprentissage plus connexe, veuillez faire attention au colonne formation php !

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!