
空间域图像处理直接对图像像素进行操作,主要包括两类核心技术:
核心函数包括:灰度变换(imadjust、histeq)、噪声添加(imnoise)、滤波(medfilt2、filter2、fspecial)、可视化(imhist、subplot、sgtitle)等。
读取彩色图像并转换为灰度图,自定义线性变换函数 J = a*I + b(a 控制对比度,b 控制亮度),对比原图、灰度图及变换后图像的效果。
%% 1. 读取图像
imagePath = 'ALi.jpg';
colorImage = imread(imagePath); % 读取彩色图像
%% 2. 转换为灰度图
grayImage = rgb2gray(colorImage);
%% 3. 获取图像尺寸
[rows, cols] = size(grayImage);
fprintf('图像尺寸:%d行 x %d列\n', rows, cols);
%% 4. 定义灰度线性变换函数
a = 1.5; % 斜率,增大对比度(a>1增强,a<1减弱)
b = -100; % 截距,调整亮度(b>0变亮,b<0变暗)
% 转换为双精度避免整数运算溢出,应用线性变换
transformedImage = a * double(grayImage) + b;
% 像素值截断到[0,255]范围,转换回uint8类型
transformedImage(transformedImage < 0) = 0;
transformedImage(transformedImage > 255) = 255;
transformedImage = uint8(transformedImage);
%% 5. 可视化结果
figure('Name', '图像灰度变换结果', 'Position', [100, 100, 1000, 400]);
subplot(1, 3, 1); imshow(colorImage); title('原图', 'FontSize', 12); axis on;
subplot(1, 3, 2); imshow(grayImage); title('灰度图', 'FontSize', 12); axis on;
subplot(1, 3, 3); imshow(transformedImage); title('灰度变换后图像', 'FontSize', 12); axis on;
sgtitle('图像灰度化及线性变换实验结果', 'FontSize', 14);
通过设置不同 Gamma 值(γ>1 和 γ<1),实现图像幂次变换,对比图像明暗变化效果。
%% 1. 读取彩色图像
imagePath = 'aali.jpg';
colorImage = imread(imagePath);
%% 2. 转换为灰度图
grayImage = rgb2gray(colorImage);
%% 3. 归一化处理(避免运算溢出)
grayDouble = double(grayImage) / 255; % 转换为double并归一到[0,1]
%% 4. 定义Gamma值并执行幂次变换
gammaA = 3.5; % γ>1,图像变暗(压缩亮部、拉伸暗部)
gammaB = 0.3; % γ<1,图像变亮(拉伸亮部、压缩暗部)
gammaImageA = grayDouble .^ gammaA; % 幂次变换:s = r^γ
gammaImageB = grayDouble .^ gammaB;
%% 5. 转换回uint8类型以便显示
gammaImageA_uint8 = uint8(gammaImageA * 255);
gammaImageB_uint8 = uint8(gammaImageB * 255);
%% 6. 可视化结果
figure('Name', 'Gamma变换结果对比', 'Position', [100, 100, 1200, 500]);
subplot(1, 4, 1); imshow(colorImage); title('原图', 'FontSize', 12); axis on;
subplot(1, 4, 2); imshow(grayImage); title('灰度图', 'FontSize', 12); axis on;
subplot(1, 4, 3); imshow(gammaImageA_uint8); title(['Gamma=', num2str(gammaA)], 'FontSize', 12); axis on;
subplot(1, 4, 4); imshow(gammaImageB_uint8); title(['Gamma=', num2str(gammaB)], 'FontSize', 12); axis on;
sgtitle('不同Gamma值的幂次变换结果对比', 'FontSize', 14);
实现对数变换 s = log(1 + r),增强图像暗部细节,对比变换前后效果。
%% 1. 读取彩色图像
imagePath = 'aali2.jpg';
colorImage = imread(imagePath);
%% 2. 转换为灰度图
grayImage = rgb2gray(colorImage);
%% 3. 转换为double类型
grayDouble = double(grayImage);
%% 4. 执行对数变换
logTransformed = log(1 + grayDouble); % 1避免log(0)无意义
%% 5. 归一化与类型转换
normalized = mat2gray(logTransformed); % 归一到[0,1]
logImage = im2uint8(normalized); % 转回uint8
%% 6. 可视化结果
figure('Name', '对数变换结果', 'Position', [100, 100, 1200, 400]);
subplot(1, 3, 1); imshow(colorImage); title('原图', 'FontSize', 12); axis on;
subplot(1, 3, 2); imshow(grayImage); title('灰度图', 'FontSize', 12); axis on;
subplot(1, 3, 3); imshow(logImage); title('对数变换后图像', 'FontSize', 12); axis on;
sgtitle('图像对数变换结果对比', 'FontSize', 14);
mat2gray归一化到 [0,1],否则图像会过暗或失真。 读取两张图像,转换为灰度图后计算并显示直方图,通过histeq函数实现直方图均衡化,对比均衡化前后的灰度分布与图像效果。
%% 1. 读取两张彩色图像
imagePath1 = 'ALi2.jpg';
imagePath2 = 'ALi.jpg';
colorImage1 = imread(imagePath1);
colorImage2 = imread(imagePath2);
%% 2. 转换为灰度图
grayImage1 = rgb2gray(colorImage1);
grayImage2 = rgb2gray(colorImage2);
%% 3. 直方图均衡化
eqImage1 = histeq(grayImage1); % 第一张图均衡化
eqImage2 = histeq(grayImage2); % 第二张图均衡化
%% 4. 可视化结果(原图、灰度图、直方图、均衡化图)
figure('Name', '直方图及均衡化结果', 'Position', [100, 100, 1400, 1000]);
% 显示原图与灰度图
subplot(4, 2, 1); imshow(colorImage1); title('原图1', 'FontSize', 12); axis on;
subplot(4, 2, 2); imshow(grayImage1); title('灰度图1', 'FontSize', 12); axis on;
subplot(4, 2, 3); imshow(colorImage2); title('原图2', 'FontSize', 12); axis on;
subplot(4, 2, 4); imshow(grayImage2); title('灰度图2', 'FontSize', 12); axis on;
% 显示直方图
subplot(4, 2, 5); imhist(grayImage1); title('灰度直方图1', 'FontSize', 12); xlabel('灰度级'); ylabel('像素数量');
subplot(4, 2, 6); imhist(grayImage2); title('灰度直方图2', 'FontSize', 12); xlabel('灰度级'); ylabel('像素数量');
% 显示均衡化结果
subplot(4, 2, 7); imshow(eqImage1); title('直方图均衡化1', 'FontSize', 12); axis on;
subplot(4, 2, 8); imshow(eqImage2); title('直方图均衡化2', 'FontSize', 12); axis on;
sgtitle('图像直方图及均衡化实验结果', 'FontSize', 14);
为灰度图添加椒盐噪声,使用 3×3、5×5、7×7 三种尺寸模板进行中值滤波,对比去噪效果。
%% 1. 读取图像并转换为灰度图
imagePath = 'ALi2.jpg';
colorImage = imread(imagePath);
grayImage = rgb2gray(colorImage);
%% 2. 添加椒盐噪声
noisyImage = imnoise(grayImage, 'salt & pepper', 0.04); % 噪声密度4%
%% 3. 不同尺寸模板中值滤波
filtered3 = medfilt2(noisyImage, [3, 3]); % 3×3模板
filtered5 = medfilt2(noisyImage, [5, 5]); % 5×5模板
filtered7 = medfilt2(noisyImage, [7, 7]); % 7×7模板
%% 4. 可视化结果
figure('Name', '椒盐噪声与中值滤波结果', 'Position', [100, 100, 1500, 400]);
subplot(1, 5, 1); imshow(colorImage); title('原图', 'FontSize', 12); axis on;
subplot(1, 5, 2); imshow(grayImage); title('灰度图', 'FontSize', 12); axis on;
subplot(1, 5, 3); imshow(noisyImage); title('添加椒盐噪声', 'FontSize', 12); axis on;
subplot(1, 5, 4); imshow(filtered3); title('3×3模板中值滤波', 'FontSize', 12); axis on;
subplot(1, 5, 5); imshow(filtered7); title('7×7模板中值滤波', 'FontSize', 12); axis on;
sgtitle('椒盐噪声及不同尺寸模板中值滤波效果对比', 'FontSize', 14);
将灰度图转换为二值图,分别使用 Sobel 算子和 Laplacian 算子进行锐化,增强图像边缘细节。
%% 1. 读取图像并转换为灰度图
color_image = imread('aali2.jpg');
gray_image = rgb2gray(color_image);
%% 2. 转换为二值图像
threshold_level = 0.5;
binary_image = im2bw(gray_image, threshold_level); % 阈值0.5二值化
%% 3. 算子锐化
% Sobel算子(水平边缘检测为主,增强边缘对比度)
H_sobel = fspecial('sobel');
sobel_sharpened = filter2(H_sobel, binary_image); % 卷积运算
% Laplacian算子(检测所有方向边缘,增强细节)
H_laplacian = fspecial('laplacian', 0.2); % 0.2为平滑系数
laplacian_sharpened = filter2(H_laplacian, binary_image);
%% 4. 可视化结果
figure('Name', '图像锐化结果对比', 'NumberTitle', 'off');
subplot(1, 4, 1); imshow(color_image); title('原图'); axis on;
subplot(1, 4, 2); imshow(binary_image); title('二值图像'); axis on;
subplot(1, 4, 3); imshow(sobel_sharpened, []); title('sobel算子锐化'); axis on;
subplot(1, 4, 4); imshow(laplacian_sharpened, []); title('拉普拉斯算子锐化'); axis on;
sgtitle('Sobel 与 Laplacian 算子锐化效果对比');
imshow(sharpened, [])自动缩放显示范围,避免图像失真。处理类型 | 关键技术 | 核心函数 | 应用场景 |
|---|---|---|---|
灰度变换 | 线性变换 | double、uint8 | 调整对比度、亮度 |
Gamma 变换 | ^(幂运算) | 明暗自适应调整 | |
对数变换 | log、mat2gray | 增强暗部细节 | |
直方图处理 | 均衡化 | imhist、histeq | 提升图像对比度 |
平滑滤波 | 中值滤波 | imnoise、medfilt2 | 去除椒盐噪声 |
锐化滤波 | Sobel 算子 | fspecial、filter2 | 边缘增强(水平 / 垂直) |
Laplacian 算子 | fspecial、filter2 | 全方向细节增强 |
mat2gray归一化或imadjust调整灰度范围;通过本次实验,系统掌握了空间域图像处理的核心方法,理解了灰度变换、直方图均衡化、滤波等技术的原理与应用场景,为后续复杂图像处理(如边缘检测、图像分割)奠定了基础。