彩色图像处理

"""RGB图像转换为HSI图像"""
from skimage import data, io
from matplotlib import pyplot as plt
import math
import numpy as np
import sys

# 定义RGB转HSI
def rgb2hsi(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('flower.jpg')
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 = rgb2hsi(r, g, b)
        hsi_image[ii, jj, :] = (h, s, i)

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

plt.subplot(2, 3, 1)
plt.axis('off')
plt.imshow(image)
plt.title('RGB原图像')

plt.subplot(2, 3, 2)
plt.axis('off')
plt.imshow(image[:, :, 0], cmap='gray')
plt.title('R分量')

plt.subplot(2, 3, 3)
plt.axis('off')
plt.imshow(hsi_image)
plt.title('HSI图像')

plt.subplot(2, 3, 4)
plt.axis('off')
plt.imshow(hsi_image[:, :, 0], cmap='gray')
plt.title('H分量')

plt.subplot(2, 3, 5)
plt.axis('off')
plt.imshow(hsi_image[:, :, 1], cmap='gray')
plt.title('S分量')

plt.subplot(2, 3, 6)
plt.axis('off')
plt.imshow(hsi_image[:, :, 2], cmap='gray')
plt.title('I分量')


plt.savefig('HSIimage.tif')
plt.show()
"""彩色图像灰度化"""
from skimage import data
from matplotlib import pyplot as plt
import numpy as np

# 载入测试图像
image = data.coffee()

# 初始化灰度图像
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 + g + b) / 3
        ave_gray[ii, jj] = r / 3 + g / 3 + b / 3
        # 加权平均法
        weight_gray[ii, jj] = 0.30 * r + 0.59 * g + 0.11 * b

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

plt.subplot(2,2,1)
plt.axis('off')
plt.imshow(image)
plt.title('彩色图像')

plt.subplot(2,2,2)
plt.axis('off')
plt.imshow(max_gray, cmap='gray')
plt.title('最大值法')

plt.subplot(2,2,3)
plt.axis('off')
plt.imshow(ave_gray, cmap='gray')
plt.title('平均值法')

plt.subplot(2,2,4)
plt.axis('off')
plt.imshow(weight_gray, cmap='gray')
plt.title('加权平均法')

plt.savefig('彩色图像灰度化.tif')

plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值