1. Reading and writing images and histograms
1. Read and display images
imread and imshow functions
clc;clear; close all;
I = imread('pout.tif');
imshow(I);
result:
If we want to display the image coordinate axis, we only need to add the following line of code after imshow:
set(gca,'Visible','on');
We can also use the whos command to view image information:
Enter whos I in the command line window to view information about the above picture:
2. Histogram
Use the hist function to display the histogram of the image, and use the histeq function to equalize the histogram.
We continue to enter the following code based on the above code:
figure();
imhist(I);
result:
You can see that the contrast of the image is relatively low. We perform histogram equalization and continue to enter the following code:
I2 = histeq(I);
imshow(I2);
figure();
imhist(I2);
result:
3. Write image
We use the imwrite function to write the above equalized image I2 to disk:
imwrite(I2, 'output.png');
You can see that there is more output.png in the file area on the left
We can use the imfinfo function in the command line window to see more detailed information about the image:
expand:
Use the montage function to display multiple pictures in one window at a time, and these images do not need to be of the same size and size. We only need to put the names of the pictures in the current directory or already in the matlab path in a dictionary:
Run the following code:
clc;clear; close all;
files = {'catdog.jpg', 'jiedao.jfif', 'Fig0638(a)(lenna_RGB).tif', 'output.png', 'Fig0335(a)(ckt_board_saltpep_prob_pt05).tif'};
montage(files)
result:
We can even see that different types (grayscale images and RGB images) can be displayed together!
2. Image type conversion:
There are several image type conversion functions in the official MATLAB documentation:
We choose a few commonly used explanations:
1. img2gray converts RGB images into grayscale images
Code:
clc;clear; close all;
RGB = imread('example.tif');
figure;
imshow(RGB);
I = im2gray(RGB);
figure;
imshow(I);
result:
im2gray
The function is rgb2gray
basically the same as , except that it can accept grayscale images as input and return them unchanged. If the input image is grayscale, rgb2gray
the function returns an error.
2. mat2gray converts the matrix into a grayscale image
Code:
clc;clear; close all;
mat = [0:255]' * ones(1,256);
mat = mat';
img = mat2gray(mat);
imshow(img);
result:
3. imsplit splits a multi-channel image into its independent channels
Code:
clc;clear; close all;
I = imread('peppers.png');
imshow(I);
[r,g,b] = imsplit(I);
montage({r,g,b},'Size',[1 3]);
result:
4. rgb2hsv converts RGB images to HSV images
Code:
clc;clear; close all;
rgbImage = imread('peppers.png');
imshow(rgbImage);
hsvImage = rgb2hsv(rgbImage);
[h,s,v] = imsplit(hsvImage);
montage({h,s,v},'Size',[1 3])
result:
There are many conversions between color spaces that I won’t list one by one:
5. Binarization of grayscale images
I won’t demonstrate it here.
6. Data type conversion
In fact, these can be converted directly. For example, if we want to convert the matrix I of uint16 to uint8, just I = uint8(I). The same is true for other data type conversions!
Example:
3. Geometric transformation and image registration
Common geometric transformations
1. imcrop crop image
Icropped = imcrop(I,rect) cropped image
clc;clear; close all;
I = imread('circuit.tif');
I2 = imcrop(I,[75 68 130 112]);
axis image;
subplot(1,2,1);
imshow(I);
title('Original Image');
subplot(1,2,2);
imshow(I2);
title('Cropped Image');
axis image;
result:
Note that the image coordinate system of matlab is:
Center cropped image using spatial reference rectangle
I = imread('parkavenue.jpg');
imshow(I)
Specify the target window size as a two-element vector of the form [ width , height ].
targetSize = [300 600];
Creates an object that specifies the spatial extent of the clipping window Rectangle
.
r = centerCropWindow2d(size(I),targetSize);
Crop the image at the limits of the spatial extent. Shows the cropped area.
J = imcrop(I,r);
imshow(J)
result:
2. imresize adjusts the image size
J = imresize(I,scale)
Returns an image J
that is I
scaled times the length and width of scale
. The input image I
can be a grayscale image, an RGB image, a binary image, or a categorical image.
Code:
clc;clear; close all;
I = imread('rice.png');
J = imresize(I, 0.5);
figure;
imshow(I);
title('Original Image');
figure;
imshow(J);
title('Resized Image');
result:
We all know that image scaling requires interpolation. We can choose the interpolation method in the imresize function:
We specify nearest neighbor interpolation in the following code (other code remains unchanged):
J = imresize(I, 0.5,'nearest');
J = imresize(I,[numrows numcols])
Returns an image J
with the number of rows and columns specified by the vector [numrows numcols] .
Code:
clc;clear; close all;
RGB = imread('peppers.png');
RGB2 = imresize(RGB, [128 128]);
figure;
imshow(RGB);
title('Original Image');
figure;
imshow(RGB2);
title('Resized Image');
result:
3. imroate rotated image
J = imrotate(I,angle)
Rotate the image I
counterclockwise around its center point angle
. To rotate the image clockwise, specify angle
a negative value for . imrotate
Make the output image J
large enough to contain the entire rotated image. By default, imrotate
nearest neighbor interpolation is used, setting J
the value of pixels outside the rotated image to 0
.
Code:
clc;clear; close all;
I = imread('peppers.png');
J = imrotate(I, 45, 'bilinear','loose');
figure(1);
imshow(I);
figure(2);
imshow(J);
result:
We change the bbox parameter to crop and observe whether the image sizes are consistent:
J = imrotate(I, 45, 'bilinear','crop');
You can see that the images are the same size after changing to 'crop'!
4. Image pyramid impyramid
B = impyramid(A,direction)
computes a Gaussian pyramid reduction or expansion of A
by one level. direction
determines whether impyramid
performs a reduction or an expansion.
Code:
clc;clear; close all;
I = imread('cameraman.tif');
I1 = impyramid(I, 'reduce');
I2 = impyramid(I1, 'reduce');
I3 = impyramid(I2, 'reduce');
figure, imshow(I);
figure, imshow(I1);
figure, imshow(I2);
figure, imshow(I3);
result:
4. Image filtering and enhancement
1. Image filtering
1.1. fspecial creates a filter kernel and imfilter filters according to the selected filter kernel.
Note: Image filtering can also use the filter2 function. Please check the difference with imfilter using the help command.
Note that the imfilter mentioned above uses correlation instead of convolution by default. If you want to use convolution for filtering, you need to specify the 'conv' parameter!
Example:
Read the image and display:
I = imread('cameraman.tif');
imshow(I);
Create a motion filter and use it to blur the image. Displays the blurred image.
H = fspecial('motion',20,45);
MotionBlur = imfilter(I,H,'replicate');
imshow(MotionBlur);
Create a circular filter and use it to blur the image. Displays the blurred image.
H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate');
imshow(blurred);
When executing imfilter, you should pay attention to converting the image to uint8 type to avoid negative values in the filter kernel causing negative numbers in the output:
Here we need to distinguish it from the operation between our images! When we want to add two images and divide by 2 to take the average, we often convert the two images to double first to prevent the truncation of uint8! Here, uint8 must be used deliberately to truncate, pay attention to the details!
1.2. imgaussfilt image two-dimensional Gaussian filtering
Note that the filter kernel size is not given here. After checking the source code, the filter kernel size defaults to 3*3 and sigma defaults to 0.5. If you want to specify the filter kernel size yourself, give it in the third key-value pair:
Example: B = imgaussfilt(A,'FilterSize',3);
Code demo:
clc;clear; close all;
I = imread('cameraman.tif');
Iblur = imgaussfilt(I,2, 'FilterSize',7); % sigma = 2, kernel_size = 5*5
montage({I,Iblur});
title('Original Image (Left) Vs. Gaussian Filtered Image (Right)');
2. Contrast adjustment
Similarly, we choose commonly used functions for demonstration!
2.1. imadjust adjusts the image intensity value
Adjust the contrast of grayscale images
clc;clear; close all;
I = imread('pout.tif');
J = imadjust(I);
montage({I,J});
Adjust the contrast of a grayscale image, specifying contrast limits
clc;clear; close all;
I = imread('pout.tif');
K = imadjust(I,[0.3 0.7],[]); %low_out和high_out未给出时默认等于low_in,high_in
montage({I,K});
Adjust the contrast of color images
clc;clear; close all;
RGB = imread('football.jpg');
RGB2 = imadjust(RGB,[.2 .3 0; .6 .7 1],[]);%对应彩色图像三通道的low_in,high_in
montage({RGB,RGB2});
2.2. imsharpen image sharpening
Image sharpening
clc;clear; close all;
a = imread('hestain.png');
imshow(a)
title('Original Image');
b = imsharpen(a);
figure, imshow(b)
title('Sharpened Image');
Control the amount of sharpening on borders
clc;clear; close all;
a = imread('rice.png');
imshow(a), title('Original Image');
b = imsharpen(a,'Radius',2,'Amount',1);
figure, imshow(b)
title('Sharpened Image');
2.3. Histeq histogram equalization was mentioned above, so I won’t go into details here.
2.4. imnoise adds noise to the image
clc;clear; close all;
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
montage({I,J});
title('Original Image(left) and Noise Image(right)');
3. Morphological operations
3.1. imerode corrosion
Among them SE:
original_img = imread('Fig0905(a)(wirebond-mask).tif');
subplot(1,4,1);
imshow(original_img);
%三次不同大小的腐蚀操作比较
SE = strel('rectangle',[11 11]);
out = imerode(original_img, SE);
subplot(1,4,2);
imshow(out);
SE = strel('rectangle',[15 15]);
out = imerode(original_img, SE);
subplot(1,4,3);
imshow(out);
SE = strel('rectangle',[45 45]);
out = imerode(original_img, SE);
subplot(1,4,4);
imshow(out);
Note that each corrosion will cause the white circle in the middle to become smaller, which is not obvious here!
Let's use nhood to demonstrate the corrosion operation again:
original_img = imread('Fig0905(a)(wirebond-mask).tif');
subplot(1,2,1);
imshow(original_img);
nhood = ones(45);
out = imerode(original_img, nhood);
subplot(1,2,2);
imshow(out);
3.2. imdilate expansion
It’s almost the same as the corrosion parameters!
original_img = imread('Fig0907(a)(text_gaps_1_and_2_pixels).tif');
imshow(original_img);
nhood = [0 1 0;1 1 1;0 1 0];
out = imdilate(original_img, nhood);
figure();
imshow(out);
Observe the effect after zooming in:
3.3. imopen operation
Similar to the syntax of corrosion and expansion
3.4. imclose closing operation
The following example shows the opening operation first and then the closing operation result.
original_img = imread('Fig0911(a)(noisy_fingerprint).tif');
subplot(1,3,1);
imshow(original_img);
nhood = ones(3);
out = imopen(original_img, nhood);
subplot(1,3,2);
imshow(out);
out = imclose(out, nhood);
subplot(1,3,3);
imshow(out);
4. Processing based on area of concern (to be updated)
5. Image arithmetic
5.1、imabsdiff
clc;clear; close all;
I = imread('cameraman.tif');
J = uint8(filter2(fspecial('gaussian'), I));
K = imabsdiff(I,J);
figure;
imshow(K,[]);%将K中像素最小值设为黑色,最大值设为白色
5.2、imadd
Add two images. Specify the output as uint16
a type to avoid truncating the results.
clc;clear; close all;
I = imread('rice.png');
J = imread('cameraman.tif');
K = imadd(I,J,'uint16');
imshow(K,[]);
5.3、imsubtract
Subtract an image from another image or subtract a constant from an image
Subtract image background
clc;clear; close all;
I = imread('rice.png');
%估计背景
background = imopen(I,strel('disk',15));
%从图像中减去背景
J = imsubtract(I,background);
montage({I,J});
5. Fourier Transform
1. fft2 implements fast Fourier transform of images
Code demo:
clc;clear;close all;
im = imread('street.jfif');
figure(1);
imshow(im);
im = rgb2gray(im);
out = fft2(im);
figure(2);
imshow(abs(out),[]); %取实部
You may have questions, why is there nothing black in the Fourier transform? In fact, if we enlarge the upper left corner of Figure 2, it actually looks like this:
So the fact is that only the upper left corner with the larger value looks white, and that's our DC component. In fact, the reason for this phenomenon is that if fft2 is used directly, the origin of the spectrum is not in the center of the image but in the upper left corner.
If we want to move the origin to the center, we need to use the fftshift function:
2. fftshift moves the spectrum origin to the center
Let's add to the above code:
figure(3);
out = fftshift(out);
imshow(abs(out),[]);
That’s it!
3. ifft2 performs inverse Fourier transform
Next, we perform low-pass filtering in the frequency domain and then inverse Fourier transform to view the results.
clc;clear;close all;
im = imread('Fig0638(a)(lenna_RGB).tif');
figure(1);
im = rgb2gray(im);%转灰度
imshow(im);
title('原图');
[M N] = size(im);
im_f = fft2(im);
lp = fspecial('gaussian',25,2);%高斯低通滤波器(空间域)(补零到与原图一样大小)
lp_f = fft2(lp,M,N);%高斯低通滤波器(频域)
figure(2);
surf(fftshift(abs(lp_f)),'edgecolor','none');%surf函数画曲面图
title('滤波核频域曲面图');
out_f = im_f.*lp_f; %频域相乘
out = ifft2(out_f);%空间域结果
figure(3);
imshow(abs(out),[]);
title('结果');
Here our filter kernel is used. Of course, we can also customize a frequency domain low-pass filter:
%D(u,v)<=D0时有值
u = 0:Q-1
v = 0:P-1
%calculate D(u,v)
H = double(D<=D0)
References:
Image Processing Toolbox of MATLAB official documentation