zoukankan      html  css  js  c++  java
  • 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()
    
    
    
    
    
  • 相关阅读:
    rabiitmq 消息丢失处理
    后端返回文件流和json格式的方式进行文件下载导出
    elasticsearch 安装
    docker-compose 命令
    艾孜尔江的部分影视作品
    游戏作品:万里同风
    Unity设置计时器
    Unity项目中VS Code部分功能出现问题解决思路
    Unity接收FFmpeg的UDP推流
    SVN Basics
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9412451.html
Copyright © 2011-2022 走看看