MATLAB图像处理 (一)

最近在做关于计算机视觉图像处理的相关东西。总结一下知识点。

首先关于图像的知识,图像可以分为黑白照片或者彩色照片。每幅照片都是由一个个像素点构成。如果是彩色图像,则每幅图片可以分为RGB三通道分别表示。由于现在我们处理的几乎全是彩色图像,所以我们主要考虑彩色图像,暂不讨论黑白图像。我们拿下面这幅图像为例。顺便看看这个图像的属性。

一、读取文件提取RGB三通道

MATLAB读取文件可以分为读取图片和视频。我们这里是读取图片,等到读取视频的时候再总结视频。

读取图片文件使用imread(),里面填的是图片的名称或者是图片的地址。

 1 %读取图片
 2 image = imread('1.jpg');
 3 figure;
 4 subplot(2,2,1)
 5 imshow(image);
 6 title('原始图像');
 7  %提取图片的RGB三通道
 8 image_double = double(image);
 9 R = image_double(:,:,1);
10 G = image_double(:,:,2);
11 B = image_double(:,:,3);
12 %分别显示RGB三通道的图片信息
13 subplot(2,2,2);
14 imshow(uint8(R));
15 title('红色通道');
16 subplot(2,2,3);
17 imshow(uint8(G));
18 title('绿色通道');
19 subplot(2,2,4);
20 imshow(uint8(B));
21 title('蓝色通道');
View Code

这里直接使用一行代码就可以读取图片信息了。

可以在工作区看到原始图像image的信息。427*640*3,分别表示像素为高*宽为427*640,与图像信息相符合。3表示图片为RGB彩色图片。红绿蓝。原始图片的类型是uint8,所以将三个通道信息提取出来后为double类型,然后生成图像时在使用uint8格式转换。

二、RGB三通道合成原始图像

现在我们获得了RGB三个成分分量,现在我们将三个分量在合成为原始图像。

1 %RGb合成原始图像
2 mix(:,:,1) = R;
3 mix(:,:,2) = G;
4 mix(:,:,3) = B;
5 figure ;
6 imshow(uint8(mix));
7 title('合成图像');
View Code

 

 

全部代码如下:

代码一(直接提取RGB):

%读取图片
image = imread('1.jpg');
figure;
subplot(2,2,1)
imshow(image);
title('原始图像');
R1 = image(:,:,1);
G1 = image(:,:,2);
B1 = image(:,:,2);
%提取图片的RGB三通道
% image_double = double(image);
% R = image_double(:,:,1);
% G = image_double(:,:,2);
% B = image_double(:,:,3);
%分别显示RGB三通道的图片信息
subplot(2,2,2);
imshow(uint8(R1));
title('红色通道');
subplot(2,2,3);
imshow(uint8(G1));
title('绿色通道');
subplot(2,2,4);
imshow(uint8(B1));
title('蓝色通道');

%RGb合成原始图像
mix(:,:,1) = R1;
mix(:,:,2) = G1;
mix(:,:,3) = B1;
figure ;
imshow(uint8(mix));
title('合成图像');
View Code

 

代码二:

 1 %读取图片
 2 image = imread('1.jpg');
 3 figure;
 4 subplot(2,2,1)
 5 imshow(image);
 6 title('原始图像');
 7  %提取图片的RGB三通道
 8 image_double = double(image);
 9 R = image_double(:,:,1);
10 G = image_double(:,:,2);
11 B = image_double(:,:,3);
12 %分别显示RGB三通道的图片信息
13 subplot(2,2,2);
14 imshow(uint8(R));
15 title('红色通道');
16 subplot(2,2,3);
17 imshow(uint8(G));
18 title('绿色通道');
19 subplot(2,2,4);
20 imshow(uint8(B));
21 title('蓝色通道');
22 
23 %RGb合成原始图像
24 mix(:,:,1) = R;
25 mix(:,:,2) = G;
26 mix(:,:,3) = B;
27 figure ;
28 imshow(uint8(mix));
29 title('合成图像');
View Code

 

posted @ 2020-09-19 21:00  wangheq  阅读(788)  评论(0编辑  收藏  举报