【数字图像处理】彩色图像处理

本文介绍了图像处理中RGB和HSI颜色空间的转换方法,包括从RGB到HSI的转换函数,并展示了如何通过HSI颜色空间进行图像分割。此外,还探讨了伪色彩图像处理,将灰度图像转换为彩色图像,以及基于颜色的图像分割,分别在HSI和RGB空间中进行了实例展示。最后,讨论了彩色图像的灰度化处理,包括最大值、平均值和加权平均三种方法。
摘要由CSDN通过智能技术生成

颜色空间

RGB空间

  1. 由3个图像分量组成,分别是R通道图像、G通道图像和B通道图像。
from skimage import data,io,exposure
from matplotlib import pyplot as plt

#读入图像
#image = data.chelsea()
image = io.imread('tomato.jpg')

plt.subplot(2, 2, 1)
plt.title('RGB image')
plt.axis('off') #不显示坐标
plt.imshow(image)

#R通道图像
imageR = image[:, :, 0]
plt.subplot(2, 2, 2)
plt.title('R image')
plt.axis('off')
plt.imshow(imageR,cmap='gray')

#G通道图像
imageG = image[:, :, 1]
plt.subplot(2, 2, 3)
plt.title('G image')
plt.axis('off')
plt.imshow(imageG,cmap='gray')

#B通道图像
imageB = image[:, :, 2]
plt.subplot(2, 2, 4)
plt.title('B image')
plt.axis('off')
plt.imshow(imageB,cmap='gray')

plt.show()

plt.tight_layout()
plt.savefig('image.png')

在这里插入图片描述

HSI颜色空间

  1. 三个分量为色调(H)、饱和度(S)和亮度(I)。
  2. 空间圆柱体的横截面称为色环,色调H由角度表示,说明最接近的光谱波长;饱和度S由圆心出发到颜色点的半径距离表示,越高就越鲜艳;亮度I由颜色点到圆柱底部的距离表示。
  3. 圆柱体底部圆心表示黑色,顶部表示白色。

相互转换

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

from skimage import data, io
from matplotlib import pyplot as plt
import math
import numpy as np
import sys


# 定义RGB图像转换为HSI图像的函数
def RGB_to_HSI(r, g, b):
    r = r / 255
    g = g / 255
    b = b / 255
    num = 0.5 * ((r - g) + (r - b))
    den = ((r - g) * (r - g) + (r - b) * (g - b)) ** 0.5
    if b <= g:
        if den == 0:
            den = sys.float_info.min
        h = math.acos(num / den)
    elif b > g:
        if den == 0:
            den = sys.float_info.min
        h = (2 * math.pi) - math.acos(num / den)
    s = 1 - (3 * min(r, g, b) / (r + g + b))
    i = (r + g + b) / 3
    return int(h), int(s * 100), int(i * 255)


image = io.imread('tomato.jpg')
#image = data.coffee()
hsi_image = np.zeros(image.shape, dtype='uint8')
for ii in range(image.shape[0]):
    for jj in range(image.shape[1]):
        r, g, b = image[ii, jj, :]
        h, s, i = RGB_to_HSI(r, g, b)
        hsi_image[ii, jj, :] = (h, s, i)

# 显示RGB原图像
plt.subplot(2, 4, 1)
plt.imshow(image)
plt.axis('off')
plt.title('RGB')

# 显示RGB原图像R分量
plt.subplot(2, 4, 2)
plt.imshow(image[:, :, 0])
plt.axis('off')
plt.title('RGB-R')

# 显示RGB原图像G分量
plt.subplot(2, 4, 3)
plt.imshow(image[:, :, 1])
plt.axis('off')
plt.title('RGB-G')

# 显示RGB原图像B分量
plt.subplot(2, 4, 4)
plt.imshow(image[:, :, 2])
plt.axis('off')
plt.title('RGB-B')

# 显示HSI原图像
plt.subplot(2, 4, 5)
plt.imshow(hsi_image)
plt.axis('off')
plt.title('HSI')

# 显示HSI图像H分量
plt.subplot(2, 4, 6)
plt.imshow(hsi_image[:, :, 0])
plt.axis('off')
plt.title('HSI-H')

# 显示HSI图像S分量
plt.subplot(2, 4, 7)
plt.imshow(hsi_image[:, :, 1])
plt.axis('off')
plt.title('HSI-S')

# 显示HSI图像I分量
plt.subplot(2, 4, 8)
plt.imshow(hsi_image[:, :, 2])
plt.axis('off')
plt.title('HSI-I')

plt.show()

在这里插入图片描述

伪色彩图像处理

灰度值到彩色变换

from skimage import data, io,color
from matplotlib import pyplot as plt
import math
import numpy as np
import sys

L = 255

#定义灰度值到彩色变换
def getR(gray):
    if gray < L/2:
        return 0
    elif gray > L/4 * 3:
        return L
    else:
        return 4 * gray - 2 * L

def getG(gray):
    if gray < L/4:
        return 0
    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

image = io.imread('tomato.jpg')
gray_image = color.rgb2gray(image) * 255
color_image = np.zeros(image.shape, dtype='uint8')
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        r, g, b = getR(gray_image[i, j]), getG(gray_image[i, j]), getB(gray_image[i, j])
        color_image[i, j, :] = (r, g, b)

plt.figure()
plt.axis('off')
plt.imshow(gray_image)

plt.figure()
plt.axis('off')
plt.imshow(color_image)

plt.show()

基于颜色的图像分割

HSI颜色空间中的分割

  1. 是面向颜色处理的,用色调H和饱和度S描述颜色,用亮度I描述光的强度。
  2. I分量与图像的彩色信息无关,H分量和S分量与人感受颜色的方式紧密相关。
from skimage import data, io,color
from matplotlib import pyplot as plt
import math
import numpy as np
import sys

#RGB to HSI
def rgbtohsi(r, g, b):
    r = r / 255
    g = g / 255
    b = b / 255
    num = 0.5 * ((r - g) + (r - b))
    den = ((r - g) * (r - g) + (r - b) * (r - b)) ** 0.5
    if b <= g:
        if den == 0:
            den = sys.float_info.min
        h = math.acos(num / den)
    elif b > g:
        if den == 0:
            den = sys.float_info.min
        h = (2 * math.pi) - math.acos((num / den))
    s = 1 - 3 * min(r, g ,b) / (r + g + b)
    i = (r + b + g) / 3
    return int(h), int(s * 100), int(i * 255)

image = io.imread('tomato.jpg')
hsi_image = np.zeros(image.shape, dtype='uint8')
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        r, g, b = image[i, j, :]
        h, s, i = rgbtohsi(r, g, b)
        hsi_image[i, j, :] = (h, s, i)

H = hsi_image[:, :, 0]
S = hsi_image[:, :, 1]
I = hsi_image[:, :, 2]

# 生成二值饱和度模板
S_template = np.zeros(S.shape, dtype='uint8')
for i in range(S.shape[0]):
    for j in range(S.shape[1]):
        if S[i, j] > 0.3 * S.max():
            S_template[i, j] = 1

# 色调图像与二值饱和度模板相乘可得到分割结果
F = np.zeros(H.shape, dtype='uint8')
for i in range(F.shape[0]):
    for j in range(F.shape[1]):
        F[i, j] = H[i, j] * S_template[i, j]

# 显示结果
plt.figure()
plt.axis('off')
plt.imshow(image)  # 显示原始RGB图像

plt.figure()
plt.axis('off')
plt.imshow(H, cmap='gray')  # 显示H分量

plt.figure()
plt.axis('off')
plt.imshow(S, cmap='gray')  # 显示S分量

plt.figure()
plt.axis('off')
plt.imshow(I, cmap='gray')  # 显示I分量

plt.figure()
plt.axis('off')
plt.imshow(S_template, cmap='gray')  # 显示二值饱和度模板

plt.figure()
plt.axis('off')
plt.imshow(F, cmap='gray')  # 显示分割结果

plt.show()

RGB颜色空间中的分割

from skimage import data, color, io
from matplotlib import pyplot as plt
import numpy as np
import math

image = io.imread(r'tomato.jpg')
r = image[:, :, 0]
g = image[:, :, 1]
b = image[:, :, 2]

# RGB颜色空间中的分割
# 选择样本区域
r_template = r[128:255, 85:169]
# 计算该区域的彩色点的平均向量a的红色分量
r_template_u = np.mean(r_template)
# 计算样本点红色分量的标准差
r_template_d = 0.0
for i in range(r_template.shape[0]):
    for j in range(r_template.shape[1]):
        r_template_d = r_template_d + (r_template[i, j] - r_template_u) * (r_template[i, j] - r_template_u)

r_template_d = math.sqrt(r_template_d / r_template.shape[0] / r_template.shape[1])
# 寻找符合条件的点,r_cut为红色分割图像
r_cut = np.zeros(r.shape, dtype='uint8')
for i in range(r.shape[0]):
    for j in range(r.shape[1]):
        if r[i, j] >= (r_template_u - 1.25 * r_template_d) and r[i, j] <= (r_template_u + 1.25 * r_template_d):
            r_cut[i, j] = 1
# image_cut为根据红色分割后的RGB图像
image_cut = np.zeros(image.shape, dtype='uint8')
for i in range(r.shape[0]):
    for j in range(r.shape[1]):
        if r_cut[i, j] == 1:
            image_cut[i, j, :] = image[i, j, :]

plt.figure()
plt.axis('off')
plt.imshow(image)  # 显示原始图像

plt.figure()
plt.axis('off')
plt.imshow(r)  # 显示R图像

plt.figure()
plt.axis('off')
plt.imshow(g)  # 显示G图像

plt.figure()
plt.axis('off')
plt.imshow(b)  # 显示B图像

plt.figure()
plt.axis('off')
plt.imshow(r_cut)  # 显示红色分割图像图像

plt.figure()
plt.axis('off')
plt.imshow(image_cut)  # 显示分割后的RGB图像

plt.show()

彩色图像的灰度化

  1. 彩色图像中(RGB空间)每一个像素点用R、G、B三个分量分别表示;而灰度图像的像素点用一个灰度值表示即可。
  2. 进行灰度处理的常用方法有:最大值灰度化法(R、G、B分量这3个数值的最大值作为灰度图像的灰度值)、平均值灰度化法(R、G、B分量这3个数的平均值)、加权平均灰度化法。
from skimage import data,io
import matplotlib.pyplot as plt
import numpy as np

# 载入RGB图像
image= io.imread('tomato.jpg')
# 初始化灰度图像
max_gray=np.zeros(image.shape[0:2],dtype='uint8')
ave_gray=np.zeros(image.shape[0:2],dtype='uint8')
weight_gray=np.zeros(image.shape[0:2],dtype='uint8')
for ii in range(image.shape[0]):
  for jj in range(image.shape[1]):
      r,g,b=image[ii,jj,:]
      # 最大值灰度化方法
      max_gray[ii,jj]=max(r,g,b)
      # 平均值灰度化方法
      ave_gray[ii,jj]=(r/3+g/3+b/3)
      # 加权平均灰度化方法
      weight_gray[ii,jj]=0.30*r+0.59*g+0.11*b

# 显示结果
plt.subplot(2,2,1)
plt.axis('off')
plt.title('image')
plt.imshow(image) # 显示原始图像

plt.subplot(2,2,2)
plt.axis('off')
plt.title('image max_gray')
plt.imshow(max_gray,cmap='gray') # 显示最大值灰度化图像

plt.subplot(2,2,3)
plt.axis('off')
plt.title('image ave_gray')
plt.imshow(ave_gray,cmap='gray') # 显示平均值灰度化图像

plt.subplot(2,2,4)
plt.axis('off')
plt.title('image weight_gray')
plt.imshow(weight_gray,cmap='gray') # 显示加权平均灰度化图像

plt.tight_layout()
plt.savefig('im.png')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值