# matlab experiment of digital image processing

Foreword: as an image algorithm siege lion, in the second semester of junior year in 2014, a dark green digital image processing (English version of Gonzalez) appeared in front of his desk. An unexpected door was opened by chance, and thus an indissoluble bond was formed. In those days, I kept searching for codes on the Internet, and I submitted my homework in less than 20 minutes after I got on the computer. I had already gone with the past years. After three years of smoking and drinking and two years of soy sauce workers, I just feel that my brain is becoming more and more inadequate. This time, I am determined to go back to the furnace and rebuild it. I hope I can open up my mind through consolidating the foundation, saying nothing more, and start classes!
----2020-7-7

## Experiment 8 color image filtering

1, Experimental purpose
Through this experiment, students can master the method of using MATLAB to process color images, and deepen their understanding of color space and color images.

2, Experimental principle
This experiment is designed based on the theory of color image processing in the course of digital image processing.
The preparatory knowledge of this experiment: Chapter VI color space in color image processing, color image processing methods.
The key points are as follows:
1. Conversion between RGB image and HSI image
2. Color image denoising
Median filtering technology

3, Experiment content
(1) HSI spatial component
Read Lena_ Color TIF image, do the following:
1. Write a function to convert it from RGB space to HIS space, and display its H component, S component and I component respectively.

```a=imread('F:\Images\lena_color.tif');
A=im2double(a);
[m,n,q]=size(A);
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
H=zeros(m,n);
for i1=1:m
for i2=1:n
num=0.5*(R(i1,i2)-G(i1,i2)+R(i1,i2)-B(i1,i2));
den=sqrt((R(i1,i2)-G(i1,i2))^2+(R(i1,i2)-B(i1,i2))*(G(i1,i2)-B(i1,i2)));
theta=acos(num/den)*180/pi;
if(B(i1,i2)<=G(i1,i2))
H(i1,i2)=theta;
else
H(i1,i2)=360-theta;
end
min1=min(R(i1,i2),G(i1,i2));
min1=min(B(i1,i2),min1);
S(i1,i2)=1-3/(R(i1,i2)+G(i1,i2)+B(i1,i2))*min1;
end
end
I=(R+G+B)/3;
subplot(221),imshow(a),title('Original image');
subplot(222),imshow(H),title('Chromaticity diagram H ');
subplot(223),imshow(S),title('Saturation diagram S');
subplot(224),imshow(I),title('Intensity diagram I');
``` 2. Write a function to convert the image from HIS space to RGB space, and display the R component, G component and B component respectively.

```a=imread('F:\Images\lena_color.tif');
A=rgb2hsv(a);
[m,n,q]=size(A);
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
H=zeros(m,n);
for i1=1:m
for i2=1:n
num=0.5*(R(i1,i2)-G(i1,i2)+R(i1,i2)-B(i1,i2));
den=sqrt((R(i1,i2)-G(i1,i2))^2+(R(i1,i2)-B(i1,i2))*(G(i1,i2)-B(i1,i2)));
theta=acos(num/den)*180/pi;
if(B(i1,i2)<=G(i1,i2))
H(i1,i2)=theta;
else
H(i1,i2)=360-theta;
end
min1=min(R(i1,i2),G(i1,i2));
min1=min(B(i1,i2),min1);
S(i1,i2)=1-3/(R(i1,i2)+G(i1,i2)+B(i1,i2))*min1;
end
end
I=(R+G+B)/3;

for i1=1:m
for i2=1:n
value1=I(i1,i2)*(1+S(i1,i2)*cos(H(i1,i2))/cos(pi/3-H(i1,i2)/180*pi));
value2=I(i1,i2)*(1-S(i1,i2));
switch (floor(H(i1,i2)/120))
case 0
im2(i1,i2,1)=value1;
im2(i1,i2,2)=3*I(i1,i2)-R(i1,i2)-B(i1,i2);
im2(i1,i2,3)=value2;
case 1
H(i1,i2)=H(i1,i2)-120;
im2(i1,i2,1)=value2;
im2(i1,i2,2)=value1;
im2(i1,i2,3)=3*I(i1,i2)-R(i1,i2)-G(i1,i2);
case 2
H(i1,i2)=H(i1,i2)-240;
im2(i1,i2,1)=3*I(i1,i2)-G(i1,i2)-B(i1,i2);
im2(i1,i2,2)=value2;
im2(i1,i2,3)=value1;
end
end
end
subplot(221),imshow(A),title('hsi chart');
subplot(222),imshow(R),title('R');
subplot(223),imshow(G),title('G');
subplot(224),imshow(B),title('B');
``` (2) Color smoothing in RGB space
Read Lena_ Color TIF image, do the following:
(1) Use 7 × 7. The mean filter smoothes all components of the color image, displays the original image and the smoothed image on the same screen, and explains the results;

```lena_color=imread('F:\Images\lena_color.tif');
lena_red=lena_color(:,:,1);
lena_green=lena_color(:,:,2);
lena_blue=lena_color(:,:,3);
H=1/49*ones(7,7);
lena_red1=imfilter(lena_red,H);
lena_green1=imfilter(lena_green,H);
lena_blue1=imfilter(lena_blue,H); %===Composite image========================
lena(:,:,1)=lena_red1;
lena(:,:,2)=lena_green1;
lena(:,:,3)=lena_blue1;
figure(1);
subplot(1,2,1);imshow(lena_color);title('Original drawing');
subplot(1,2,2);imshow(lena);title('7*7 Mean filtering');
``` (2) Use 7 × 7. The mean filter smoothes the red component of the color image, other components remain unchanged, the original image and the smoothed image are displayed on the same screen, and the results are explained;

```lena_color=imread('F:\Images\lena_color.tif');
lena_red=lena_color(:,:,1);
H=1/49*ones(7,7);
lena_red1=imfilter(lena_red,H);
lena(:,:,1)=lena_red1;
lena(:,:,2)=lena_color(:,:,2);
lena(:,:,3)=lena_color(:,:,3);
figure(1);
subplot(1,2,1);imshow(lena_color);title('origin');
subplot(1,2,2);imshow(lena);title('7*7red');
``` (3) The histogram equalization method is used to enhance the three components of the color image. The original image and the enhanced image are displayed on the same screen, and the results are explained;

```lena_color=imread('F:\Images\lena_color.tif');
lena_red=lena_color(:,:,1);
lena_green=lena_color(:,:,2);
lena_blue=lena_color(:,:,3);
lena_red1=histeq(lena_red);
lena_green1=histeq(lena_green);
lena_blue1=histeq(lena_blue);
lena(:,:,1)=lena_red1;
lena(:,:,2)=lena_green1;
lena(:,:,3)=lena_blue1;
figure(1);
subplot(1,2,1);imshow(lena_color);title('Original drawing');
subplot(1,2,2);imshow(lena);title('Enhanced image');
``` (4) Add salt and pepper noise to the image, a_ Noisy=imnoise (a, 'salt & Pepper', 0.02), select a suitable filter for filtering, and observe the filtering effect;

```lena_color=imread('F:\Images\lena_color.tif');
lena_color_noi=imnoise(lena_color,'salt & pepper',0.02);
lena_red=lena_color_noi(:,:,1);
lena_green=lena_color_noi(:,:,2);
lena_blue=lena_color_noi(:,:,3);
lena_red1=medfilt2(lena_red);
lena_green1=medfilt2(lena_green);
lena_blue1=medfilt2(lena_blue);
lena(:,:,1)=lena_red1;
lena(:,:,2)=lena_green1;
lena(:,:,3)=lena_blue1;
figure(1);
subplot(1,3,1);imshow(lena_color);title('Original drawing');
subplot(1,3,2);imshow(lena_color_noi);title('Salt and pepper noise diagram');
subplot(1,3,3);imshow(lena);title('Median filter graph');
``` (5) Add Gaussian noise to the image, a_ Noisy=imnoise (a, 'gauss', 0.02), select a suitable filter for filtering, and observe the filtering effect;

```lena_color=imread('F:\Images\lena_color.tif');
lena_color_noi=imnoise(lena_color,'gauss',0.02);
lena_red=lena_color_noi(:,:,1);
lena_green=lena_color_noi(:,:,2);
lena_blue=lena_color_noi(:,:,3);
H=1/49*ones(7,7);
lena_red1=imfilter(lena_red,H);
lena_green1=imfilter(lena_green,H);
lena_blue1=imfilter(lena_blue,H);
lena(:,:,1)=lena_red1;
lena(:,:,2)=lena_green1;
lena(:,:,3)=lena_blue1;
figure(1);
subplot(1,3,1);imshow(lena_color);title('Original drawing');
subplot(1,3,2);imshow(lena_color_noi);title('Gaussian noise figure');
subplot(1,3,3);imshow(lena);title('Mean filter graph');
``` Thinking: what should we pay attention to when filtering color images?

4, Experimental methods and procedures
1. Complete the above experiments in sequence
2. According to the requirements of experiment content, analyze and program, arrange the program and experiment results into word documents, analyze the results, and prepare the experiment report.

5, Test report requirements
1. The experiment was completed by students alone.
2. Each experiment was reported in a uniform format.
The contents of the experiment report include: experiment requirements, experiment items, typical program flow chart, program list, data results, analysis and discussion.

Posted by kwong on Thu, 02 Jun 2022 10:29:54 +0530