Experiment 1 Matlab image reading, writing and display (digital image processing)

Purpose

Master the matrix operation of matlab

Master the basic grammar of matlab

Master the functions and methods of reading, writing and displaying various digital images in matlab

Experimental content

1. Generate a 10*10 two-dimensional random matrix

Implement matrix transposition and cropping

Set rows 3 to 5 in the matrix to 0 and columns 5 to 7 to 0

Design ideas

1. Generate a random 10*10 matrix;
2. Display the original matrix;
3. Realize the transposition of the matrix; 4. Display the transposed matrix
;
Rows and columns 5 to 7 are set to 0;
6. Display the cropped matrix.

code and comments

% Generate a random 10*10 matrix

A = randi(100,10,10);

% display the original matrix

disp('Original Matrix:');

disp(A);

% realize the transpose of the matrix

At = A'; % Transpose using A'

% display the transposed matrix

disp('Transpose Matrix:');

disp(At);

% Realize the clipping of the matrix

% Set rows 3 to 5 and columns 5 to 7 in the matrix to 0

A(3:5,:) = 0; % Set lines 3 to 5 to 0

A(:,5:7) = 0; % Set columns 5 to 7 to 0

% Display the cropped matrix

disp('Cropped Matrix:');

disp(A);

Show results

Result analysis

The value of each element of the generated random matrix is ​​randomly generated between 1 and 100. By comparing the original matrix, the transposed matrix, and the cropped matrix, it can be found that: 1. The transpose operation will replace the rows of the original
matrix 2. The clipping operation will set all rows
3 to 5 and columns 5 to 7 in the matrix to 0.

2. The bubbling method realizes the sorting of 10 elements (without self-contained functions)

Design ideas

1. Generate 10 random numbers to form a 10-order vector;
2. Display unsorted vectors;
3. Use for loop nesting to achieve sorting, and each inner loop compares the size of adjacent elements and exchanges positions until the inner loop End;
4. Display the sorted vector.

code and comments

% Generate 10 random numbers

v = randi([1 100], 1, 10);

disp("Unsorted vector: ");

disp(v); % display unsorted vector

% Bubble Sort

n = number(v);

for i = 1:n

    for j = 1:n-i

        if v(j) > v(j+1)

            temp = v(j+1);

            v(j+1) = v(j);

            v(j) = temp;

        end

    end

end

% display the sorted vector

disp("Sorted vector: ");

disp(v);

Show results

Result analysis

The above code will sort the unordered elements, using the bubbling method, that is, the method of comparing two adjacent elements with each other

3. Create m files to realize image reading (imread) and display (image,

imshow , imagesc), save (imwrite) and information observation (size,

imfinfo )

Design ideas

1. Use the imread function to read a picture and obtain its image data;
2. Use the image or imshow function to display the image;
3. Use the imagesc function to display the image and set the color mapping;
4. Use the imwrite function to save the image;
5. Use the The size function gets the size of the image;
6. Use the imfinfo function to get the information of the image.

code and comments

% read image information

image_data = imread("C:\Users\52212\Desktop\xust.jpg

");

% display image

figure;

subplot(1, 3, 1);

image(image_data);

title("Display image using image function");

subplot(1, 3, 2);

imshow(image_data);

title("Show image using imshow function");

subplot(1, 3, 3);

imagesc(image_data);

colormap(gray);

title("Display images using the imagesc function");

% save the image

imwrite(image_data, "xust_result.jpg");

% Get image information

image_size = size(image_data);

image_info = imfinfo("xust.jpg ");

fprintf("The size of the image is: %dx %d\n", image_size(2), image_size(1));

fprintf("Image information is:\n");

disp(image_info);

Show results

Result analysis

Load a picture named xust.jpg and display it using image, imshow and imagesc functions respectively. Among them, the imagesc function converts the image into a grayscale image and performs color mapping to make the image clearer and easier to read.
Then, the code will use the imwrite function to save the processed image as example_result.jpg file, use the size function to get the size of the image, and use the imfinfo function to get the full information of the image.

4. Run the following code, observe the values ​​and types of variables A and B, write comments for each line of code, and analyze why the results are different from Figure 1 and Figure 2, and why Figure 1 and Figure 3 are the same.

code and comments

% Clear all variables and command windows
clear all;
clc;
% Read the image and save it in variable A
A=imread('cameraman.tif');
% Convert image type A to double and save it in variable B
B=double (A);
% Create a new graphics window
figure,
% Create a subplot with 1 row and 3 columns in this window, and display image A in the first column
subplot(1,3,1),imshow(A);
% Display image B in column 2
subplot(1,3,2),imshow(B);
% map the gray value of image B to the range [0,255] in column 3 and display
subplot(1,3, 3),imshow(B,[0,255]);

Show results

Result analysis

The difference between Figure 1 and Figure 2 is that image A is of uint8 type, that is, an 8-bit unsigned integer, and its gray value range is between [0,255], while image B is of type double, that is, double-precision floating-point type, gray Degree values ​​range between [0,1]. By default, the imshow function maps grayscale values ​​to the range [0,1] for display, so the grayscale displays in Figure 1 and Figure 2 are different.
Figure 1 and Figure 3 are the same because image B is mapped to the [0,255] range in the imshow function, which is the same gray value range as image A, so the display effect is the same.

problem and solution

1. The file name is wrong, so the picture cannot be opened correctly. Because there is a picture with the same name in the local file, there is an error and it cannot be run, or the address is wrong and there is a problem

Solution: Use matlab's built-in image, or change the local image address.

2. The function does not run correctly, the function cannot be found, and the image processing toolbox is not installed correctly, resulting in the function not being found or an error occurring.

Solution: re-downloaded the full version of matlab

Summary and experience

This experiment is based on the MATLAB image processing toolbox and the practice of matrix operations . Through the use of MATLAB functions to read, process and display images, the relevant knowledge of digital image processing is further understood and consolidated. Through this experiment, I have a deeper understanding and practical experience in image processing functions commonly used in MATLAB, such as imread, imshow, subplot, etc., and I am more familiar with matrix operations. At the same time, I also have a deeper understanding of programming. The importance should focus on programming practice, debugging and solving problems in a timely manner, so as to better grasp the knowledge learned.
At the same time, during the experiment, factors such as the size, type, brightness, contrast, and color of the image also need to be considered, especially the correctness and legality of the input parameters when calling the function, otherwise it is easy to cause the code to run incorrectly or abnormally.
In short, through the study of this experiment, I have a deeper understanding of digital image processing, and I have officially started learning about familiar image processing.

appendix

1.

% Generate a random 10*10 matrix

A = randi(100,10,10);

% display the original matrix

disp('Original Matrix:');

disp(A);

% realize the transpose of the matrix

At = A'; % Transpose using A'

% display the transposed matrix

disp('Transpose Matrix:');

disp(At);

% Realize the clipping of the matrix

% Set rows 3 to 5 and columns 5 to 7 in the matrix to 0

A(3:5,:) = 0; % Set lines 3 to 5 to 0

A(:,5:7) = 0; % Set columns 5 to 7 to 0

% Display the cropped matrix

disp('Cropped Matrix:');

disp(A);

2.

% Generate 10 random numbers

v = randi([1 100], 1, 10);

disp("Unsorted vector: ");

disp(v); % display unsorted vector

% Bubble Sort

n = number(v);

for i = 1:n

    for j = 1:n-i

        if v(j) > v(j+1)

            temp = v(j+1);

            v(j+1) = v(j);

            v(j) = temp;

        end

    end

end

% display the sorted vector

disp("Sorted vector: ");

disp(v);

3.

image_data = imread("C:\Users\52212\Desktop\xust.jpg

");

% display image

figure;

subplot(1, 3, 1);

image(image_data);

title("Display image using image function");

subplot(1, 3, 2);

imshow(image_data);

title("Show image using imshow function");

subplot(1, 3, 3);

imagesc(image_data);

colormap(gray);

title("Display images using the imagesc function");

% save the image

imwrite(image_data, "xust_result.jpg");

% Get image information

image_size = size(image_data);

image_info = imfinfo("xust.jpg ");

fprintf("The size of the image is: %dx %d\n", image_size(2), image_size(1));

fprintf("Image information is:\n");

disp(image_info);

4.

clear all;

clc;

A=imread('cameraman.tif');

B=double(A);

figure,

subplot(1,3,1),imshow(A);

subplot(1,3,2),imshow(B);

subplot(1,3,3),imshow(B,[0,255]);

Guess you like

Origin blog.csdn.net/m0_63975371/article/details/131392470