FPGA Player

好记性不如烂笔头

导航

RGB转换成YCbCr

clear all;
close all;
clc;

img=imread('colorbar.jpg');%('ngc6543a.jpg');
%img=mat2gray(img);  %[0,1];
[m n dim]=size(img);
imshow(img);
%picture RGB
img=double(img);
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);

%%RGB2YCbCr
Y=zeros(m,n);    %
Cb=zeros(m,n);   
Cr=zeros(m,n);   
matrix=[0.299 0.587 0.114;
        -0.172 -0.339 0.511;
        0.511 -0.428 -0.083];
for i=1:m
   for j=1:n 
        tmp=matrix*[R(i,j) G(i,j) B(i,j)]';
        Y(i,j)=tmp(1);
        Cb(i,j)=tmp(2)+128;
        Cr(i,j)=tmp(3)+128;
   end
end

%%YCbCr2RGB
matrix=inv(matrix);
for i=1:m
   for j=1:n 
        tmp=matrix*[Y(i,j) Cb(i,j)-128 Cr(i,j)-128]';
        R(i,j)=tmp(1);
        G(i,j)=tmp(2);
        B(i,j)=tmp(3);
   end
end

%%
img(:,:,1)=R;
img(:,:,2)=G;
img(:,:,3)=B;
figure;
imshow(uint8(img))

  

posted on 2017-10-30 16:58  中国的孩子  阅读(1840)  评论(0编辑  收藏  举报