Experiment 1 MATLAB image processing basis
MATLAB 具有强大的图像处理工具箱,有助于人们更好的理解各种图像处理算法,通 过调用工具箱中的函数,可以减少编程的复杂性,简化编程。本次实验主要掌握 MATLAB中的一些基本图像操作函数,包括图像的输入、显示、查看图像属性、改变大小、获取图像的统计信息、保存、多波段图像读写等功能,并进行简单的图像处理编程操作。
1. The purpose of the experiment
1. Further consolidate the understanding of Matlab development environment;
2. 2. Understand the related functions of image processing and master their usage;
3. 3. Understand and master file operation functions
; 4. Master the help commands of matlab and learn to use the help functions of matlab
; Master the basic methods of matlab programming, and realize simple image processing operations through programming.
2. Experimental basis
1. Image processing related functions
Matlab provides a variety of image processing functions. Table 1 lists some functions and their functions.
imread
read image
imshow
image display
imwrite
save image
colorbar
display colorbar
imfinfo
get image info
subimage
display image
imresize
change image size
imcrop
cut image
imrotate
image zoom
imhist
display image histogram
mean
calculate image mean
warp
texture map
std2
calculate image standard deviation
multibandread
read multiband binary image
corr2
calculate correlation coefficient
multibandwrite
write multiband binary image
2. The file operation function
fopen
opens the file
fread
reads the binary file
fclose
closes the file
fwrite
writes the binary file
fscanf
reads the text file
fprintf
writes the text file
feof
tests whether the pointer is at the end of the file
fseek
sets the file pointer position
frewind
resets the pointer to the beginning of the file
ftell
get file pointer position
3. Image grayscale histogram
The image grayscale histogram is a function of grayscale, describing the number or frequency of each grayscale pixel in the image. The abscissa is the gray level, and the ordinate is the number of pixels each gray level has or the frequency of occurrence of the gray level.
Gray level frequency calculation formula:
v_i=n_i/n
where ni is the number of occurrences of gray level i, and n is the total number of pixels.
4. Entropy
Entropy is the representation of the amount of image information, which reflects the richness of image information and is of great significance in image coding and image quality evaluation.
H=-∑_(i=0)^(L-1)▒p_i 〖log〗_2 p_i
In the formula, pi is the probability of occurrence of gray level i.
3. Experiment content and steps
1. Familiar with the working environment of Matlab
(1) Turn on the computer, start the MATLAB program, and enter the working interface of MATLAB;
(2) Familiar with the MATLAB menu and the functions of each toolbar; the
menu bar
current folder
command line window
workspace
(3) Create a matrix in the command window and operate on the elements in the matrix;
a=zeros(5,5)
a =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0a(2:4,2:4)=1
a =
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
(4) Create a matrix and perform matrix operations;a=[1 2 3 4 5 6];
b=[6 5 4 3 2 1];
a+b
ans =
7 7 7 7 7 7a.*b
ans =
6 10 12 12 10 6a-b
ans =
-5 -3 -1 1 3 5a./b
ans =
0.1667 0.4000 0.7500 1.3333 2.5000 6.0000a*b’
ans =
56a'*
bans =
6 5 4 3 2 1
12 10 8 6 4 2
18 15 12 9 6 3
24 20 16 12 8 4
30 25 20 15 10 5
36 30 24 18 12 6
(5) Plot the following two-dimensional function , and change the color and line type of the curve, which are drawn with blue solid lines, red dotted lines, and black dots, respectively.
y=2e^(-5x) sin(2πx) 0≤x≤2π
2. Familiar with the help commands of Matlab, learn to use the help information of Matlab; and use the help commands to view the image processing functions.
help command: View help for a function
help subplot
subplot Create axes in tiled positions.
H = subplot(m,n,p), or subplot(mnp), breaks the Figure window
into an m-by-n matrix of small axes, selects the p-th axes for
the current plot, and returns the axes handle. The axes are
counted along the top row of the Figure window, then the second
row, etc. For example,
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
type command: view the original code
type fft2
function f = fft2(x, mrows, ncols)
%FFT2 Two-dimensional discrete Fourier Transform.
% FFT2(X) returns the two-dimensional Fourier transform of matrix X.
% If X is a vector, the result will have the same orientation.
% FFT2(X,MROWS,NCOLS) pads matrix X with zeros to size MROWS-by-NCOLS
% before transforming.
% Class support for input X:
% float: double, single
% See also FFT, FFTN, FFTSHIFT, FFTW, IFFT, IFFT2, IFFTN.
% Copyright 1984-2010 The MathWorks, Inc.
if ismatrix(x)
if nargin==1
f = fftn(x);
else
f = fftn(x,[mrows ncols]);
end
else
if nargin==1
f = fft(fft(x,[],2),[],1);
else
f = fft(fft(x,ncols,2),mrows,1);
end
end
lookfor command:
lookfor plot(x,y,'k.')
did not find plot(x,y,'k.').
3. Familiar with the file operation functions in Matlab
(1) Create a data file test.dat to store the data of matrix A.
(2) Read the content of the file test.dat
B =
1 8
4 3
7 6
2 9
5 0
cnt =
9
(3) File positioning
dingwei
four =
4
position =
8
eight =
5
4. Basic image operations
Familiarize yourself with the matlab image processing functions provided in Table 1, and perform basic operations on images.
(1) Image read and write operations. Such as
(2) Perform basic operations on images. Such as
5. Programming
(1) Write function files, calculate
(2) Write and count the histogram of a grayscale image in Matlab language, and display the image and its grayscale histogram in the same graphics window;
(3) Calculate the entropy of a grayscale image.
(4) Program the image mirroring operation and display it.
Fourth, the experimental experience
1. Know the MATLAB interface and know the basic programming statements.
2. Learned the input and output of images, basic image processing operations, and can solve the grayscale histogram and entropy of images.
3. The statements used are different, the programming codes are varied, and different statements have certain similarities, and they can all get the correct results.