FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

2.2 RGB与YCbCr转换原理

RGB与YCbCr依据以下公式进行转换:

RGB转为YCbCr:

Y = 0.257*R + 0.504*G + 0.098*B + 16

Cb = – 0.148*R – 0.291*G + 0.439*B + 128

Cr = 0.439*R – 0.368*G – 0.071*B + 128

YCbCr转为RGB:

R = 1.164*(Y-16) + 1.596*(Cr-128)

G = 1.164*(Y-16) – 0.392*(Cb-128) – 0.813*(Cr-128)

B = 1.164*(Y-16) + 2.017*(Cb-128)

通过Matlab对转换算法进行验证:

FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

Matlab转换代码:

%**********************************************************************

% ——————————————————————-

% Company: Cascatrix

% Engineer: Carson

%

% Create Date: 2023/02/11

% Design Name: rgb_ycbcr

% Module Name: rgb_ycbcr

% Tool Versions: v1.0

% Description: Convert RGB888 into YCbCr

%——————————————————————-

%*********************************************************************/

clear;clear all;clc;

% Load image

image_in = imread(cascatrix.jpg);

% Seperate R G B

image_r = int16(image_in(:,:,1));

image_g = int16(image_in(:,:,2));

image_b = int16(image_in(:,:,3));

% Get image size

[row,col,n] = size(image_in);

% Calculate Y Cb Cr

image_y = 0.257*image_r + 0.504*image_g + 0.098*image_b + 16;

image_cb = – 0.148*image_r – 0.291*image_g + 0.439*image_b + 128;

image_cr = 0.439*image_r – 0.368*image_g – 0.071*image_b + 128;

% Recover R G B

image_out_r = 1.164*(image_y – 16) + 1.596*(image_cr – 128);

image_out_g = 1.164*(image_y – 16) – 0.392*(image_cb – 128) – 0.813*(image_cr – 128);

image_out_b = 1.164*(image_y – 16) + 2.017*(image_cb – 128);

% Output processed RGB image

image_out = cat(3,image_out_r,image_out_g,image_out_b);

% Display before/after processed image

figure(1)

subplot(121);

imshow(uint8(image_in)), title(Image in);

subplot(122);

imshow(uint8(image_out));title(Image out);

% Display Y Cb Cr

figure(2)

subplot(131);

imshow(uint8(image_y)),title(Image Y);

subplot(132);

imshow(uint8(image_cb)),title(Image Cb);

subplot(133);

imshow(uint8(image_cr)),title(Image Cr);

Matlab代码将输出结果:

1. 转换前/后图像对比:

FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

2. Y、Cb、Cr三通道图像显示:

FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

2.3 RGB生成灰度图像原理

RGB生成灰度图像的方法较多,常用转换公式如下:

采用RGB中任意单通道表示灰度值:

Gray = R (Gray = G or Gray = B)

采用RGB三通道的平均值表示灰度值:

Gray = (R + G + B) / 3

采用RGB三通道的加权均值表示灰度值:

Gray = 0.299*R + 0.587*G + 0.114*B

采用RGB三通道的最大/最小值的均值表示灰度值:

Gray = ( max(R, G, B) + min(R, G, B) ) / 2

通过Matlab对转换算法进行验证:

FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

Matlab转换代码(采用不同方法,转换结果略有区别):

%**********************************************************************

% ——————————————————————-

% Company: Cascatrix

% Engineer: Carson

%

% Create Date: 2023/02/11

% Design Name: rgb_gray

% Module Name: rgb_gray

% Tool Versions: v1.0

% Description: Convert RGB888 into gray value

%——————————————————————-

%*********************************************************************/

clear;clear all;clc;

% Load image

image_in = imread(cascatrix.jpg);

% Seperate R G B

image_r = int16(image_in(:,:,1));

image_g = int16(image_in(:,:,2));

image_b = int16(image_in(:,:,3));

% Get image size

[row,col,n] = size(image_in);

% Calculate gray value by different methods

gray = rgb2gray(image_in);

% gray = image_r;

% gray = 0.299 * image_r + 0.587 * image_g + 0.114 * image_b;

% gray = (image_r + image_g + image_b) / 3;

% Display before/after processed image

figure

subplot(121);

imshow(uint8(image_in)), title(Image in);

subplot(122);

imshow(uint8(gray));title(Image out);

Matlab代码将输出转换前/后图像:

FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:FPGA数字图像处理基础:色彩空间转换(Verilog)-fpga颜色识别 https://www.yhzz.com.cn/a/4507.html

上一篇 2023-04-11 01:33:21
下一篇 2023-04-11 01:37:56

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。