天天看點

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

數字圖像處理01:imadjust函數的Pyhton實作

1、imadjust函數

Matlab的工具箱函數:imadjust(f,[low_in high_in],[low_out high_out],gamma,c)

imadjust函數是針對灰階圖像進行灰階變換的基本圖像處理函數,此函數将圖像f 灰階值映射到g中的新值,也就是将low_in與high_in之間的值映射到low_out與high_out之間。low_in以下與high_in以上的值可以被截去。也就是将low_in以下的值映射為low_out;将high_in以上的值映射為high_out。 參數gamma指明了由f映射生成圖像g時曲線的形狀。如果gamma的值小于1,映射被權重至較高(較亮)的輸出值。如果gamma的值大于1,映射被權重至較低(較暗)的輸出值。如果省略函數參數,gamma預設為1(線性映射)。

2、imadjust函數公式

r為輸入灰階值;s為輸出灰階值。

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

3、imadjust函數的映射關系圖:

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

4、python代碼實作

1. 顯示圖像标題所需的字型

from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
           

2. 需要導入的庫

import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
           

3. 定義imadjust函數

其中img為輸入圖像。

4. 讀入圖像

f = misc.imread(img).astype(np.int16)
plt.figure(1)
plt.imshow(f, cmap='gray')
plt.axis('off')
plt.title('原始圖像')
plt.show()
           

其中用scipy的misc.imread函數來讀入圖像,讀入的圖像就是一個矩陣,無需再對其矩陣化。需要注意的是,plt.imshow函數裡必須要加cmap='gray’參數,否則圖像會出現僞彩色。

原始圖像:

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

5. 函數實作部分

# imadjust函數運算部分
    for x in range(0, w):
        for y in range(0, h):
            if f[x, y] <= low_in:
                f1[x, y] = low_out
            elif f[x, y] >= high_in:
                f1[x, y] = high_out
            else:
                f1[x, y] = c * (f[x, y]**gamma)
           

經過imadjust函數變換後的圖:

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

其中參數實作為:imadjust(‘figure1.png’, 50, 100, 5, 10, 1, 1)

6. 對原始圖像和變換後圖像作內插補點

代碼: f2 = np.abs(f-f1) #內插補點的絕對值

注意要對內插補點取絕對值。

內插補點圖像:

數字圖像處理01:imadjust函數的Pyhton實作數字圖像處理01:imadjust函數的Pyhton實作

7. 整體代碼

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 11 13:54:26 2019

@author: ChengGD
"""


from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']


#from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
#import scipy
#import cv2

def imadjust(img, low_in, high_in, low_out, high_out, gamma, c):
   
    f = misc.imread(img).astype(np.int16)
    plt.figure(1)
    plt.imshow(f, cmap='gray')
    plt.axis('off')
    plt.title('原始圖像')
    plt.show()
    
    w, h = f.shape
    f1 = np.zeros([w, h])
    # imadjust函數運算部分
    for x in range(0, w):
        for y in range(0, h):
            if f[x, y] <= low_in:
                f1[x, y] = low_out
            elif f[x, y] >= high_in:
                f1[x, y] = high_out
            else:
                f1[x, y] = c * (f[x, y]**gamma)
   
    scipy.misc.imsave('figure2.png', f1)
    plt.figure(2)
    plt.imshow(f1, cmap='gray')
    plt.axis('off')
    plt.title('變換圖像')
    plt.show()
    
    
    plt.figure(3)
    f2 = np.abs(f-f1)     #內插補點的絕對值
    
    scipy.misc.imsave('figure3.png', f2)
    plt.imshow(f2, cmap='gray')
    plt.axis('off')
    plt.title('內插補點圖像')
    plt.show()
    

imadjust('figure1.png', 50, 100, 5, 10, 1, 1)