2.图像点操作。自选一张图片,完成以下图像处理:①选择合适的阈值对图像进行二值化处理,对比变化前后的图像;②编程实现对某一区域灰度值加或减一定数值的操作,对比变化前后的图像。
图像二值化(阈值)处理:
·经过阈值处理后的图像变成了一幅黑白二值图.
双固定阈值法:
, 其中
为指定的阈值.
窗口灰度变换处理:
·灰度窗口变换的变换函数表达式如下:
, 式中
表示窗口的下限,
表示窗口的上限.
分段线性变换:
·灰度窗口变换的变换函数表达式如下:
2.1 图像二值化(阈值)处理
2.1.1 整幅图片二值化处理
%图像二值化处理
%用底层算法实现
image = imread('Durant.png');
image_gray = rgb2gray(image);
%
image_matrix = image_gray(:,:,1);
[height, width, channels] = size(image_gray);
G = zeros(height, width);
%
for i = 1 : height
for j = 1: width
if(image_matrix(i, j) > 128)%阈值选为128
G(i, j) = 255;
else
G(i, j) = 0;
end
end
end
%
G = uint8(G);
%
subplot(1, 2, 1);
imshow(image_gray);
title('原图的灰度图');
%
subplot(1, 2, 2);
imshow(G);
title('二值化后的灰度图');
(阈值选为128)
(阈值选为64)
2.1.2 图片中某块区域二值化处理
%图像二值化处理(对图像的某一区域)
%用底层算法实现
image = imread('Durant.png');
image_gray = rgb2gray(image);
%
image_matrix = image_gray(:,:,1);
[height, width, channels] = size(image_gray);
G = zeros(height, width);
%
for i = 1 : height
for j = 1: width
if(i >= 1 && i <= 250 && j >= 100 && j <= 300) %对某一区域进行二值化处理
if(image_matrix(i, j) > 128)%阈值选为128
G(i, j) = 255;
else
G(i, j) = 0;
end
else
G(i, j) = image_matrix(i, j);
end
end
end
%
G = uint8(G);
%
subplot(1, 2, 1);
imshow(image_gray);
title('原图的灰度图');
%
subplot(1, 2, 2);
imshow(G);
title('二值化后的灰度图');