第6章 Python 数字图像处理(DIP) - 彩色图像处理3 -色彩变换、彩色校正、彩色图像平滑和锐化、HSI彩色空间中的分割、RGB空间中的分割、彩色边缘检测

数字图像处理(DIP)--Python 同时被 2 个专栏收录
57 篇文章 10 订阅

色彩变换

# 图像颜色分量的显示
from PIL import Image

img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0630(01)(strawberries_fullcolor).tif')
img_cmyk = img_ori.convert("CMYK")

img_temp = np.array(img_cmyk)
img_c = img_temp[:, :, 0]
img_m = img_temp[:, :, 1]
img_y = img_temp[:, :, 2]
img_k = img_temp[:, :, 3]

plt.figure(figsize=(20, 25))
plt.subplot(541), plt.imshow(img_cmyk), plt.title('Original CMYK')

# CMYK, seems is CMY, as K is all black
plt.subplot(545), plt.imshow(img_c, 'gray'), plt.title('Cyan')
plt.subplot(546), plt.imshow(img_m, 'gray'), plt.title('Magenta')
plt.subplot(547), plt.imshow(img_y, 'gray'), plt.title('Yellow')
plt.subplot(5, 4, 8), plt.imshow(img_k, 'gray'), plt.title('Black')

# Show RGB channels
img_rgb = np.array(img_ori)
plt.subplot(5, 4, 9), plt.imshow(img_rgb[:, :, 0], 'gray'), plt.title('Red')
plt.subplot(5, 4, 10), plt.imshow(img_rgb[:, :, 1], 'gray'), plt.title('Green')
plt.subplot(5, 4, 11), plt.imshow(img_rgb[:, :, 2], 'gray'), plt.title('Blue')

# Show HSI channels
img_hsi = img_ori.convert("HSV")
img_hsi = np.array(img_hsi)
plt.subplot(5, 4, 13), plt.imshow(img_hsi[:, :, 0], 'gray'), plt.title('Hue')
plt.subplot(5, 4, 14), plt.imshow(img_hsi[:, :, 1], 'gray'), plt.title('Saturation')
plt.subplot(5, 4, 15), plt.imshow(img_hsi[:, :, 2], 'gray'), plt.title('Intensity')

plt.tight_layout()
plt.show()

在这里插入图片描述

from PIL import Image

img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0630(01)(strawberries_fullcolor).tif')
img_cmyk = img_ori.convert("CMYK")

img_temp = np.array(img_cmyk)
img_c = img_temp[:, :, 0]
img_m = img_temp[:, :, 1]
img_y = img_temp[:, :, 2]
img_k = img_temp[:, :, 3]

plt.figure(figsize=(20, 25))
plt.subplot(541), plt.imshow(img_cmyk), plt.title('Original CMYK')

# CMYK, seems is CMY, as K is all black
plt.subplot(545), plt.imshow(img_c, 'gray'), plt.title('Cyan')
plt.subplot(546), plt.imshow(img_m, 'gray'), plt.title('Magenta')
plt.subplot(547), plt.imshow(img_y, 'gray'), plt.title('Yellow')
plt.subplot(548), plt.imshow(img_k, 'gray'), plt.title('Black')

# change K value
img_k_new = img_k * 1 + 150
img_cmyk_new = np.dstack((img_c, img_m, img_y, img_k_new))

plt.subplot(549), plt.imshow(img_cmyk_new, 'gray'), plt.title('New CMYK')

plt.tight_layout()
plt.show()

在这里插入图片描述

def gamma_img(img, c, gamma):
    
    img = np.array(img).astype(float)
    output_img = c * img ** gamma
    img_scale = np.uint8((output_img / output_img.max()) * 255)
    
    return img_scale
def sigmoid_plot(img, scale):
    x = np.linspace(img.min(), img.max(), 500)
    x1 = x - 125
    y = 1 / (1 + np.exp(-x1 / scale))
    return x, y
    plt.plot(x, y)
    plt.grid()
def sigmoid_transform(img, scale):
    
    img = np.array(img).astype(float)
    img_temp = (img - 125.)
    
    img_new = 1 / (1 + np.exp(-img_temp / scale))
    
    img_new = np.uint8(normalize(img_new) * 255)
    
    return img_new
# 色调和彩色校正
from PIL import Image

img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(top_ left_flower).tif')

plt.figure(figsize=(15, 5))
plt.subplot(131), plt.imshow(img_ori), plt.title('Original')

img_colour = sigmoid_transform(img_ori, 30)

plt.subplot(132), plt.imshow(img_colour), plt.title('Colour Correct')

# x, y = sigmoid_plot(np.array(img_ori), 40)
# plt.subplot(133), sigmoid_plot(np.array(img_ori), 40), plt.title("Transform")

x, y = sigmoid_plot(np.array(img_ori), 40)
plt.axes([0.68, 0.15, 0.15, 0.3]), plt.plot(x, y), plt.title("Transform"), plt.grid()

# plt.tight_layout()
plt.show()

在这里插入图片描述

# 色调和彩色校正
from PIL import Image

img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(middle_row_left_chalk ).tif')

plt.figure(figsize=(10, 5))
plt.subplot(121), plt.imshow(img_ori), plt.title('Original')

img_colour = gamma_img(img_ori, 1, 1.5)

plt.subplot(1, 2, 2), plt.imshow(img_colour), plt.title('Colour Correct')

plt.tight_layout()
plt.show()

在这里插入图片描述

# 色调和彩色校正
from PIL import Image

img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(bottom_left_stream).tif')

plt.figure(figsize=(10, 5))
plt.subplot(121), plt.imshow(img_ori), plt.title('Original')

img_colour = gamma_img(img_ori, 1, 0.5)

plt.subplot(1, 2, 2), plt.imshow(img_colour), plt.title('Colour Correct')

plt.tight_layout()
plt.show()

在这里插入图片描述

彩色图像平滑和锐化

import numpy as np

def arithmentic_mean(image, kernel):
    """
    :param image: input image
    :param kernel: input kernel
    :return: image after convolution
    """
    
    img_h = image.shape[0]
    img_w = image.shape[1]

    m = kernel.shape[0]
    n = kernel.shape[1]

    # padding
    padding_h = int((m -1)/2)
    padding_w = int((n -1)/2)

    image_pad = np.pad(image.copy(), (padding_h, padding_w), mode="constant", constant_values=0)

    image_convol = image.copy()
    for i in range(padding_h, img_h + padding_h):
        for j in range(padding_w, img_w + padding_w):
            temp = np.sum(image_pad[i-padding_h:i+padding_h+1, j-padding_w:j+padding_w+1] * kernel)
            image_convol[i - padding_h][j - padding_w] = 1/(m * n) * temp
    image_convol = np.uint8(normalize(image_convol) * 255)
    return image_convol
# 图像颜色分量的显示
from PIL import Image

# img_ori = Image.open('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif')
img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif')
img_ori = img_ori[:, :, ::-1]

# Show RGB channels
plt.figure(figsize=(10, 10))
img_rgb = np.array(img_ori)
plt.subplot(2, 2, 1), plt.imshow(img_rgb), plt.title('RGB')
plt.subplot(2, 2, 2), plt.imshow(img_rgb[:, :, 0], 'gray'), plt.title('Red')
plt.subplot(2, 2, 3), plt.imshow(img_rgb[:, :, 1], 'gray'), plt.title('Green')
plt.subplot(2, 2, 4), plt.imshow(img_rgb[:, :, 2], 'gray'), plt.title('Blue')

plt.tight_layout()
plt.show()

# Show HSI channels
plt.figure(figsize=(15, 5))
img_hsi = cv2.cvtColor(np.array(img_ori), cv2.COLOR_RGB2HSV)
img_hsi = np.array(img_hsi)
plt.subplot(1, 3, 1), plt.imshow(img_hsi[:, :, 0], 'gray'), plt.title('Hue')
plt.subplot(1, 3, 2), plt.imshow(img_hsi[:, :, 1], 'gray'), plt.title('Saturation')
plt.subplot(1, 3, 3), plt.imshow(img_hsi[:, :, 2], 'gray'), plt.title('Intensity')

plt.tight_layout()
plt.show()

在这里插入图片描述在这里插入图片描述

# 图像平滑
mean_kernal = np.ones([5, 5])
mean_kernal = mean_kernal / (mean_kernal.size)

img_rgb_new = np.zeros(img_rgb.shape, np.uint8)

for i in range(3):
    img_temp = img_rgb[:, :, i]
    img_dst = arithmentic_mean(img_temp, kernel=mean_kernal)
    img_rgb_new[:, :, i] = img_dst

img_hsi_new = np.zeros(img_rgb.shape, np.uint8)

for i in range(3):
    if i == 2:
        img_temp = img_hsi[:, :, i]
        img_dst = arithmentic_mean(img_temp, kernel=mean_kernal)
        img_hsi_new[:, :, i] = img_dst
    else:
        img_hsi_new[:, :, i] = img_hsi[:, :, i]
    
img_hsi_rgb = cv2.cvtColor(img_hsi_new, cv2.COLOR_HSV2RGB)

img_diff = img_rgb_new - img_hsi_rgb

plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1), plt.imshow(img_rgb_new), plt.title('RGB')
plt.subplot(1, 3, 2), plt.imshow(img_hsi_rgb), plt.title('HSI RGB')
plt.subplot(1, 3, 3), plt.imshow(img_diff), plt.title('Differenc')

plt.tight_layout()
plt.show()

在这里插入图片描述

def laplacian_img(img_gray):
    # 拉普拉期算子,用于边缘检对于检测图像中的模糊也非常有用
    kernel_laplacian = np.array((
                        [0,1,0],
                        [1,-4,1],
                        [0,1,0]), np.int8)

    imgkernel_laplacian = cv2.filter2D(img_gray, -1, kernel_laplacian)
    
    laplacian_img = np.uint8(normalize(img_gray + imgkernel_laplacian) * 255)
    
    return laplacian_img
# 图像锐化
img_rgb_new = np.zeros(img_rgb.shape, np.uint8)

for i in range(3):
    img_temp = img_rgb[:, :, i]
    img_dst = laplacian_img(img_temp)
    img_rgb_new[:, :, i] = img_dst

img_hsi_new = np.zeros(img_rgb.shape, np.uint8)

for i in range(3):
    if i == 2:
        img_temp = img_hsi[:, :, i]
        img_dst = laplacian_img(img_temp)
        img_hsi_new[:, :, i] = img_dst
    else:
        img_hsi_new[:, :, i] = img_hsi[:, :, i]
    
img_hsi_rgb = cv2.cvtColor(img_hsi_new, cv2.COLOR_HSV2RGB)

img_diff = img_rgb_new - img_hsi_rgb

plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1), plt.imshow(img_rgb_new), plt.title('RGB')
plt.subplot(1, 3, 2), plt.imshow(img_hsi_rgb), plt.title('HSI RGB')
plt.subplot(1, 3, 3), plt.imshow(img_diff), plt.title('Differenc')

plt.tight_layout()
plt.show()

在这里插入图片描述

使用彩色分割图像

HSI 彩色空间中的分割

# HSI彩色图像分割

img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0642(a)(jupiter_moon_original).tif')
img_ori = img_ori[:, :, ::-1] # BGR 2 RGB

# Show HSI channels
plt.figure(figsize=(14, 20))
img_hsi = cv2.cvtColor(np.array(img_ori), cv2.COLOR_RGB2HSV)

plt.subplot(4, 2, 1), plt.imshow(img_ori), plt.title('Ori')
plt.subplot(4, 2, 2), plt.imshow(img_hsi[:, :, 0], 'gray'), plt.title('Hue')
plt.subplot(4, 2, 3), plt.imshow(img_hsi[:, :, 1], 'gray'), plt.title('Saturation')
plt.subplot(4, 2, 4), plt.imshow(img_hsi[:, :, 2], 'gray'), plt.title('Intensity')

# Threshold
img_s = normalize(img_hsi[:, :, 1])
thresh = 0.255 #0.255 #img_s.max() * 0.1 + 0.233
print(thresh)

img_thresh = img_s.copy()
img_thresh = np.where(img_thresh <= thresh, img_thresh, 1)
img_thresh = np.where(img_thresh > thresh, img_thresh, 0)
plt.subplot(4, 2, 5), plt.imshow(img_thresh, 'gray'), plt.title('Binary Thred of Saturation')

# Threshold X Hue
img_thred_hue = img_hsi[:, :, 0] * img_thresh
plt.subplot(4, 2, 6), plt.imshow(img_thred_hue, 'gray'), plt.title('Hue X Binary Thred')

# Histogram
plt.subplot(4, 2, 7), plt.hist(img_thred_hue.flatten(), bins=256), plt.title('Hue X Binary Thred')

# Binary
img_binary = img_thred_hue.copy()
img_binary = np.where(img_binary <= 125, img_binary, 255) # >125 为1
img_binary = np.where(img_binary > 125, img_binary, 0)  # < 125为0
plt.subplot(4, 2, 8), plt.imshow(img_binary, 'gray'), plt.title('Binary')

plt.tight_layout()
plt.show()
0.255

在这里插入图片描述

RGB空间中的分割

  1. 欧氏距离
  2. 协方差矩阵
  3. 边界盒
def rgb_segment(img_rgb, img_roi, d0):
    """
    RGB spatial domain sementation base of ROI
    param: img_rgb: input image, RGB channel
    param: img_roi: region of interesting of the image where you want to be seperated
    param: d0: the Euculidean distance of the ROI region against others
    return: img_dst, a mask image range [0, 1]    
    """
    mean = np.mean(img_roi, axis=(0, 1))
    sigma = np.std(img_roi, axis=(0, 1))
    img_dst = np.zeros(img_rgb.shape[:2])
    height, width = img_dst.shape
    
    for h in range(height):
        for w in range(width):
            temp = img_rgb[h, w]
            if np.linalg.norm(temp - mean) <= d0:
                img_dst[h, w] = 1
            else:
                img_dst[h, w] = 0
                
    return img_dst
# RGB彩色图像分割

img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0642(a)(jupiter_moon_original).tif')
# img_ori = img_ori[:, :, ::-1] # BGR 2 RGB
img_rgb = cv2.cvtColor(img_ori, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(14, 20))
plt.subplot(4, 2, 1), plt.imshow(img_rgb), plt.title('Ori')

# draw rectangle
# img_rect = cv2.rectangle(img_rgb, (60, 240), (98, 315), (255, 255, 255), 2)
# plt.subplot(4, 2, 2), plt.imshow(img_rect), plt.title('ROI')

# show ROI
roi = img_rgb[240:315, 60:98, :]
mean = np.mean(roi, axis=(0, 1))
sigma = np.std(roi, axis=(0, 1))
print(f"RGB mean -> {mean}")
print(f"RGB sigma  -> {sigma}")
plt.subplot(4, 2, 3), plt.imshow(roi), plt.title('ROI')

img_dst = rgb_segment(img_rgb, roi, d0=38)
            
plt.subplot(4, 2, 4), plt.imshow(img_dst, 'gray'), plt.title('Segment')

plt.tight_layout()
plt.show()   
RGB mean -> [146.81298246  40.47473684  42.62385965]
RGB sigma  -> [23.60878011 25.67369246 17.97835714]

在这里插入图片描述

彩色边缘检测

img1_r = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(a)(RGB1-red).tif', -1)
img1_g = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(b)(RGB1-green).tif', -1)
img1_b = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(c)(RGB1-blue).tif', -1)

img1_rgb = np.dstack((img1_r, img1_g, img1_b))

img2_r = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(e)(RGB2_red).tif', -1)
img2_g = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(f)(RGB2_green).tif', -1)
img2_b = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(g)(RGB2_blue).tif', -1)

img2_rgb = np.dstack((img2_r, img2_g, img2_b))

plt.figure(figsize=(20, 10))

plt.subplot(2, 4, 1), plt.imshow(img1_r, 'gray'), plt.title('R channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 2), plt.imshow(img1_g, 'gray'), plt.title('G channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 3), plt.imshow(img1_b, 'gray'), plt.title('B channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 4), plt.imshow(img1_rgb), plt.title('RGB'), plt.xticks([]), plt.yticks([])

plt.subplot(2, 4, 5), plt.imshow(img2_r, 'gray'), plt.title('R channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 6), plt.imshow(img2_g, 'gray'), plt.title('G channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 7), plt.imshow(img2_b, 'gray'), plt.title('B channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 8), plt.imshow(img2_rgb), plt.title('RGB'), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()   

在这里插入图片描述

彩色图像中的噪声

# RGB channel merge to RGB image
img1_r = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(a)(lenna-noise-R-gauss-mean0-var800).tif', 0)
img1_g = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(b)(lenna-noise-G-gauss-mean0-var800).tif', 0)
img1_b = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(c)(lenna-noise-B-gauss-mean0-var800).tif', 0)
img1_rgb = np.dstack((img1_r, img1_g, img1_b))

plt.figure(figsize=(10, 10))

plt.subplot(2, 2, 1), plt.imshow(img1_r, 'gray'), plt.title('R channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 2), plt.imshow(img1_g, 'gray'), plt.title('G channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 3), plt.imshow(img1_b, 'gray'), plt.title('B channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 4), plt.imshow(img1_rgb), plt.title('RGB'), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()   

在这里插入图片描述

# convert RGB to HSI, noise affect all channels
img1_hsi = cv2.cvtColor(img1_rgb, cv2.COLOR_RGB2HSV_FULL)

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1), plt.imshow(img1_hsi[:, :, 0], 'gray'), plt.title('Hue'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 2), plt.imshow(img1_hsi[:, :, 1], 'gray'), plt.title('Saturation'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 3, 3), plt.imshow(img1_hsi[:, :, 2], 'gray'), plt.title('Intensity'), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()   

在这里插入图片描述

# RGB image, only green channel affect noise, but convert to HSI, all channel affect
img1_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH06/Fig0650(a)(rgb_image_G_saltpep_pt05).tif')
img1_rgb = img1_ori[:, :, ::-1]

plt.figure(figsize=(20, 10))

img1_hsi = cv2.cvtColor(img1_rgb, cv2.COLOR_RGB2HSV_FULL)

plt.subplot(2, 4, 1), plt.imshow(img1_rgb), plt.title('RGB'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 2), plt.imshow(img1_hsi[:, :, 0], 'gray'), plt.title('Hue'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 3), plt.imshow(img1_hsi[:, :, 1], 'gray'), plt.title('Saturation'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 4), plt.imshow(img1_hsi[:, :, 2], 'gray'), plt.title('Intensity'), plt.xticks([]), plt.yticks([])

plt.subplot(2, 4, 5), plt.imshow(img1_rgb[:, :, 0], 'gray'), plt.title('R channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 6), plt.imshow(img1_rgb[:, :, 1], 'gray'), plt.title('G channel'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 4, 7), plt.imshow(img1_rgb[:, :, 2], 'gray'), plt.title('B channel'), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()   

在这里插入图片描述

  • 2
    点赞
  • 2
    评论
  • 4
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值