python如何对比图像的区别

coldplay.xixi
Freigeben: 2020-08-29 11:34:30
Original
4292 人浏览过

python对比图像的区别方法:首先使用【pylab.imread】读取图片;然后使用【matplotlib.pylab - plt.imshow】显示图片;接着灰度图与RGB图相互转换;最后保存图片即可。

python如何对比图像的区别

相关学习推荐:python教程

python对比图像的区别方法:

一、读取图片

pylab.imread和PIL.Image.open读入的都是RBG顺序,

而cv2.imread读入的是BGR顺序,混合使用的时候要特备注意

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]
(, dtype('float32'), 0.0, 1.0)    # matplotlib读取进来的图片是float,0-1
Nach dem Login kopieren

2 PIL.image.open

from PIL import Image
import numpy as np
img = Image.open('examples.png')
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]
(, 0, 255)    # 注意,PIL是有自己的数据结构的,但是可以转换成numpy数组
(, dtype('uint8'), 0, 255)    # 和用matplotlib读取不同,PIL和matlab相同,读进来图片和其存储在硬盘的样子是一样的,uint8,0-255
Nach dem Login kopieren

3 cv2.imread

import cv2
import numpy as np
img = cv2.imread('examples.png')    # 默认是读入为彩色图,即使原图是灰度图也会复制成三个相同的通道变成彩色图
img_gray = cv2.imread('examples.png', 0)    # 第二个参数为0的时候读入为灰度图,即使原图是彩色图也会转成灰度图
print(type(img), img.dtype, np.min(img), np.max(img))
print(img.shape)
print(img_gray.shape)
[out]
(, dtype('uint8'), 0, 255)    # opencv读进来的是numpy数组,类型是uint8,0-255
(824, 987, 3)    # 彩色图3通道
(824, 987)    # 灰度图单通道
Nach dem Login kopieren
import cv2
import pylab as plt
from PIL import Image
import numpy as np
img_plt = plt.imread('examples.png')
img_pil = Image.open('examples.png')
img_cv = cv2.imread('examples.png')
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顺序
Nach dem Login kopieren

二、显示图片

1、matplotlib.pylab - plt.imshow,这个函数的实际上就是将一个numpy数组格式的RGB图像显示出来

import pylab as plt
import numpy as np
img = plt.imread('examples.png')
plt.imshow(img) 
plt.show()
Nach dem Login kopieren
import pylab as plt
from PIL import Image
import numpy as np
img = Image.open('examples.png')
img_gray = img.convert('L')    #转换成灰度图像
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('examples.png'))    # 实际上plt.imshow可以直接显示PIL格式图像
plt.show()
Nach dem Login kopieren
import pylab as plt
import cv2
import numpy as np
img = cv2.imread('examples.png')
plt.imshow(img[..., -1::-1])    # 因为opencv读取进来的是bgr顺序呢的,而imshow需要的是rgb顺序,因此需要先反过来
plt.show()
Nach dem Login kopieren

2 cv2显示图片

import cv2
image2=cv2.imread(r"test/aaa/0002/0002_0_1.jpg")
cv2.imshow("1",image2)
cv2.waitKey(0)
Nach dem Login kopieren

三、灰度图-RGB图相互转换

1 PIL.Image

from PIL import Image
img = Image.open('examples.png')
img_gray = img.convert('L')    # RGB转换成灰度图像
img_rgb = img_gray.convert('RGB') # 灰度转RGB
print(img)
print(img_gray)
print(img_rgb)
[out]


Nach dem Login kopieren

2 cv2(注意,opencv在读入图片的时候就可以通过参数实现颜色通道的转换,下面是用别的方式实现)

import cv2
import pylab as plt
img = cv2.imread('examples.png')
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
Nach dem Login kopieren

四、保存图片

1 PIL.image - 保存PIL格式的图片

from PIL import Image
img = Image.open('examples.png')
img.save('examples2.png')
img_gray = img.convert('L')
img_gray.save('examples_gray.png')    # 不管是灰度还是彩色,直接用save函数保存就可以,但注意,只有PIL格式的图片能够用save函数
Nach dem Login kopieren

2 cv2.imwrite - 保存numpy格式的图片

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

想了解更多相关学习,敬请关注php培训栏目!

以上是python如何对比图像的区别的详细内容。更多信息请关注PHP中文网其他相关文章!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!