matlab灰度变彩色+白平衡算法实现

% matlab彩色图像分离通道并合并成处理后的彩色图像(彩色变灰度再变彩色)
% 白平衡算法(灰度世界法)消除RGB受光照影响
clear all;
close all;
%读入原始图像
srcImage = imread('7.jpg');
figure,imshow(srcImage);
srcImageGray = rgb2gray(srcImage);
figure,imshow(srcImageGray);
%白平衡算法
R = srcImage(:,:,1);
G = srcImage(:,:,2);
B = srcImage(:,:,3);
%求其各通道平均值及增益
Raver = mean(mean(R));
Gaver = mean(mean(G));
Baver = mean(mean(B));
K = (Raver+Gaver+Baver)/3;
Kr = K/Raver;
Kg = K/Gaver;
Kb = K/Baver;
%更新RGB值
Rnew = R*Kr;
Gnew = G*Kg;
Bnew = B*Kb;
%合并通道
% [m,n,k] = size(srcImage);
% dstImage = zeros(m,n,k);
dstImage(:,:,1) = Rnew;
dstImage(:,:,2) = Gnew;
dstImage(:,:,3) = Bnew;

figure,imshow(dstImage);
dstImageGray = rgb2gray(dstImage);
figure,imshow(dstImageGray);

结果图如下:

原图:

白平衡后图:

 

posted on 2016-07-27 18:26  在某一天老去  阅读(4807)  评论(0编辑  收藏  举报

导航