RGB2YCbCr RGB2Gray,其中 ycbcr中的Y 和 gray 都是表示灰度,这两种灰度有什么区别呢?
RGB2YCbCr RGB2Gray灰度的区别
简单来说,他们两RGB乘的数值不同,计算公式不同。FPGA上大部分转灰度都是使用Ycbcr中Y分量这个形式。
Y = 0.257R+0.564G+0.098*B+16
Gray = R0.299 + G0.587 + B*0.114
Matlab代码:
%RGB_YCbCr
%RGB_YCbCr
clc;
clear all;
close all;
RGB_data = imread('lena.jpg');
figure;
imshow(RGB_data);
subplot(1,3,1)
imshow(RGB_data);
title('原始图像');
%--------------------------------------------------------
R_data = RGB_data(:,:,1);
G_data = RGB_data(:,:,2);
B_data = RGB_data(:,:,3);
[ROW,COL, DIM] = size(RGB_data);
Y_data = zeros(ROW,COL);
Cb_data = zeros(ROW,COL);
Cr_data = zeros(ROW,COL);
Gray_data = RGB_data;
for r = 1:ROW
for c = 1:COL
Y_data(r, c) = 0.299*R_data(r, c) + 0.587*G_data(r, c) + 0.114*B_data(r, c);
Cb_data(r, c) = -0.172*R_data(r, c) - 0.339*G_data(r, c) + 0.511*B_data(r, c) + 128;
Cr_data(r, c) = 0.511*R_data(r, c) - 0.428*G_data(r, c) - 0.083*B_data(r, c) + 128;
end
end
Gray_data(:,:,1)=Y_data;
Gray_data(:,:,2)=Y_data;
Gray_data(:,:,3)=Y_data;
%灰度
subplot(1,3,2)
imshow(Gray_data);
title('Ycbcr灰度图像');
Gray_data2 = rgb2gray(RGB_data)
subplot(1,3,3)
imshow(Gray_data2);
title('gray灰度图像');
所用函数为imnoise (img, type),该函数中的type可以为5种噪声参数,分别为:
imgn = imnoise(Gray_data,'salt & pepper',0.02);
%加入椒盐噪声后的图像
figure;
imshow(imgn); %加入椒盐噪声的图像
imgn2 = imnoise(Gray_data, 'gaussian');
%加入高斯白噪声后的图像
figure;
imshow(imgn2);
%这里处理的是椒盐噪声的图像
Median_Img = Gray_data;%转存一次
for r = 2:ROW-1
for c = 2:COL-1
median3x3 =[imgn(r-1,c-1) imgn(r-1,c) imgn(r-1,c+1)
imgn(r,c-1) imgn(r,c) imgn(r,c+1)
imgn(r+1,c-1) imgn(r+1,c) imgn(r+1,c+1)];
sort1 = sort(median3x3, 2, 'descend');
sort2 = sort([sort1(1), sort1(4), sort1(7)], 'descend');
sort3 = sort([sort1(2), sort1(5), sort1(8)], 'descend');
sort4 = sort([sort1(3), sort1(6), sort1(9)], 'descend');
mid_num = sort([sort2(3), sort3(2), sort4(1)], 'descend');
Median_Img(r,c) = mid_num(2);
end
end
%中值滤波
figure;
imshow(Median_Img);
%--------------------------------------------------------
%均值滤波
r=0, c = 0;
for r = 2:1:ROW-1
for c = 2:1:COL-1
Mean_Img(r,c) = (imgn(r-1, c-1) + imgn(r-1, c) + imgn(r-1, c+1) + imgn(r, c-1) + imgn(r, c) + imgn(r, c+1) + imgn(r+1, c-1) + imgn(r+1, c) + imgn(r+1, c+1)) / 9;
end
end
figure;
imshow(Mean_Img);
title('均值滤波图像');
Median_Img = double(Median_Img); #对中值滤波的数值进行转double精度处理
Sobel_Threshold = 150;
Sobel_Img = zeros(ROW,COL);
for r = 2:ROW-1
for c = 2:COL-1
Sobel_x = Median_Img(r-1,c+1) + 2*Median_Img(r,c+1) + Median_Img(r+1,c+1) - Median_Img(r-1,c-1) - 2*Median_Img(r,c-1) - Median_Img(r+1,c-1);
Sobel_y = Median_Img(r-1,c-1) + 2*Median_Img(r-1,c) + Median_Img(r-1,c+1) - Median_Img(r+1,c-1) - 2*Median_Img(r+1,c) - Median_Img(r+1,c+1);
Sobel_Num = abs(Sobel_x) + abs(Sobel_y);
%Sobel_Num = sqrt(Sobel_x^2 + Sobel_y^2);
if(Sobel_Num > Sobel_Threshold)
Sobel_Img(r,c)=255;
else
Sobel_Img(r,c)=0;
end
end
end
%sobel后的图像
figure;
imshow(Sobel_Img);
title('中值滤波后进行sobel处理');
先腐蚀后膨胀叫开运算,开运算的作用是清除图像边缘周围非边缘的细小的点。先膨胀后腐蚀为闭运算,闭运算的作用是清除图像内部的空洞。
介绍
大磊FPGA腐蚀膨胀介绍
Dilation_img = zeros(ROW,COL);
for r = 2:ROW-1
for c = 2:COL-1
%每行先做或运算,最后三个或运算在相或,这是膨胀
or1 = bitor(Sobel_Img(r-1, c-1), bitor(Sobel_Img(r-1, c), Sobel_Img(r-1, c+1)));
or2 = bitor(Sobel_Img(r, c-1), bitor(Sobel_Img(r, c), Sobel_Img(r, c+1)));
or3 = bitor(Sobel_Img(r+1, c-1), bitor(Sobel_Img(r+1, c), Sobel_Img(r+1, c+1)));
Dilation_img(r, c) = bitor(or1, bitor(or2, or3));
end
end
%显示膨胀的后图像
figure;
imshow(Dilation_img);
%Erosion
Erosion_img = zeros(ROW,COL);
for r = 2:ROW-1
for c = 2:COL-1
and1 = bitand(Dilation_img(r-1, c-1), bitand(Dilation_img(r-1, c), Dilation_img(r-1, c+1)));
and2 = bitand(Dilation_img(r, c-1), bitand(Dilation_img(r, c), Dilation_img(r, c+1)));
and3 = bitand(Dilation_img(r+1, c-1), bitand(Dilation_img(r+1, c), Dilation_img(r+1, c+1)));
Erosion_img(r, c) = bitand(and1, bitand(and2, and3));
end
end
%腐蚀操作
figure;
imshow(Erosion_img);
Matlab 数字图像处理教程 BY NINGHECHUANG