- Author:ZERO-A-ONE
- Date:2021-05-10
一、实验目的
- 了解图像的基本的类型和表示方式
- 了解索引图像中图像矩阵与调色板的关系
- 掌握RGB图像分层结构及分层加强的方法
二、实验内容与要求
- 写出“将输入的RGB图像分层,并将图像的指定层加强”的描述算法(可参考课本中给的程序)
- 分别显示“image.jpg"的原始图像、R层灰度图像、G层灰度图像、B层灰度图像及R层色彩增强图像
三、实验过程
3.1 任务一
使用Python中的PIL库即可完成任务
from PIL import Image
im = Image.open('test.jpg')
pix = im.load()
width = im.size[0]
height = im.size[1]
for x in range(width):
for y in range(height):
tmp = []
r, g, b = pix[x, y]
r = r + 2
g = g + 20
b = b + 50
tmp.append(r)
tmp.append(g)
tmp.append(b)
pix[x, y] = tuple(tmp)
im.save("增强.jpg")
原图
增强后图片
3.2 任务二
使用opencv-python库即可完成任务
import numpy as np
import cv2
image = cv2.imread("test.jpg")
#test = cv2.imshow("Original",image)
#R、G、B分量的提取
(B,G,R) = cv2.split(image)#提取R、G、B分量
cv2.imwrite('R.png',R)
cv2.imwrite('G.png',G)
cv2.imwrite('B.png',B)
R分量
G分量
B分量
另附Matlab相关代码:
%文件名:rgbanalysis.m
%程序员:郭迟
%编写时间:2004.2.8
%函数功能:将输入的RGB图像分层,并将图像的指定层加强
%输入格式举例:[imageRGB,imageR,imageG,imageB,result]=rgbanalysis('c:\lenna.jpg','jpg',1)
%参数说明:
%image输入的原始RGB图像的地址
%permission为图像文件类型
%level为要加强的层:1为R,2为G,3为B
%imageRGB为输入的RGB图像的RGB矩阵
%imageR为R层分量的矩阵
%imageG为G层分量的矩阵
%imageB为B层分量的矩阵
%result为色彩加强的RGB矩阵
% jpg,png,bmp,tif格式均可运行,要求RGB模式
function [imageRGB,imageR,imageG,imageB,result]=rgbanalysis(image,permission,level)
%读取图像信息并转换为double型
imageRGB=imread(image,permission);
imageRGB=double(imageRGB)/255;
result=imageRGB;
%对图像进行分层提取
imageR=imageRGB(:,:,1);
imageG=imageRGB(:,:,2);
imageB=imageRGB(:,:,3);
%显示结果
subplot(331),imshow(imageRGB),title('原始图像');
subplot(334),imshow(imageR),title('R层灰度图像');
subplot(335),imshow(imageG),title('G层灰度图像');
subplot(336),imshow(imageB),title('B层灰度图像');
%对相应的层进行颜色加强和和
if level==1
imageR=imageR+0.2;
end
if level==2
imageG=imageG+0.2;
end
if level==3
imageB=imageB+0.2;
end
result(:,:,1)=imageR;
result(:,:,2)=imageG;
result(:,:,3)=imageB;
%通过图像写回保存将超出范围的像素值自动调整为最大
imwrite(result,'temp.jpg','jpg');
result=imread('temp.jpg','jpg');
%显示结果
subplot(337),imshow(result),title('色彩增强的结果') ;