四、圖像增強系列------線性增強

線性增強基本算法

python實現線性增強基本算法

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#### 線性增強基本算法

# 繪製直方圖函數
def grayHist(img):
    h, w = img.shape[:2]
    pixelSequence = img.reshape([h * w, ])
    numberBins = 256
    histogram, bins, patch = plt.hist(pixelSequence, numberBins,
                                      facecolor='black', histtype='bar')
    plt.xlabel("gray label")
    plt.ylabel("number of pixels")
    plt.axis([0, 255, 0, np.max(histogram)])
    plt.show()


img = cv.imread("peng.png", 0)
## b可以省略
b = 0
out = -0.5 * img + b
# 進行數據截斷,大於255的值截斷爲255
out[out > 255] = 255
# 數據類型轉換
out = np.around(out)
out = out.astype(np.uint8)
# 分別繪製處理前後的直方圖
grayHist(img)
grayHist(out)
cv.imshow("img", img)
cv.imshow("out", out)
cv.waitKey()

效果圖如下

在這裏插入圖片描述
變換前:

變換後:
在這裏插入圖片描述

分段線性變換

python實現分段線性變換

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


# 繪製直方圖函數
def grayHist(img):
    h, w = img.shape[:2]
    pixelSequence = img.reshape([h * w, ])
    numberBins = 256
    histogram, bins, patch = plt.hist(pixelSequence, numberBins,
                                      facecolor='black', histtype='bar')
    plt.xlabel("gray label")
    plt.ylabel("number of pixels")
    plt.axis([0, 255, 0, np.max(histogram)])
    plt.show()

img = cv.imread("peng.png", 0)
img = cv.resize(img, None, fx=0.3, fy=0.3)
h, w = img.shape[:2]
out = np.zeros(img.shape, np.uint8)
for i in range(h):
    for j in range(w):
        pix = img[i][j]
        if pix < 50:
            out[i][j] = 0.5 * pix
        elif pix < 150:
            out[i][j] = 3.6 * pix - 310
        else:
            out[i][j] = 0.238 * pix + 194
        # 數據類型轉換
out = np.around(out)
out = out.astype(np.uint8)
grayHist(img)
grayHist(out)
cv.imshow("img", img)
cv.imshow("out", out)
cv.waitKey()

分段線性變換效果圖

在這裏插入圖片描述
變換前:

變換後:
在這裏插入圖片描述

線性增強統計量算法

待更新。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章