第6章主要讲的是彩色图像处理,一些彩色模型如RGB,CMK,CMYK,HSI等色彩模型;彩色模型的变换关系;还包含由灰度图像怎样处理成假彩色图像;使用彩色分割图像等。本章比较少理论还有变换的描述,主要以代码为主,如有需要,请自行查看书本。
这里写目录标题
- 假彩色图像处理
- 灰度分层(灰度分割)和彩色编码
- 灰度值到彩色变换
- Gray -> RGB
假彩色图像处理
灰度分层(灰度分割)和彩色编码
def gray_slice(img_gray):img_ori = img_gray / 255.rows,cols = img_ori.shape[:2]labels = np.zeros([rows,cols])for i in range(rows):for j in range(cols):if(img_ori[i,j] < 0.125):labels[i,j] = 0elif(img_ori[i,j] < 0.25):labels[i,j] = 0.2elif(img_ori[i,j] < 0.375):labels[i,j] = 0.4elif(img_ori[i,j] < 0.5):labels[i,j] = 0.5elif(img_ori[i,j] < 0.625):labels[i,j] = 0.6elif(img_ori[i,j] < 0.75):labels[i,j] = 0.8elif(img_ori[i,j] < 0.875):labels[i,j] = 0.9else:labels[i,j] = 1return labels
# Gray to RGB
from skimage import io, exposure, colorimg_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0620(a)(picker_phantom).tif', 0)labels = gray_slice(img_ori)
labels = np.uint8(labels * 255)
img_rgb = color.label2rgb(labels)plt.figure(figsize=(20, 5))
plt.subplot(141), plt.imshow(img_ori, 'gray'), plt.title('Original')plt.subplot(142), plt.imshow(img_rgb, ), plt.title('Pseudo RGB')
# plt.subplot(143), plt.imshow(img_cmyk, ), plt.title('CMYK')
# plt.subplot(144), plt.imshow(img_r, ), plt.title('Red Channel')plt.tight_layout()
plt.show()
# Gray to RGB
from skimage import io, exposure, color
def gray_slice(img_gray):rows,cols = img_gray.shape[:2]labels = np.zeros([rows,cols], np.uint8)for i in range(rows):for j in range(cols):if(img_gray[i,j] < 250):labels[i,j] = 125else:labels[i,j] = 100return labelsimg_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0621(a)(weld-original).tif', 0)labels = gray_slice(img_ori)
img_rgb = color.label2rgb(labels)plt.figure(figsize=(20, 5))
plt.subplot(141), plt.imshow(img_ori, 'gray'), plt.title('Original')plt.subplot(142), plt.imshow(img_rgb, ), plt.title('Pseudo RGB')
# plt.subplot(143), plt.imshow(img_cmyk, ), plt.title('CMYK')
# plt.subplot(144), plt.imshow(img_r, ), plt.title('Red Channel')plt.tight_layout()
plt.show()
# Gray to RGB
from skimage import io, exposure, color
def gray_slice(img_gray):rows,cols = img_gray.shape[:2]labels = np.zeros([rows,cols], np.uint8)for i in range(rows):for j in range(cols):if(img_gray[i,j] < 31):labels[i,j] = 0elif(img_gray[i,j] < 63):labels[i, j] = 10elif(img_gray[i,j] < 95):labels[i, j] = 20elif(img_gray[i,j] < 127):labels[i, j] = 30elif(img_gray[i,j] < 159):labels[i, j] = 40elif(img_gray[i,j] < 191):labels[i, j] = 255elif(img_gray[i,j] < 223):labels[i, j] = 255else:labels[i,j] = 255return labelsimg_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0622(a)(tropical_rain_grayscale.tif', 0)labels = gray_slice(img_ori)
img_rgb = color.label2rgb(labels)hist, bins, patches = plt.hist(img_ori.flatten(), bins=256)
plt.figure(figsize=(15, 10))
plt.subplot(211), plt.imshow(img_ori, 'gray'), plt.title('Original')
plt.subplot(212), plt.imshow(img_rgb, ), plt.title('Pseudo RGB')
plt.tight_layout()
plt.show()
灰度值到彩色变换
# Gray to RGB
from skimage import io, exposure, colorimg_r = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0627(a)(WashingtonDC Band3-RED).TIF', 0)
img_g = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0627(b)(WashingtonDC Band2-GREEN).TIF', 0)
img_b = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0627(c)(1)(WashingtonDC Band1-BLUE).TIF', 0)
img_ir = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0627(d)(WashingtonDC Band4).TIF', 0)# IR G B
img_irgb = np.dstack([img_ir, img_g, img_b])# R IR B
img_RIRB = np.dstack([img_r, img_ir, img_b])plt.figure(figsize=(15, 10))
plt.subplot(231), plt.imshow(img_r, 'gray'), plt.title('Red Band')
plt.subplot(232), plt.imshow(img_g, 'gray'), plt.title('Green Band')
plt.subplot(233), plt.imshow(img_b, 'gray'), plt.title('Blue Band')
plt.subplot(234), plt.imshow(img_ir, 'gray'), plt.title('IR Band')
plt.subplot(235), plt.imshow(img_irgb), plt.title('IR G B ')
plt.subplot(236), plt.imshow(img_RIRB), plt.title('R IR B')
plt.tight_layout()
plt.show()# RGB
img_rgb = np.dstack([img_r, img_g, img_b])
plt.figure(figsize=(5, 5))
plt.imshow(img_rgb), plt.title('RGB')
plt.tight_layout()
plt.show()
# import numpy as np
# from skimage import io,exposure,color
# import matplotlib.pyplot as plt
# import math
# import sys# 灰度值到彩色变换
# 定义灰度值到彩色变换
L = 255
def GetR(gray):if gray < L/2:return 0elif gray > L/4*3:return Lelse:return 4*gray-2*L
def GetG(gray):if gray < L/4:return 4*grayelif gray > L/4*3:return 4*L-4*grayelse:return L
def GetB(gray):if gray < L/4:return Lelif gray > L/2:return 0else:return 2*L-4*graydef gray2rgb(img_gray):height, width = img_gray.shape[:2]dst = np.zeros((height, width, 3), dtype = 'uint8')for h in range(height):for w in range(width):r,g,b = GetR(img_gray[h,w]),GetG(img_gray[h,w]),GetB(img_gray[h,w])dst[h, w, :] = (r,g,b)return dst
# Gray to RGB
from skimage import io, exposure, colorimg_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0620(a)(picker_phantom).tif', 0)img_rgb = gray2rgb(img_ori)plt.figure(figsize=(20, 5))
plt.subplot(141), plt.imshow(img_ori, 'gray'), plt.title('Original')plt.subplot(142), plt.imshow(img_rgb, ), plt.title('Pseudo RGB')
# plt.subplot(143), plt.imshow(img_cmyk, ), plt.title('CMYK')
# plt.subplot(144), plt.imshow(img_r, ), plt.title('Red Channel')plt.tight_layout()
plt.show()
# Gray to RGB
from skimage import io, exposure, colorimg_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0622(a)(tropical_rain_grayscale.tif', 0)img_rgb = gray2rgb(img_ori)plt.figure(figsize=(20, 10))
plt.subplot(141), plt.imshow(img_ori, 'gray'), plt.title('Original')plt.subplot(142), plt.imshow(img_rgb, ), plt.title('Pseudo RGB')
# plt.subplot(143), plt.imshow(img_cmyk, ), plt.title('CMYK')
# plt.subplot(144), plt.imshow(img_r, ), plt.title('Red Channel')plt.tight_layout()
plt.show()
Gray -> RGB
严格来说这不是由于Gray转RGB,因为利用原来的GB通道
我们要将RGB表示转换为gGB表示,也就是用灰度分量g取代蓝色分量R,蓝色分量B和绿色分量G不变。我们可以从gGB计算出红色分量R,因为灰度g=pR+qG+tB(其中p=0.2989,q=0.5870,t=0.1140),于是R=(g-qG-t*B)/p。于是我们只要保留B和G两个颜色分量,再加上灰度图g,就可以回复原来的RGB图像。同样,我们这里的g是可以随便取代红绿蓝三种分量中的任一分量的。下面进行演示。
# Gray to RGB
img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0646(a)(lenna_original_RGB).tif')src = img_ori.copy()
# src_gray = bgr2gray(img_ori)
src_gray = cv2.cvtColor(img_ori, cv2.COLOR_BGR2GRAY)B = src[:,:,0]
G = src[:,:,1]
R = src[:,:,2]# 灰度g=p*R+q*G+t*B(其中p=0.2989,q=0.5870,t=0.1140),于是B=(g-p*R-q*G)/t。于是我们只要保留R和G两个颜色分量,再加上灰度图g,就可以回复原来的RGB图像。
g = src_gray[:]
p = 0.2989; q = 0.5870; t = 0.1140
B_new = (g - p * R - q * G) /t
B_new = np.uint8(normalize(B_new) * 255) # 这种方式会有点偏蓝
# B_new = np.uint8(B_new / 255) # 这种方式会偏绿
src_new = np.zeros((src.shape)).astype("uint8")
src_new[:,:,0] = B_new
src_new[:,:,1] = G
src_new[:,:,2] = Rplt.figure(figsize=(20, 5))
plt.subplot(141), plt.imshow(img_ori[:, :, ::-1]), plt.title('Original')
plt.subplot(142), plt.imshow(src_gray, ), plt.title('GrayScale')
plt.subplot(143), plt.imshow(src_new[..., ::-1], ), plt.title('Gray To RGB')
# plt.subplot(144), plt.imshow(img_r, ), plt.title('Red Channel')plt.tight_layout()
plt.show()