Matlab学习1-图像处理灰度

图像处理,灰度处理
1、图像的缩小
效果如下:
在这里插入图片描述
test.m代码如下:

%imresize改变图像大小

j=imread('img.jpg');%读取图片
img1=imresize(j,[256,256]);
subplot(2,2,1),imshow(img1),xlabel('a图,256*256,256灰度级');

img2=imresize(img1,0.5,'nearest');%图像处理,参数(图片,放大缩小,方法)
subplot(2,2,2),imshow(img2),xlabel('b图,128*128,256灰度级');%摆放图像位置

img3=imresize(img2,0.5,'nearest');
subplot(2,2,3),imshow(img3),xlabel('c图,64*64,256灰度级');

img4=imresize(img3,0.5,'nearest');
subplot(2,2,4),imshow(img4),xlabel('d图,32*32,256灰度级');

 imwrite(img1, 't1a.png');
 imwrite(img2, 't1b.png');
 imwrite(img3, 't1c.png');
 imwrite(img4, 't1d.png');

2、灰度图像的深度处理,
效果如下:
在这里插入图片描述

test.m代码如下:

J=imread('img.jpg');    % 2215*2956
J=rgb2gray(J);%将打开的图像转换为灰度图rgb2gray
Img=imresize(J,[256,256]);    % 256*256

imwrite(Img, 't3a.png', 'Bitdepth', 8);   % Bitdepth = 8
imwrite(Img, 't3b.png', 'Bitdepth', 4);   % Bitdepth = 4
imwrite(Img, 't3c.png', 'Bitdepth', 2);   % Bitdepth = 2

Black = find( im2bw(Img)==0 );
White = find( im2bw(Img)==1 );
Img(Black)=0;
Img(White)=170;

imwrite(Img, 't3d.png');   % Bitdepth = 8 ,但看起来 Bitdepth = 1

I1=imread('t3a.png');
I2=imread('t3b.png');
I3=imread('t3c.png');
I4=imread('t3d.png');
subplot(2,2,1),imshow(I1),xlabel('(a) 256×256,256级灰度');
subplot(2,2,2),imshow(I2),xlabel('(b) 256×256,16级灰度');
subplot(2,2,3),imshow(I3),xlabel('(c) 256×256,4级灰度');
subplot(2,2,4),imshow(I4),xlabel('(d) 256×256,2级灰度');

素材代码如下:
gitee:https://gitee.com/CYFreud/matlab/tree/master/image_processing

猜你喜欢

转载自blog.csdn.net/CHengYuP/article/details/123784664