圖像處理的幾種方法

  • 圖像處理的幾種方法 1.使用skimage
    name details name details name details
    astronaut 宇航員圖片 coffee 一杯咖啡圖片 lena lena圖片
    camera 拿相機的人圖片 coins 硬幣 moon 月亮
    checkerboard 棋盤 horse page 一頁書
    chelsea 小貓圖片 hubble_deep_field 星空 text 文字
    clock 時鐘 immunohistochemistry 結腸    
    import numpy as np
    import scipy
    import matplotlib.pyplot as plt
    from skimage import io,data
    img=data.chelsea()
    img=io.imread('d:/python/loli.jpg')
    #讀取圖片
    # img=io.imread('d:/python/loli.jpg',as_grey=True)
    #圖片變換
    [m,n,l]=img.shape
    img1=skimage.transform.resize(img,(int(m*0.6),int(n*0.6),l),mode='reflect')
    #保存圖片
    io.imsave('d:/python/loli1.jpg',img1)
    io.imshow(img1)
    plt.show()
    # 顯示圖片信息
    print (type(img))  #顯示類型
    print (img.shape)  #顯示尺寸
    print (img.shape[0])  #圖片寬度
    print (img.shape[1])  #圖片高度
    print (img.shape[2])  #圖片通道數
    print (img.size)   #顯示總像素個數
    print (img.max())  #最大像素值
    print (img.min())  #最小像素值
    print (img.mean()) #像素平均值
    C:\ProgramData\Anaconda2\lib\site-packages\skimage\util\dtype.py:122: UserWarning: Possible precision loss when converting from float64 to uint8
      .format(dtypeobj_in, dtypeobj_out))

    <type 'numpy.ndarray'>
    (415L, 518L, 3L)
    415
    518
    3
    644910
    255
    0
    111.793140128
    2 使用scipy.misc(miscellaneous routines)不推薦,不少已經deprecated

    Various utilities that don’t have another home.
    Note that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.html

    Deprecated functions:python

    col 1 col 2
    bytescale(*args, **kwds) bytescale is deprecated!
    fromimage(*args, **kwds) fromimage is deprecated!
    imfilter(*args, **kwds) imfilter is deprecated!
    imread(*args, **kwds) imread is deprecated!
    imresize(*args, **kwds) imresize is deprecated!
    imrotate(*args, **kwds) imrotate is deprecated!
    imsave(*args, **kwds) imsave is deprecated!
    imshow(*args, **kwds) imshow is deprecated!
    toimage(*args, **kwds) toimage is deprecated!
    from scipy import misc
    import matplotlib.pyplot as plt
    import numpy as np
    # 讀取圖片
    img=misc.imread('d:/python/loli.jpg',mode='RGB')
    # 改變大小
    img1=scipy.misc.imresize(img,(111,80),interp='nearest')
    # 顯示圖片
    #misc.imshow(img) have been deprecated
    plt.imshow(img)
    plt.show()
    misc.imsave('d:/python/loli1.jpg',img)

    3 使用PIL作圖像處理

    3.1 Reading and Writing Images : open( infilename ) , save( outfilename )ide

    3.2 Cutting and Pasting and Merging Images :ui

    crop() : 從圖像中提取出某個矩形大小的圖像。它接收一個四元素的元組做爲參數,各元素爲(left, upper, right, lower),座標系統的原點(0, 0)是左上角。
    paste() :
    merge()3d

    3.3 幾何變換rest

    box = (100, 100, 200, 200)
     region = im.crop(box)
     region.show()
     region = region.transpose(Image.ROTATE_180)
     region.show()
     im.paste(region, box)
     im.show()
    #旋轉一幅圖片
    def roll(image, delta):
        "Roll an image sideways"
    
        xsize, ysize = image.size
    
        delta = delta % xsize
        if delta == 0: return image
    
        part1 = image.crop((0, 0, delta, ysize))
        part2 = image.crop((delta, 0, xsize, ysize))
        image.paste(part2, (0, 0, xsize-delta, ysize))
        image.paste(part1, (xsize-delta, 0, xsize, ysize))
    
        return image
    #簡單的幾何變換
    out = im.resize((128, 128))                     #
    out = im.rotate(45)                             #逆時針旋轉 45 度角。
    out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右對換。
    out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下對換。
    out = im.transpose(Image.ROTATE_90)             #旋轉 90 度角。
    out = im.transpose(Image.ROTATE_180)            #旋轉 180 度角。
    out = im.transpose(Image.ROTATE_270)            #旋轉 270 度角。
    from PIL import Image
    im=Image.open('d:/python/loli.jpg')
    print im.format,im.size,im.mode
    im.show()
    JPEG (518, 415) RGB
    box = (100, 100, 200, 200)
    region = im.crop(box)
    region.show()
    region = region.transpose(Image.ROTATE_180)
    region.show()
    im.paste(region, box)
    im.show()
    out = im.resize((128, 128))                     #
    out = im.rotate(45)                             #逆時針旋轉 45 度角。
    out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右對換。
    out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下對換。
    out = im.transpose(Image.ROTATE_90)             #旋轉 90 度角。
    out = im.transpose(Image.ROTATE_180)            #旋轉 180 度角。
    out = im.transpose(Image.ROTATE_270) 
    out.show()
  • 以上是圖像處理的幾種方法的內容,更多 圖像處理 python 的內容,請您使用右上方搜索功能獲取相關信息。
相關文章
相關標籤/搜索