当前位置:   article > 正文

Matlab 常用的图像处理方法_dilation_img

dilation_img

Matlab 常用的图像处理方法

RGB转灰度

RGB2YCbCr RGB2Gray,其中 ycbcr中的Ygray 都是表示灰度,这两种灰度有什么区别呢?
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灰度图像');
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

原始图像和ycbcr,Gray灰度图像对比

噪声添加

所用函数为imnoise (img, type),该函数中的type可以为5种噪声参数,分别为:

  • ‘gaussian’(高斯白噪声),
  • ‘localvar’(与图象灰度值有关的零均值高斯白噪声),
  • ‘poisson’(泊松噪声),
  • ‘salt & pepper’(椒盐噪声)
  • ‘speckle’(斑点噪声)。
 imgn = imnoise(Gray_data,'salt & pepper',0.02); 
  %加入椒盐噪声后的图像
 figure; 
 imshow(imgn); %加入椒盐噪声的图像
  • 1
  • 2
  • 3
  • 4

imgn2 = imnoise(Gray_data, 'gaussian'); 
%加入高斯白噪声后的图像
figure; 
imshow(imgn2); 
  • 1
  • 2
  • 3
  • 4

滤波

中值滤波

%这里处理的是椒盐噪声的图像
 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); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

均值滤波

%--------------------------------------------------------
%均值滤波
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('均值滤波图像');

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

很明显,均值滤波不适合用于处理椒盐噪声

sobel算子处理

 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处理');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

图像形态学运算

先腐蚀后膨胀叫开运算,开运算的作用是清除图像边缘周围非边缘的细小的点。先膨胀后腐蚀为闭运算,闭运算的作用是清除图像内部的空洞。
介绍
大磊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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

Reference

Matlab 数字图像处理教程 BY NINGHECHUANG

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/523910
推荐阅读
相关标签
  

闽ICP备14008679号