Image1 = im2double(imread('timg.bmp'));
figure(1), subplot(1, 2, 1), imshow(Image1), title('原图像');
gray = rgb2gray(Image1);
figure(1), subplot(1, 2, 2), imhist(gray);
rgb2gray()函数–rgb图像转灰度图像
imhist()函数–统计变量显示直方图
[h,w] = size(gray);
NewImage1 = zeros(h, w);
a=80/256; b=180/256; c=30/256; d=220/256;
for x=1:w
for y=1:h
if gray(y, x)
NewImage3 = zeros(h, w, 3);
for x=1:w
for y=1:h
if gray(y, x)<64/256
NewImage3(y, x, 1)=0;
NewImage3(y, x, 2)=4*gray(y, x);
NewImage3(y, x, 3)=1;
elseif gray(y, x)<128/256
NewImage3(y, x, 1)=0;
NewImage3(y, x, 2)=1;
NewImage3(y, x, 3)=2-4*gray(y, x);
elseif gray(y, x)<192/256
NewImage3(y, x, 1)=4*gray(y, x)-2;
NewImage3(y, x, 2)=1;
NewImage3(y, x, 3)=0;
else
NewImage3(y, x, 1)=1;
NewImage3(y, x, 2)=4-4*gray(y, x);
NewImage3(y, x, 3)=0;
end
end
end
figure(2), subplot(2, 2, 3), imshow(NewImage3), title('伪彩色增强图像');
% 添加噪声
noiseIsp = imnoise(gray, 'salt & pepper', 0.1);
noiseIg = imnoise(gray, 'gaussian');
figure(3), subplot(2, 2, 1), imshow(noiseIsp), title('椒盐噪声');
figure(3), subplot(2, 2, 3), imshow(noiseIg), title('高斯噪声');
% 中值滤波
result1 = medfilt2(noiseIsp);
result2 = medfilt2(noiseIg);
figure(3), subplot(2, 2, 2), imshow(result1), title('椒盐噪声--中值滤波');
figure(3), subplot(2, 2, 4), imshow(result2), title('高斯噪声--中值滤波');
imnoise()函数–按指定类型在图像上添加噪声
medfilt2()函数–中值滤波
H1 = [-1 -2 -1;0 0 0;1 2 1];
H2 = [-1 0 1;-2 0 2;-1 0 1];
R1 = imfilter(gray,H1);
R2 = imfilter(gray,H2);
edgeImage = abs(R1)+abs(R2);
sharpImage = gray+edgeImage;
figure(4), subplot(1, 2, 1), imshow(edgeImage), title('Sobel梯度图像');
figure(4), subplot(1, 2, 2), imshow(sharpImage), title('Sobel锐化图像');
edge()函数–对灰度或二值图像I进行边缘检测,检测后图像为二值图像。除了sobel算子还有canny算子、prewitt算子、log算子等。
Image1 = im2double(imread('timg.bmp'));
subplot(1, 3, 1), imshow(Image1), title('原图像');
gray = rgb2gray(Image1);
NewImage1 = imadjust(Image1, [0 1], [0.3 0.7]);
subplot(1, 3, 2), imshow(NewImage1), title('灰度变换');
NewImage2 = log(Image1+1);
subplot(1, 3, 3), imshow(NewImage2, []), title('灰度对数变换');