偽彩色圖像處理


2022-03-09

由於人類可以辨別上千種顏色和強度,卻只能辨別幾十種灰度,因此進行偽彩色圖像處理可以增強人眼對細節的分辨能力,幫助人們更好地觀察和分析圖像。

偽彩色圖像處理主要包括強度分層技術和灰度值到彩色變換技術,這也是我想和大家分享的內容。

先來看看我做的思維導圖:

 

 

 提醒uu們要注意:灰度圖像與彩色圖像不是一一對應的關系,它們相互有很多轉換方法。

 

1、強度分層

強度分層也稱為灰度分層或灰度分割。將灰度圖像按照灰度值范圍划分為不同的層級,然后給每個層級賦予不同的顏色,從而增強不同層級的對比度。強度分層技術將灰度圖像轉換為偽彩色圖像,且偽彩色圖像的顏色種類數目與強度分層的數目一致。

思考:若圖不一樣,划分方式是否也不一樣?

一種簡單強度分層技術的示例代碼如下:

【分段顯示,方便讀者理解以及上機debug】

from skimage import data,color
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#中文顯示函數
def set_Chinese():
    import matplotlib
    matplotlib.rcParams['font.sans-serif']=['SimHei']
    matplotlib.rcParams['axes.unicode_minus']=False
img = data.coffee()
set_Chinese()
grayimg = color.rgb2gray(img)  #將彩色圖像轉換為灰度圖像
fig = plt.figure()
ax1 = fig.add_subplot(331)
ax1.axis('off')
ax1.imshow(grayimg,cmap = 'gray')  #顯示灰度圖像
rows,cols = grayimg.shape
labels = np.zeros([rows,cols])
ax1.set_title('(a)灰度圖像')

for i in range(rows):
    for j in range(cols):
        if (grayimg[i,j] < 0.4):
            labels[i,j] = 0
        elif (grayimg[i,j] < 0.8):
            labels[i,j] = 1
        else:
            labels[i,j] = 2

psdimg = color.label2rgb(labels) #不同的灰度區間采用不同的顏色
ax2 = fig.add_subplot(311)
ax2.axis('off')
ax2.imshow(psdimg)  #顯示強度分成圖像
ax2.set_title('(b)強度分層圖像')

運行結果:

思考:

技術細節上,怎么設置labels來調顏色?

DEFAULT_COLORS=('red','blue','yellow','magenta','green','indigo','darkorange','cyan','pink',yellowgreen')


 

 

2.灰度值到彩色變換 

灰度值到彩色變換首先是對任何像素的灰度值進行3個獨立的變換,然后將3個變換結果分別作為偽彩色圖像的紅、綠、藍通道的亮度值。與強度分層技術相比,灰度值到彩色變換技術更通用。

灰度圖像按一種映射關系轉換為偽彩色圖像的代碼如下:

from skimage import data,color
from matplotlib import pyplot as plt
import numpy as np
#中文顯示函數
def set_Chinese():
    import matplotlib
    matplotlib.rcParams['font.sans-serif']=['SimHei']
    matplotlib.rcParams['axes.unicode_minus']=False
#定義灰度值到彩色變換
L = 255
def GetR(gray):
    if gray < L / 2:
        return 0
    elif gray > L / 4 * 3:
        return 1
    else:
        return 4 * gray - 2 * L
def GetG(gray):
    if gray < L / 4:
        return 4 * gray
    elif gray > L / 4 * 3:
        return 4 * L - 4 * gray
    else: 
        return L
def GetB(gray):
    if gray < L / 4:
        return L
    elif gray > L / 2:
        return 0
    else:
        return 2 * L - 4 * gray
img = data.coffee()
grayimg = color.rgb2gray(img) * 255  #將彩色圖像轉換為灰度圖像
colorimg = np.zeros(img.shape,dtype='uint8')
for ii in range(img.shape[0]):
    for jj in range(img.shape[1]):
        r,g,b = GetR(grayimg[ii,jj]), GetG(grayimg[ii,jj]), GetB(grayimg[ii,jj])
        colorimg[ii,jj,:]=(r,g,b)
#顯示結果
set_Chinese()
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.imshow(grayimg,cmap='gray')  #顯示灰度圖像
ax1.axis('off')
ax1.set_title('(a)灰度圖像')
ax2.imshow(colorimg)   #顯示偽彩色圖像
ax2.axis('off')
ax2.set_title('(b)偽彩色圖像')

 

 思考:

1、三種變換(紅色變換、綠色變換、藍色變換)函數中,區間長度不是均勻的,那么怎樣的對應關系是最好的呢?

2、可以找到一種轉換方法,使得偽彩色圖像與原圖顏色一致嗎?

  2.1 要先知道原圖的彩色分布

  2.2 探索需要用到什么編程技巧?

    tip1: 求rgb2gray函數的反函數。先發掘出rgb2gray函數做的事情,然后想辦法反轉過來。

 

 


 

路漫漫其修遠兮,吾將上下而求索


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM