Nostalgic images and numerical fitting of RGB and HSI color spaces

Written earlier, this is a summary of the explanations about the numerical “XX” course assignments of the “X” course in optics at “XX” University. Jiang Yue looks the same every year, and so does the assignment.

I can’t go home during the Mid-Autumn Festival and National Day and have nothing to do, so I decided to write this answer on September 30, 2023, for reference only to save everyone’s time. If Anybody causes you to fail a class because of copying, and makes the teacher resist this form of communication, then you really deserve to die

Finally, I hope this answer will become a bridge to help all junior brothers and sisters to reach your poems and distant places faster

Part 1: Implemented functions

A. Required function one

1. Blur a color image through RGB and HSI color spaces, increase the neighborhood area (5 * 5, 11 * 11, 21 * 21), and observe the difference in the processing results of the two spaces (HSI only blurs brightness and contrast The difference between three components being blurred simultaneously)
2. Perform color processing on a color image – grayscale image, nostalgic image (using RGB space and HSI space respectively)

B. Required function two

1. Use the following statement to generate a pair of variables, use cftool, polyfit and fit to fit respectively, give the fitting parameter results and R-square values, and draw the original variables and fitting results in the same figure, x =0:2:20; y=0.1 * x + 0.2 * x .^ 2 + 0.5 * x .^ 5 + 10 * x .^ 10;

Part 2: Basic instructions

A. Functions used

rgb2hsi()

For functions given by the teacher, just ask the academic committee or the teacher directly. I have read it and the implementation idea is very simple. I respect intellectual property rights and labor results, so I will not provide it.
It should be noted that this function must be placed in the same path as your job file, that is, the same folder. (You can also put it in the default function library of matlab. If you don’t understand, just put it in the same folder.)
The function of this function is to convert the normal RGB image into an HSI color gamut space image

hsi2rgb()

For functions given by the teacher, just ask the academic committee or the teacher directly. I have read it and the implementation idea is very simple. I respect intellectual property rights and labor results, so I will not provide it.
It should be noted that this function must be placed in the same path as your job file, that is, the same folder. (You can also put it in the default function library of matlab. If you don’t understand, just put it in the same folder.)
The function of this function is to convert the hsi image into an rgb three-color image

B. Analysis of difficult ideas

There is no particular difficulty, except that you may not know how to start with the nostalgic images. Let me analyze it for you

The following are relatively perfect plans. Don’t hand in homework that is too perfect, otherwise it will be too difficult. You don’t add points. If the teacher compares the assignments, how can you hand in the assignments that you asked others to figure out on their own? ? ! ! ! ! ! !

1.rgb picture nostalgic

For old RGB photos, the key is fading, dullness and yellowing. If we want the photo to look old, we first need to make its color less bright, and secondly, make its color closer to yellow.

%############ Description ################
1. Yellowing: Adjust and increase the proportion of red and green in the picture, and the picture will look yellow.
2. Dim: Let the various colors interact with each other. Time will cause the photos to fade and the single bright color will disappear.
3. Fading: Blue cannot participate in the synthesis of yellow, and its total intensity value is required to decrease. The yellow color is caused by the paper being left for a long time, and the intensity value is slightly enhanced.
%############      illustrate      ################
pic_path = 'jiangnan.png' ; %Input picture path
pic = imread(pic_path); %Open the picture

r1 = pic(:,:,1); %red picture
g1 = pic(:,:,2); %green picture
b1 = pic(:,:,3); %blue picture

output_r = (r1 * 0.393) + (g1 * 0.769) + (b1 * 0.189); %Retro red diagram, the algorithm is derived from the equation suggested by Microsoft
output_g = (r1 * 0.349) + (g1 * 0.686) + (b1 * 0.168); %Retro green plot, the algorithm is derived from the equation suggested by Microsoft
output_b = (r1 * 0.272) + (g1 * 0.534) + (b1 * 0.131); % Retro blue plot, the algorithm is derived from the equation suggested by Microsoft

figure;
imshow(pic_rgb_fugu);
title('RGB:Nostalgic picture');

2.hsi picture nostalgic

The basic idea is the same, there are some subtle differences, and I didn’t adjust it perfectly, that’s it.

%############ Description ################
1. Yellowing: Adjust and increase the proportion of red and green in the picture, and the picture will look yellow.
2. Dim: Let the various colors interact with each other. Time will cause the photos to fade and the single bright color will disappear.
3. Fading: Blue cannot participate in the synthesis of yellow, and its total intensity value is required to decrease. The yellow color is caused by the paper being left for a long time, and the intensity value is slightly enhanced.
%############      illustrate      ################
pic_path = 'jiangnan.png' ; %Input picture path
pic = imread(pic_path); %Open the picture

pic_hsi = rgb2hsi(pic); %rgb image to hsi image, use the function given by the teacher, please put the function in the same folder as this file

h1=pic_hsi(:,:,1); %Layer: Angle? Hue
s1=pic_hsi(:,:,2); %Layer: Radius? Saturation
i1=pic_hsi(:,:,3); %layer: height? intensity

[m,n] = size(h1);
output_i = i1;
output_h = h1;
output_s = s1;

for i=1:m
    for j = 1:n
        if 0.5 < h1(i,j) & amp; & amp; h1(i,j) < 0.833
            output_i(i,j) = i1(i,j) * 0.7;
        end
    end
end
for i=1:m
    for j = 1:n
        if 0 <= h1(i,j) & amp; & amp; h1(i,j) < 0.33
            output_h(i,j) = h1(i,j);
        end
        if 0.33 <= h1(i,j) & amp; & amp; h1(i,j) < 0.5
            output_h(i,j) = h1(i,j) - 0.33;
        end
        if 0.5 <= h1(i,j) & amp; & amp; h1(i,j) < 0.833
            output_h(i,j) = h1(i,j);
        end
        if 0.833 <= h1(i,j) & amp; & amp; h1(i,j) < 1
            output_h(i,j) = h1(i,j) - 0.83;
        end
        if 0 <= s1(i,j) & amp; & amp; s1(i,j) < 0.7
            output_h(i,j) = 0.167;
        end
    end
end

output_s = s1 * 0.6;


pic_hsi_fugu(:,:,1) = output_h; %Integrate the matrix
pic_hsi_fugu(:,:,2) = output_s; %Integrate the matrix
pic_hsi_fugu(:,:,3) = output_i; %Integrate the matrix
pic_hsi_fugu = double(pic_hsi_fugu); % unit8 is converted to double
pic_hsi_rgb_fugu = hsi2rgb(pic_hsi_fugu); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file
%Show results
figure('Name','HSI color space-grayscale image, nostalgic image');
imshow(pic_hsi_rgb_fugu);
title('HSI:Nostalgic Picture');

Part 3: My homework

Pictures I used in my homework:

jiangnan.png
If you want to reproduce my results, you can use it, but copying homework is not supported

First question

% File name: DSH_3_1.m
% version: 1.0
% Creation time: Wednesday, September 27, 2023 15:30
% Function: 1. Blur a color image through RGB and HSI color spaces, increase the neighborhood area (5*5, 11*11, 21*21), and observe the difference in the processing results of the two spaces (HSI only applies brightness The difference between blurring and simultaneous blurring of three components)
% 2. Perform color processing on a color image - grayscale image, nostalgic image (using RGB space and HSI space respectively)

%¥¥¥¥¥¥ Function: 1. Blur a color image through RGB and HSI color spaces and increase the neighborhood area (5*5, 11*11, 21*21)
%¥¥¥¥¥¥¥¥¥¥Observe the difference between the two spatial processing results (HSI only blurs the brightness and blurs the three components simultaneously)
pic_path = 'jiangnan.png' ; %Input picture path
pic = imread(pic_path); %Open the picture
figure('Name','Original picture'); %Create a window that displays the original picture
imshow(pic); %Display pictures
%##############Blur a color image through RGB color space#################
r1 = pic(:,:,1); %red picture
g1 = pic(:,:,2); %green picture
b1 = pic(:,:,3); %blue picture

fil1 = fspecial('average',[5,5]); %Create a 5 * 5 filter matrix.
pic_ave_r1 = imfilter(r1,fil1,'symmetric'); %mean fuzzy
pic_ave_g1 = imfilter(g1,fil1,'symmetric'); %mean fuzzy
pic_ave_b1 = imfilter(b1,fil1,'symmetric'); % mean blur
pic_1(:,:,1) = pic_ave_r1; %Integrate the matrix
pic_1(:,:,2) = pic_ave_g1; %Integrate the matrix
pic_1(:,:,3) = pic_ave_b1; %Integrate the matrix

fil2 = fspecial('average',[11,11]); %Create a 11 * 11 filter matrix.
pic_ave_r2 = imfilter(r1,fil2,'symmetric'); %mean fuzzy
pic_ave_g2 = imfilter(g1,fil2,'symmetric'); % mean blur
pic_ave_b2 = imfilter(b1,fil2,'symmetric'); % mean blur
pic_2(:,:,1) = pic_ave_r2; %Integrate the matrix
pic_2(:,:,2) = pic_ave_g2; %Integrate the matrix
pic_2(:,:,3) = pic_ave_b2; %Integrate the matrix

fil3 = fspecial('average',[21,21]); %Create a 21 * 21 filter matrix.
pic_ave_r3 = imfilter(r1,fil3,'symmetric'); %mean fuzzy
pic_ave_g3 = imfilter(g1,fil3,'symmetric'); % mean blur
pic_ave_b3 = imfilter(b1,fil3,'symmetric'); % mean blur
pic_3(:,:,1) = pic_ave_r3; %Integrate the matrix
pic_3(:,:,2) = pic_ave_g3; %Integrate the matrix
pic_3(:,:,3) = pic_ave_b3; %Integrate the matrix
 %Show results
figure('Name','RGB mean blur');
subplot(1,3,1);
imshow(pic_1);
title('RGB:5 * 5 mean blur');
subplot(1,3,2);
imshow(pic_2);
title('RGB:11 * 11 mean blur');
subplot(1,3,3);
imshow(pic_3);
title('RGB:21 * 21 mean blur');
%##############Blur a color image through HSI color space#################
pic_hsi = rgb2hsi(pic); %rgb image to hsi image, use the function given by the teacher, please put the function in the same folder as this file

h1=pic_hsi(:,:,1); %Layer: Angle? Hue
s1=pic_hsi(:,:,2); %Layer: Radius? Saturation
i1=pic_hsi(:,:,3); %layer: height? intensity

fil1 = fspecial('average',[5,5]); %Create a 5 * 5 filter matrix.
pic_ave_i1 = imfilter(i1,fil1,'symmetric'); %mean fuzzy
pic_hsi_1(:,:,3) = pic_ave_i1; %Integrate the matrix
pic_hsi_1(:,:,2) = s1; %Integrate the matrix
pic_hsi_1(:,:,1) = h1; %Integrate the matrix
pic_rgb_1 = hsi2rgb(pic_hsi_1); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file

fil2 = fspecial('average',[11,11]); %Create a 11 * 11 filter matrix.
pic_ave_i2 = imfilter(i1,fil2,'symmetric'); % mean blur
pic_hsi_2(:,:,3) = pic_ave_i2; %Integrate the matrix
pic_hsi_2(:,:,2) = s1; %Integrate the matrix
pic_hsi_2(:,:,1) = h1; %Integrate the matrix
pic_rgb_2 = hsi2rgb(pic_hsi_2); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file

fil3 = fspecial('average',[21,21]); %Create a 21 * 21 filter matrix.
pic_ave_i3 = imfilter(i1,fil2,'symmetric'); % mean blur
pic_hsi_3(:,:,3) = pic_ave_i3; %Integrate the matrix
pic_hsi_3(:,:,2) = s1; %Integrate the matrix
pic_hsi_3(:,:,1) = h1; %Integrate the matrix
pic_rgb_3 = hsi2rgb(pic_hsi_3); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file
 %Show results
figure('Name','HSI mean fuzzy');
subplot(1,3,1);
imshow(pic_rgb_1);
title('HSI:5 * 5 mean fuzzy');
subplot(1,3,2);
imshow(pic_rgb_2);
title('HSI:11 * 11 mean fuzzy');
subplot(1,3,3);
imshow(pic_rgb_3);
title('HSI:21 * 21 mean fuzzy');

%¥¥¥¥¥¥Function: 2. Perform color processing on a color image - grayscale image, nostalgic image (using RGB space and HSI space respectively)
%#############Color processing of a color image through RGB color space - grayscale image, nostalgic image############### ##
%##############Grayscale image#################
pic_gray = rgb2gray (pic); %Generate grayscale image
 %Show results
figure('Name','RGB color space-grayscale image, nostalgic image');
subplot(1,2,1);
imshow(pic_gray);
title('RGB: Grayscale image');
%##############Nostalgia Image#################
r1 = pic(:,:,1); %red picture
g1 = pic(:,:,2); %green picture
b1 = pic(:,:,3); %blue picture
output_r = (r1 * 0.393) + (g1 * 0.769) + (b1 * 0.189); %Retro red diagram, the algorithm is derived from the equation suggested by Microsoft
output_g = (r1 * 0.349) + (g1 * 0.686) + (b1 * 0.168); %Retro green plot, the algorithm is derived from the equation suggested by Microsoft
output_b = (r1 * 0.272) + (g1 * 0.534) + (b1 * 0.131); % Retro blue plot, the algorithm is derived from the equation suggested by Microsoft

pic_rgb_fugu(:,:,1) = output_r; %Integrate the matrix
pic_rgb_fugu(:,:,2) = output_g; %Integrate the matrix
pic_rgb_fugu(:,:,3) = output_b; %Integrate the matrix
 %Show results
subplot(1,2,2);
imshow(pic_rgb_fugu);
title('RGB:Nostalgic picture');
%#############Color processing of a color image through HSI color space - grayscale image, nostalgic image############### ##
%##############Nostalgia Image#################
pic_hsi = rgb2hsi(pic); %rgb image to hsi image, use the function given by the teacher, please put the function in the same folder as this file

h1=pic_hsi(:,:,1); %Layer: Angle? Hue
s1=pic_hsi(:,:,2); %Layer: Radius? Saturation
i1=pic_hsi(:,:,3); %layer: height? intensity
output_h = h1 * 0.5; %Retro tone map, I adjusted it randomly by myself, but I couldn’t find a ready-made solution---DSH blank
output_s = s1 * 0.2; % Retro saturation map, I adjusted it myself, but I couldn’t find a ready-made solution---DSH blank
output_i = i1 * 0.6; % retro intensity map, I adjusted it randomly by myself, but I couldn’t find a ready-made solution---DSH blank

pic_hsi_fugu(:,:,1) = output_h; %Integrate the matrix
pic_hsi_fugu(:,:,2) = output_s; %Integrate the matrix
pic_hsi_fugu(:,:,3) = output_i; %Integrate the matrix
pic_hsi_fugu = double(pic_hsi_fugu); % unit8 is converted to double
pic_hsi_rgb_fugu = hsi2rgb(pic_hsi_fugu); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file
%Show results
figure('Name','HSI color space-grayscale image, nostalgic image');
subplot(1,2,2);
imshow(pic_hsi_rgb_fugu);
title('HSI:Nostalgic Picture');
%##############Grayscale image#################
pic_hsi_gray(:,:,1) = h1; % Integrate the matrix. I adjusted it myself and couldn't find a ready-made solution---DSH blank
pic_hsi_gray(:,:,2) = s1 * 0; %Integrate the matrix. I adjusted it myself and couldn't find a ready-made solution---DSH blank
pic_hsi_gray(:,:,3) = i1; % Integrate the matrix. I adjusted it myself and couldn't find a ready-made solution---DSH blank

pic_hsi_gray = hsi2rgb(pic_hsi_gray); %hsi image to rgb image, use the function given by the teacher, please put the function in the same folder as this file
 %Show results
subplot(1,2,1);
imshow(pic_hsi_gray);
title('HSI: Grayscale image');

fprintf('Hello, world\t --from DSH\
'); %Easter egg

Second question

% File name: DSH_3_2.m
% version: 1.0
% Creation time: Wednesday, September 27, 2023 15:30
% Function: 1 Use the following statement to generate a pair of variables, use cftool, polyfit and fit to fit respectively, give the fitting parameter results and R-square values, and draw the original variables and fitting results in the same figure , x=0:2:20; y=0.1*x + 0.2*x.^2 + 0.5*x.^5 + 10*x.^10;

x = 0 : 2 : 20; %The value of x asked by the teacher to choose
y = 0.1 * x + 0.2 * x .^ 2 + 0.5 * x .^ 5 + 10 * x .^ 10; %Functional relationship given by the teacher

cftool;

a = polyfit(x,y,7); %Perform polyfit fitting
y_polyfit = a(1) * x .^ 7 + a(2) * x .^ 6 + a(3) * x .^ 5 + a(4) * x .^ 4 + a(5) * x .^ 3 + a(6) * x .^ 2 + a(7) * x .^ 1 + a(8) * x .^ 0; %Generate the post-fitting expression and calculate the value of y_polyfit

f = fittype('p1 * x + p2 * x .^ 2 + p3 * x .^ 5 + p4 * x .^ 10'); %Set fittype, that is, set the form of the function, the function I set and the original function They are exactly the same, so R-square must be 1! !
[c,gof] = fit(x',y',f,'StartPoint', [0, 0, 0, 0]); % Use fit to fit, startpoint is the initial value of p1, p2, p3, p4
y_fit = c.p1 * x + c.p2 * x .^ 2 + c.p3 * x .^ 5 + c.p4 * x .^ 10; %Generate the post-fitting expression and calculate the value of y_fit

x1 = 0 : 1 : 40; %Regenerate the x value I want to find R-square
y1 = 0.1 * x1 + 0.2 * x1 .^ 2 + 0.5 * x1 .^ 5 + 10 * x1 .^ 10; %Use the regenerated x value to generate a more detailed original function
y1_polyfit = a(1) * x1 .^ 7 + a(2) * x1 .^ 6 + a(3) * x1 .^ 5 + a(4) * x1 .^ 4 + a(5) * x1 .^ 3 + a(6) * x1 .^ 2 + a(7) * x1 .^ 1 + a(8) * x1 .^ 0; %Use the regenerated x value to generate a more detailed polyfit fitting function
y1_fit = c.p1 * x1 + c.p2 * x1 .^ 2 + c.p3 * x1 .^ 5 + c.p4 * x1 .^ 10; %Use the regenerated x value to generate a more detailed fit simulation composite function
r2_polyfit = 1 - sum((y1(:)-y1_polyfit(:)).^2)/sum((y1(:)-mean(y1(:))).^2); % Calculate the R of polyfit fitting -square, where the sum function is summation and the mean function is averaging
r2_fit = 1 - sum((y1(:)-y1_fit(:)).^2)/sum((y1(:)-mean(y1(:))).^2); % Calculate the R of fit -square, where the sum function is summation and the mean function is averaging

figure %Create a display window
plot(x1, y1, 'ro', x1, y1_polyfit,'g*', x1, y1_fit, 'b-'); %Plot
legend('original function', strcat(['polyfit fitting,R2 = ' num2str(r2_polyfit)]), strcat(['fit fitting,R2 = ' num2str(r2_fit)])); %Write annotation, num2str function Convert numbers to characters and use the strcat function to combine two strings

fprintf('Hello, world\t --from DSHkongbai\
'); %Easter egg