影像处理
1 Introduction to digital image
1.1 Digital images and Its Acquisition
- An image is an artifact that depicts or records visual perception.
- Typically acquired by using charge-coupled device (电荷耦合元件,CCD) or complementary metal-oxide-semiconductor(互补金属氧化物半导体,CMOS) devices.
1.2 Types of digital images
- True color or RGB Image(彩色图片)
Each pixel has a particular color described by the amount of red, green and blue in it - Greyscale Image(灰度图片)
Each pixel is a shade of gray, normally from 0(black) to 255 (white) - Binary Image(黑白图片,二元图片)
Each pixel is just black or white
1.2.1 Typical RGB Image
1.3 Elements of images
1.3.1 Binary Image
每个像素只有0和1。
1.3.2 Greyscale Image
如果是8位图像的话,每一个像素点就是0~255。
数字越大:越亮;数字越小:越暗。
1.3.3 Color Image
2 Read and show images
2.1 Read and show an image
- Read an image:
imread()
- Show an image:
imshow()
- Example:
clear;clc;close all;
I=imread('pout.tif');
imshow(I);
2.2 Image Viewer
作用:得到每个像素点的信息
语法:imtool('pout.tif')
效果:
3 Image arithmetic(影像的四则运算)
指令 | 描述 | 具体用法 |
---|---|---|
imabsdiff | Absolute difference of two images | |
imadd (+) | Add two images or add constant to image | |
imapplymatrix | Linear combination of color channels | |
imcomplement | Complement image(求补集) | |
imdivide (\) | Divide one image into another or divide image by constant | |
imlincomb | Linear combination of images | |
immultiply (*) | Multiply two images or multiply image by constant | |
imsubtract (-) | Subtract one image from another or subtract constant from image | 放链接 |
3.1 Image Multiplication
语法:immultiply(图像名, 倍数)
示例:(让图片变亮)
I=imread('rice.png');
subplot(1,2,1);imshow(I);
J=immultiply(I,1.5);
subplot(1,2,2);imshow(J);
程序运行结果:
3.2 Image Addition
语法:imadd(图像名1,图像名2)
程序示例:
clc;clear;
I=imread('rice.png');
J=imread('cameraman.tif');
K=imadd(I,J);
subplot(1,3,1);imshow(I);
subplot(1,3,2);imshow(J);
subplot(1,3,3);imshow(K);
程序结果:
注意点:
- 相加的两幅图片必须大小相等(同型矩阵才能相加)
- 两幅图片相加之后整体变亮,因为相加之后灰度值变大,变量,而有的地方超过255就直接变白了,即饱和了。
3.3 Image Histogram(直方图)
语法:imhist(图像名)
把所有像素的灰度值统计出来,做成直方图。
示例:
clc;clear;close all;
I=imread('pout.tif');
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imhist(I);
程序运行结果:
3.4 Histogram Equalization(均值处理)
语法:histeq(图像矩阵名)
作用:提供图片的对比度。
程序示例:
clc;clear;close all;
I=imread('pout.tif');
subplot(2,2,1);imshow(I);
subplot(2,2,2);imhist(I);
I2=histeq(I); %均值处理
subplot(2,2,3);imshow(I2);
subplot(2,2,4);imhist(I2);
运行结果:
由运行结果可以看出,经过均值处理之后,图片的对比度明显提高,灰度值的分布也更加均匀。
颜色深得变深,浅的变浅
4 Geometric Transformation(几何变换)
图像的四则运算只要是改变每个像素点的灰度值,而几何变换是进行对图像的旋转平移等操作。即改变每个像素点(pixel)的位置。
4.1 Geometric Transformation Matrixes(2D)
Transform | Example | Transformation Matrix | Function |
---|---|---|---|
Translation(平移) | ![]() | [ x ′ y ′ 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] [ x y 1 ] \begin{bmatrix}x' \\y'\\ 1 \end{bmatrix}= \begin{bmatrix}1 & 0 & t_x \\0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix}x \\y\\ 1\end{bmatrix} ⎣⎡x′y′1⎦⎤=⎣⎡100010txty1⎦⎤⎣⎡xy1⎦⎤ | |
Scale(放缩) | ![]() | [ S x 0 0 0 S y 0 0 0 1 ] \begin{bmatrix}S_x & 0 & 0 \\0 & S_y & 0 \\ 0 & 0 & 1 \end{bmatrix} ⎣⎡Sx000Sy0001⎦⎤ | imresize() |
Shear(剪切) | ![]() | [ 1 h x 0 h y 1 0 0 0 1 ] \begin{bmatrix}1 & h_x & 0 \\h_y & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} ⎣⎡1hy0hx10001⎦⎤ | |
Rotation with θ(clock-wise)(顺时针旋转) | ![]() | [ c o s θ s i n θ 0 − s i n θ c o s θ 0 0 0 1 ] \begin{bmatrix}cos\theta& sin\theta & 0 \\-sin\theta & cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} ⎣⎡cosθ−sinθ0sinθcosθ0001⎦⎤ | imrotate() |
4.2 Image Rotation
语法:imrotate(图像矩阵名称,旋转角度,'method')
利用指定的插值方法method进行图像A的旋转,method是文本字符串,需要引号引出,method包括:
- ‘nearest’最临近插值-默认值
- 'bilinear’双线性的
- 'bicubic’双三次的
程序示例:
clc;clear;close all;
I=imread('rice.png');
subplot(1,2,1);imshow(I);
I2=imrotate(I,35,'bilinear');
subplot(1,2,2);imshow(I2);
运行结果:
注意:
不难发现,旋转之后的图像所占的空间更大。
5 Write Images(写入/存储图像)
语法:imwrite(图像矩阵名,'存储名.格式')
支持的格式有:.bmp
, .gif
, .hdf
, .jpg
, .jpeg
, .jp2
, .jpx
,.pcx
, .pnm
, .ppm
, .ras
, .tif
, .tiff
, .xwd
---------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------
以下部分以解决一个实际问题为导向,进行图像处理的学习
问题:数米粒
数出图中有几颗米粒?
6 Image thresholding(灰阶–>二值)
6.1 函数 graythresh( )
作用:这个函数可以帮助你找到一个最合适的threshold。
语法:返回值(level)=graythresh(图像矩阵名)
6.2 函数 im2bw( )
作用:把图像转换成为二值图像。
语法:新图像名=im2bw(图像矩阵名,level)
程序示例:
clc;clear;close all;
I=imread('rice.png');
subplot(1,2,1);imshow(I);
level=graythresh(I);
I2=im2bw(I,level);
subplot(1,2,2);imshow(I2);
运行结果:
7 Background estimation
问题导向:如上处理结果,不难发现,在图上的上半部分有部分噪点(sparkle),而在下半部分也存在一些米粒不见了
分析原因:因为灰度图的背景,在上半部分偏亮,导致有些小点在转化为二值图时超过了门槛,变成了白色;而在背景的下半部分偏暗,所以导致有的米粒没有达到门槛灰度值,变成了黑色。
解决方法:为了解决此问题,需要我们提取背景,然后删除背景带来的影响。
7.1 Background Estimation
语法:背景图像名=imopen(图像名,SE)
SE可以是单个结构元素对象或者结构元素对象数组。
这里涉及matlab的开闭运算,不作详细说明。
程序示例:
clc;clear;close all;
I=imread('rice.png');
subplot(1,2,1);imshow(I);
BG=imopen(I,strel('disk',15)); %创建一个平坦圆形结构,半径为15,然后进行开运算。
subplot(1,2,2);imshow(BG);
运行结果:
如上所述,已经提取出图片的背景,下一步就是把背景从图片中剔除。
7.2 Background Subtraction
影响的四则运算,前面有汇总放链接!
语法:新图像=imsubtract(被减图A,减图B)
%A-B
程序示例:
clc;clear;close all;
I=imread('rice.png');
subplot(1,3,1);imshow(I);
BG=imopen(I,strel('disk',15));
subplot(1,3,2);imshow(BG);
NewRice=imsubtract(I,BG);
subplot(1,3,3);imshow(NewRice);
程序结果:
7.3 Thresholding on Background Removed Image
相当于把之前写过的代码累加起来。
程序示例:
clc;clear;close all;
%没有背景处理
I=imread('rice.png');
subplot(1,3,1);imshow(I);
level=graythresh(I);
I2=im2bw(I,level);
subplot(1,3,2);imshow(I2);
%有背景处理
BG=imopen(I,strel('disk',15));
NewRice=imsubtract(I,BG);
level=graythresh(NewRice);
I3=im2bw(NewRice,level);
subplot(1,3,3);imshow(I3);
运行结果:
对比不难发现,第二幅图中少米粒以及由噪点的情况都消失了。
8 Connected-component labelling
原理:扫描所有的像素点,如果相邻的两个像素点都是白色,则两个像素点就会拥有同样的标签。
8.1 函数 bwlabel()
语法:[L,num] = bwlabel(BW,n)
- L-返回一个和BW相同的矩阵,同一个连通区域的元素值相等
- num-返回连通区域的个数
- BW-被处理的图像
- n-值为4或8,表示是按4连通寻找区域,还是8连通寻找,默认为8。
- 8连通:是说一个像素,如果和其他像素在上、下、左、右、左上角、左下角、右上角或右下角连接着,则认为他们是联通的;
- 4连通:如果像素的位置在其他像素相邻的上、下、左或右,则认为他们是连接着的,连通的,在左上角、左下角、右上角或右下角连接,则不认为他们连通。
程序示例:
clc;clear;close all;
I=imread('rice.png');
BG=imopen(I,strel('disk',15));
NewRice=imsubtract(I,BG);
level=graythresh(NewRice);
I3=im2bw(NewRice,level);
[L,num]=bwlabel(I3,8);
运行结果:
由上图可以看出,原图是256*256的图像,得到的矩阵L也是256*256,同一块区域都被标上了相同的编号,把这块区域圈起来,就是一个米粒的形状,输出num值,可以得到整个图上有99个米粒。
8.2 Color coding objects
作用:给每一个标签一个颜色
语法:RGB = label2rgb(L)
输入:L——标记矩阵(可由labelmatrix, bwlabel, bwlabeln, watershed返回)
输出:RGB——彩色图像
注:根据L的数值对应,默认对应到colormap(jet)的色彩,返回RGB矩阵
程序示例:
clc;clear;close all;
I=imread('rice.png');
BG=imopen(I,strel('disk',15));
NewRice=imsubtract(I,BG);
level=graythresh(NewRice);
I3=im2bw(NewRice,level);
[L,num]=bwlabel(I3,8);
ColorL=label2rgb(L);
imshow(ColorL);
运行结果:
8.3 Object Properties
作用:对每一个connected component提供一系列的属性
语法:STATS = regionprops(L,'properties')
- 输入
L——标记矩阵
字符串——字符串为basic时,属性: ‘Area’, ‘Centroid’, 和 ‘BoundingBox’ 将被计算。 - 输出
STATS——是一个结构体数组,每个元素包含了相应的属性
程序示例:
clc;clear;close all;
I=imread('rice.png');
BG=imopen(I,strel('disk',15));
NewRice=imsubtract(I,BG);
level=graythresh(NewRice);
I3=im2bw(NewRice,level);
[L,num]=bwlabel(I3,8);
data=regionprops(L,'basic');
data(55) %以第55颗米为例
运行结果: