• Python: 图像处理的基本运算


    Python 作为一种面向对象、直译式的计算机程序语言,在很多领域得到广泛应用。

    本文主要介绍 Python 在图像处理中的基本运算,借助 scikit-image 库,Python 在做图像处理的
    时候非常方便,对于习惯用MATLAB的人来说,可以非常快速的习惯Python的语法。 熟悉了这些
    基本的运算,就可以做进一步复杂的图像处理了。

    import numpy as np
    from skimage import data
    import matplotlib.pyplot as plt
    from skimage import io
    import time
    from skimage import img_as_float, img_as_ubyte
    
    # 读取图像
    file_name='E:/Visual Effects/PS Algorithm/2.jpg';
    img=io.imread(file_name)
    
    file_name2='E:/Visual Effects/PS Algorithm/3.jpg'
    img2=io.imread(file_name2)
    
    # 图像的融合,
    img3=img2*0.5+img*0.5;
    
    # 浮点值的图像归一化到 0-1 之间
    img3=img3/255.0;
    
    # 图像数据类型的转换
    img3=img_as_float(img3);
    img3=img_as_ubyte(img3);
    
    # 分离图像的 R, G, B 三个通道
    r_img=img2[:,:,0];
    g_img=img2[:,:,1];
    b_img=img2[:,:,2];
    
    # 保存图像
    io.imsave('red.jpg', r_img);
    io.imsave ('out.jpg', img3);
    
    # 访问图像中的像素
    # 单通道图像
    aa=r_img[0:3, 0:4];
    # 多通道图像
    bb=img[0:3, 0:4, :];
    
    print aa
    print bb[1]
    
    # 图像的显示
    # 显示彩色图像
    plt.figure(1)
    plt.imshow(img)
    
    # 显示灰度图像
    plt.figure(2)
    plt.imshow(r_img, plt.cm.gray)
    plt.figure(3)
    plt.imshow(g_img, plt.cm.gray)
    plt.figure(4)
    plt.imshow(b_img, plt.cm.gray)
    
    plt.axis('off');
    plt.show();
    
    # 获取图像的维度,行数,列数以及通道数
    row, col, channel=img.shape;
    print "red channel", r_img.dtype, r_img.shape
    print "green channel", g_img.dtype, g_img.shape
    print "blue channel", g_img.dtype, b_img.shape
    
    print r_img.shape, row, col, channel
    
    # 求图像的最大值,最小值,均值
    print img3.min(), img3.max(), img3.mean()
    
    
    
    
    
  • 相关阅读:
    Java实现找出数组中重复次数最多的元素以及个数
    java经典小算法
    java将数组中的零放到末尾
    BP神经网络
    Centos配置Caffe详解
    JAVA面试题之实现字符串的倒序输出
    Android 发送短信与接收短信
    java 选择排序法
    java数组获取最值
    spring kafka consumer原理解析二
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9412451.html
Copyright © 2020-2023  润新知