初级cnn研究辅助:python的matplotlib显示图片

本文介绍了如何使用Python的Matplotlib库进行图像处理和绘制,包括显示彩色与灰度图像、在图像中标记特定区域和点等内容。通过实例展示了subplot参数的意义及如何在图像上添加标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简单例子:

# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    img_gray = img.convert("L")
    fig = plt.figure()
    ax = fig.add_subplot(121)
    ax.imshow(img)
    ax = fig.add_subplot(122)
    ax.imshow(img_gray, cmap="gray")#以灰度图显示图片
    ax.set_title("hei,i'am the title")#给图片加titile
    #plt.axis("off")#不显示刻度
    plt.show()#显示刚才所画的所有操作
图片的其他处理,可以查看我的前几篇文章。

二、简单说一下

ax = fig.add_subplot(121)
里的121.第一个“1”代表图片只有一行;第一个“2”代表有两列;第二个“1”代表第一张图片在1行2列的矩阵中的位置。

如果是一个2*2的矩阵,第三个数字的排序是:

1   2

3   4

即,以行为主

当然还会出现这样的需求:

# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    img_gray = img.convert("L")
    fig = plt.figure()
    ax = fig.add_subplot(121)
    ax.imshow(img)
    ax.set_title("hei,i'am the first")

    ax = fig.add_subplot(222)
    ax.imshow(img_gray, cmap="gray")#以灰度图显示图片
    ax.set_title("hei,i'am the second")#给图片加titile

    ax = fig.add_subplot(224)
    ax.imshow(img_gray, cmap="gray")#以灰度图显示图片
    ax.set_title("hei,i'am the third")#给图片加titile
    #plt.axis("off")#不显示刻度
    plt.show()#显示刚才所画的所有操作
效果:

三、需求:在图中框出你想要的区域:

# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    img_gray = img.convert("L")
    fig = plt.figure()
    ax = fig.add_subplot(121)
    ax.imshow(img)
    ax.set_title("hei,i'am the first")
    pointx = [20, 120, 120, 20, 20]
    pointy = [20, 20, 120, 120, 20]
    ax.plot(pointx, pointy, 'r')#画一个矩形,黑色;'r'红色
效果:

四、或者你想要画点:

# -*- coding=UTF-8 -*-
import Image
from matplotlib import pyplot as plt
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    img_gray = img.convert("L")
    fig = plt.figure()
    ax = fig.add_subplot(121)
    ax.imshow(img)
    ax.set_title("hei,i'am the first")
    pointx = [20, 120, 120, 20, 20]
    pointy = [20, 20, 120, 120, 20]
    ax.plot(pointx, pointy, 'r')#画一个矩形,黑色;'r'红色
    ax.scatter(65, 70)#画点
    ax.scatter(90, 70)#画点
    plt.axis("off")#不显示刻度
效果:

(额,我怎么把我男神搞成这个样子了。。。。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值