工具: MATLAB;
动机:使用 MATLAB toolbox 程序设计相对简单,通过初步学习一些图像处理的常见技术,帮助建立这方面的技能体系;
面向对象:有过经验,但是一头雾水的我。
1 图像、视频文件读取
clear all; close all; clc;
% Read an image
A = imread('lena.jpg');
% Display the read image
figure, imshow(A);
% Print height, width and number of channels of the read image
height = size(A, 1);
width = size(A, 2);
number_of_channels = size(A, 3);
% Resize the image to 2x and display
B = imresize(A, 2.0);
figure, imshow(B);
% Rotate the image 逆时针旋转
C = imrotate(A, 90);
figure, imshow(C);
关键点:imread,figure(imshow),size获取高度,宽度,图像通道数的参数,图像缩放,旋转。
clear all; close all; clc;
% Read an image
A = imread('lena.jpg');
% Translate the image
A_trans = imtranslate(A, [5 15]);
% Write the transformed image to disk
imwrite(A_trans, 'newlena.jpg');
figure,imshow(A_trans);
关键:imwrite及其调用前做的工作。
1-1 视频读取(选看)
clear all; close all; clc;
% Initialize an object for reading video
videoObj = vision.VideoFileReader('video1.mp4', 'ImageColorSpace', 'RGB');
% Get video frame size and frame rate
S = info(videoObj);
width = S.VideoSize(1);
height = S.VideoSize(end);
frame_rate = S.VideoFrameRate;
% Get individual video frames
image_data = step(videoObj);
% Run the loop until all frames have been displayed
while ~isDone(videoObj)
% Display video frame one by one
imshow(image_data);
image_data = step(videoObj);
end
% Release the object for reading video
release(videoObj);
关键:step,其流程reader读取生成对象,得到信息,逐帧显示 , PS:视频教程中有另外一种实现。
3 图像格式
3.1 RGB、灰度图片
clear all; close all; clc;
% Read a RGB image
A = imread('lena.jpg');
% Verify number of channels
number_of_channels = size(A, 3)
% Convert RGB image to Grayscale
A_gray = rgb2gray(A);
number_of_channels2 = size(A_gray, 3)
figure, subplot(1, 2, 1), imshow(A), title('Input RGB image');
subplot(1, 2, 2), imshow(A_gray), title('Converted Grayscale image');
关键:灰度图像颜色通道位1,rgb图像颜色通道数是3,subplot 显示多个图像,便于查看。
clear all; close all; clc;
% Read a RGB image
A = imread('lena.jpg');
% Extract individual R, G & B channels from RGB image
R = A(:, :, 1);
G = A(:, :, 2);
B = A(:, :, 3);
% Convert RGB image to HSV color space
A_hsv = rgb2hsv(A);
% Extract individual H, S & V channels from HSV image
H = A_hsv(:, :, 1);
S = A_hsv(:, :, 2);
V = A_hsv(:, :, 3);
% Display the read RGB image and HSV converted image
figure, subplot(1, 2, 1), imshow(A), title('Read RGB image');
subplot(1, 2, 2), imshow(A_hsv), title('Converted HSV image');
% Displaying individual image channels.
figure, subplot(1, 3, 1), imshow(S), title('Saturation channel');
subplot(1, 3, 2), imshow(H), title('Hue channel');
subplot(1, 3, 3), imshow(V), title('Value channel');
关键:R、G、B、H、S、V 都是提取空间域(矩阵表示)的信息