2 图像的基础性运算

2 图像的基础性运算

2021-12-02
个人学习记录,欢迎交流批评。
图像的基础运算包括:几何运算、代数运算、模板运算。

2.1 图像的几何运算

图像的几何运算是一种空间变换,主要在于确定变换前后图像中点与点之间的映射关系。
将点(x,y)变换到(x’,y’)可表示为:
( x ′ y ′ 1 ) = ( x y 1 ) [ a c p b d q e f s ] (3) (x' y' 1)=(x y 1)\left[ \begin{matrix} a& c& p \\ b& d& q \\ e& f& s \\ \end{matrix} \right] \tag{3} (xy1)=(xy1)abecdfpqs(3)
其中,
T = [ a c p b d q e f s ] (3) T=\left[ \begin{matrix} a& c& p \\ b& d& q \\ e& f& s \\ \end{matrix} \right] \tag{3} T=abecdfpqs(3)
为变换矩阵。

在介绍运算之前,了解图像的插值运算:
(1)最近邻插值:选取距离最近的像素的值作为插值点像素的值;
(2)双线性插值、三线性插值、其他插值方法。

1、图像平移

T = [ 1 0 0 0 1 0 a b 1 ] (3) T=\left[ \begin{matrix} 1& 0& 0 \\ 0& 1& 0 \\ a& b& 1\\ \end{matrix} \right] \tag{3} T=10a01b001(3)
图像延x轴平移a,沿y轴平移b。

Image1 = imread('C:\Users\Administrator\Desktop\timg\lotus.jpg');
[h,w,c] = size(Image1);
deltax = 20;
deltay = 20;
newh = h + abs(deltay);
neww = w + abs(deltax);
tform = affine2d([1 0 0; 0 1 0; deltax deltay 1]); % 定义平移变换
TF = isTranslation(tform)  % 判断是否为平移变换
[xLimitsOut,yLimitsOut] = outputLimits(tform,[1 w],[1 h]);
% 确定合适的输出坐标
if deltax < 0
	xLimitsOut(2) = xLimitsOut(2) + abs(deltax);
else
	xLimitsOut(1) = xLimitsOut(1) - abs(deltax);
end
if deltay < 0
	xLimitsOut(2) = xLimitsOut(2) + abs(deltay);
else
	xLimitsOut(1) = xLimitsOut(1) - abs(deltay);
end
% 不扩大尺寸输出图像对象
R1 = imref2d([h,w],[1 w],[1 h]);
% 扩大尺寸输出图像对象
R2 = imref2d([newh,neww],xLimitsOut,yLimitsOut);
NewImage1 = imwarp(Image1,tform,'FillValue',255,'OutputView',R1);
NewImage2 = imwarp(Image1,tform,'FillValue',255,'OutputView',R2);

figure,imshow(Image1);
figure,imshow(NewImage1);
figure,imshow(NewImage2);

2、图像镜像

水平镜像:
T = [ − 1 0 0 0 1 0 M − 1 0 1 ] (3) T=\left[ \begin{matrix} -1& 0& 0 \\ 0& 1& 0 \\ M-1& 0& 1\\ \end{matrix} \right] \tag{3} T=10M1010001(3)
垂直镜像:
T = [ 1 0 0 0 − 1 0 0 N − 1 1 ] (3) T=\left[ \begin{matrix} 1& 0& 0 \\ 0& -1& 0 \\ 0& N-1& 1\\ \end{matrix} \right] \tag{3} T=10001N1001(3)
对角镜像:
T = [ − 1 0 0 0 − 1 0 M − 1 N − 1 − 1 ] (3) T=\left[ \begin{matrix} -1& 0& 0 \\ 0& -1& 0 \\ M-1& N-1& -1\\ \end{matrix} \right] \tag{3} T=10M101N1001(3)

% 水平镜像
HImage = flipdim(Image1,2); %按列翻转
% 垂直镜像
VImage = flipdim(Image1,1);%按行翻转
% 对角镜像
CImage = flipdim(HImage,1);
NewImage = [Image1,HImage;VImage,CImage];
figure,imshow(NewImage);

在这里插入图片描述

3、图像旋转

若逆时针旋转θ角度,变换矩阵T为
T = [ c o s θ − s i n θ 0 s i n θ c o s θ 0 0 0 1 ] (3) T=\left[ \begin{matrix} cos\theta& -sin\theta& 0 \\ sin\theta& cos\theta& 0 \\ 0& 0& 1\\ \end{matrix} \right] \tag{3} T=cosθsinθ0sinθcosθ0001(3)

% 读取图像为double类型
Image1double = im2double(imread('C:\Users\Administrator\Desktop\timg\lotus.jpg'));
% 旋转不剪切,双线性插值
ImageRNC = imrotate(Image1double,15,'bilinear');
% 旋转剪切(剪切:输出图像与输入图像同样大小,对其剪切)
ImageRC = imrotate(Image1double,15,'bilinear','crop');
subplot(131),imshow(Image1double),title('original');
subplot(132),imshow(ImageRNC),title('RNC');
subplot(133),imshow(ImageRC),title('RC');

在这里插入图片描述

4、图像的缩放

图像如果没有按比例缩放,即kx≠ky会产生几何畸变,变换矩阵:
T = [ k x 0 0 0 k y 0 0 0 1 ] (3) T=\left[ \begin{matrix} kx& 0& 0 \\ 0& ky& 0 \\ 0& 0& 1\\ \end{matrix} \right] \tag{3} T=kx000ky0001(3)

ImageS = imresize(Image1,'Scale',[1.6,2.8]); % 不按比例放缩 
imshow(ImageS);

5、图像错切

图像错切是指平面景物在投影平面上的非垂直投影
T = [ 1 d y 0 d x 1 0 0 0 1 ] (3) T=\left[ \begin{matrix} 1& dy& 0 \\ dx& 1& 0 \\ 0& 0& 1\\ \end{matrix} \right] \tag{3} T=1dx0dy10001(3)

dx=0.5;
dy=0.8;
tform = affine2d([1 dy 0;dx 1 0;0 0 1]);
ImageV = imwarp(Image1,tform);
imshow(ImageV);

在这里插入图片描述

6、图像转置

图像转置是指图像的行列坐标互换。
T = [ 0 1 0 1 0 0 0 0 1 ] (3) T=\left[ \begin{matrix} 0& 1& 0 \\ 1& 0& 0 \\ 0& 0& 1\\ \end{matrix} \right] \tag{3} T=010100001(3)

tform = affine2d([0 1 0;1 0 0;0 0 1]);
ImageT = imwarp(Image1,tform);
imshow(ImageT);

2.2 图像的代数运算

代数运算主要包括加减乘除、与或非等运算。
需要是两幅大小相等的图像

1、加法和减法运算

图像对应点相加减;
加法:实现图像叠加(随机噪声可通过多次叠加图像取平均值消除);减法:总图像-背景图=目标特征图。(可用于显示两幅图像的差异,取出不需要的图案,图像分割等)

% 加法
Image2 = imread('C:\Users\Administrator\Desktop\timg\cloud.jpg');
Imageadd = imadd(Image1,Image2);
imshow(Imageadd);
% 减法
Imagesub = imsubtract(Imageadd,Image1);
imshow(Imagesub);  % 不会得到Image2,超出取值范围的被截断

2、乘除法

对应点相乘除;
乘法主要用于图像与模板相乘实现局部特征的提取;
除法可以消除空间可变的量化敏感函数、归一化、产生比率图像等。

% 乘法
Imagemul = immultiply(Image1,Image2);
imshow(Imagemul);
% 除法
Imagediv = imdivide(Image1,Image2);
imshow(Imagediv);

3、逻辑运算

非运算: f ( x , y ) = 255 − f ( x , y ) f(x,y) = 255 - f(x,y) f(x,y)=255f(x,y) 获得原图像的反图像,称为反色;
与运算: g ( x , y ) = f 1 ( x , y ) & f 2 ( x , y ) g(x,y) = f1(x,y) \& f2(x,y) g(x,y)=f1(x,y)&f2(x,y) 求两幅图像的相交子图;
或运算: g ( x , y ) = f 1 ( x , y ) ∥ f 2 ( x , y ) g(x,y) = f1(x,y) \| f2(x,y) g(x,y)=f1(x,y)f2(x,y) 合并两幅图像的子图像;
注意:按位逻辑运算和逻辑运算(输出0-1)不同。

% 求反图像
ImageCom = imcomplement(Image1);
imshow(ImageCom);
% 按位逻辑运算
ImageBL = bitand(Image1,Image2);
imshow(ImageBL);
%ImageBL = bitcmp(Image1);
%ImageBL = bitor(Image1,Image2);
%ImageBL = bitxor(Image1,Image2);

在这里插入图片描述

% 逻辑运算符进行运算
ImageL = Image1 & Image2;
imshow(double(ImageL));   % 不转double会报错
%ImageL = ~Image1;
%ImageL = Image1 | Image2;
%ImageL = xor(Image1,Image2);

在这里插入图片描述

2.3 邻域及模板运算

1、邻域:以某像素点为中心,一般有4、8、24邻域。
2、模板:又叫滤波器、核、掩膜、窗口,用一个小的二维矩阵来表示,模板上的值称为加权系数。
3、模板运算:卷积或互相关运算,不断在图像上移动模板的位置,将模板中心对准图像的一个位置(x,y),然后让二者重合区域的对应元素一一相乘,后加和,成为新的(x,y)上的取值。
注意:由于边缘像素进行模板运算有不同的处理方法,如在外围补0或不变,或直接不算等,运算后的结果的形状会不同,要注意选择!

% 1/255*5的模板
H = [1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1];
H = H/25;
Image1gray = rgb2gray(Image1);
Image1graydouble = im2double(Image1gray);
% conv2进行补0
ImageConv = conv2(Image1graydouble,H,'same'); % 返回与原图像大小相同的中心部分 
imshow(ImageConv);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值