数字图像处理-黑白图像灰度扫描

问题描述

实现一个函数 s = scanLine4e(f, I, loc), 其中 f 是一个灰度图像,I 是一个整数,loc 是一个字符串。当 loc 为’row’时,I 代表行数。当 loc 为’column’时,I 代表列数。输出 s 是对应的相关行或者列的像素灰度矢量。调用该函数,提取 cameraman.tif 和 einstein.tif 的中心行和中心列的像素灰度矢量并将扫描得到的灰度序列绘制成图。

代码实现

导库

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

扫描函数

def  scanLine4e(f, I, loc):
    s = []
    if loc == 'row':
        for i in range(len(f[I,:])):
            s.append(f[I,i])
        return s

    elif loc == 'column':
        for i in range(len(f[:,I])):
            s.append(f[i,I])
        return s
    else:
        raise Exception("输入格式有误")

主函数

if __name__ == '__main__':
    # 导入第一张图片
    im1 = Image.open('cameraman.tif')
    im1 = np.array(im1)
    h1,w1 = im1.shape[0],im1.shape[1]
    # 调用函数提取中心行、列
    s11 = scanLine4e(im1,int(h1/2),'row')
    s12 = scanLine4e(im1,int(w1/2),'column')
    # 导入第二张图片
    im2 = Image.open('einstein.tif')
    im2 = np.array(im2)
    h2, w2 = im2.shape[0], im2.shape[1]
    s21 = scanLine4e(im2, int(h2 / 2), 'row')
    s22 = scanLine4e(im2, int(w2 / 2), 'column')
    #绘图
    plt.subplot(2, 2, 1)
    plt.plot(s11)
    plt.title('cameraman center row')
    plt.subplot(2, 2, 2)
    plt.plot(s12)
    plt.title('cameraman center column')
    plt.subplot(2, 2, 3)
    plt.plot(s21)
    plt.title('einstein center row')
    plt.subplot(2, 2, 4)
    plt.plot(s22)
    plt.title('einstein center column')
    plt.show()

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值