Geometric transformation of image (matlab)

1 Introduction:

The geometric transformation of an image is to map the coordinates in one image to the new coordinate position in another image. It does not change the pixel value of the image, but only changes the geometric position of the pixel, so that the original image can generate position, shape and Transformation of size.

2. Image translation:

All points on a picture are moved along the X axis in the horizontal direction and along the Y axis in the vertical direction according to the given offset, and the size after translation is the same.

The imtranslate function translates the image

>> I = imread('E:\persional\matlab\images\Fig0809(a).bmp');
se = imtranslate(I, [50 140]);%将一个平面结构化元素分别向下和向右移动
subplot(121);imshow(I), title('原图')
subplot(122), imshow(se), title('移动后的图像');

The imdilate dilation function translates the image

I = imread('E:\persional\matlab\images\Fig0809(a).bmp');
se = translate(strel(1), [50 140]);%将一个平面结构化元素分别向下和向右移动
J = imdilate(I,se);%利用膨胀函数平移图像
subplot(121);imshow(I), title('原图')
subplot(122), imshow(J), title('移动后的图像');

insert image description here

Image filter:

Image mirroring is divided into two types: vertical mirroring and horizontal mirroring
Horizontal mirroring: (x0, y0) ------ x1 = M - x0 y1 = y0;
Vertical and horizontal: (x0, y0) ------ x1 = x0 y1 = M - y0;

Z = flipdim():

close all;clear all;clc; 
I=imread('E:\persional\matlab\images\Fig0809(a).bmp'); 
J1=flipdim(I,1);
J2=flipdim(I,2);
J3=flipdim(I,3);
set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置
//set(0,'defaultFigureColor',[1 1 1])%修改图形背景颜色的设置
figure,
subplot(1,2,1),imshow(I) ;
subplot(1,2,2),imshow(J1);
figure,
subplot(1,2,1),imshow(J2);
subplot(1,2,2),imshow(J3);

insert image description here
insert image description here

image scaling

Image scaling means that a given image is scaled by fx in the X-axis direction and fy in the Y-axis direction. Image scaling will change the relative position between pixels of the original image, resulting in geometric distortion.
1.B = imresize(A,m): m>1, zoom in; 0<m<1, zoom out;
2.B = imresize(A,[mrows,ncols]): [mrows,ncols] shows the scaled row and column, if the bit is NaN, the function will generate the number of ncols or mrows according to the aspect ratio of the input image A;
3.[B,newmap] = (A,map,m): m is scaled proportionally; it can also be [mrows, ncols];
4.[…] = imresize(…,method): method optional:
type of interpolation method: 'nearest' nearest neighbor interpolation; 'bilinear' bilinear interpolation, 'bicubic' bicubic interpolation
kernel function : 'box' Box kernel function, 'triangle' triangle kernel function ('bicubic' same), 'Cubic' cube kernel function ('bicubic same'), 'lanczos2' Lanczos-2 kernel function and 'lanczos3' Lanczos- 3 kernel functions.
5.[…] = imreadsize(…,method): By setting the value of paramter, control the scaling characteristics of the image, as shown in the following figure:
insert image description here

close all;clear all;clc;
[X,map] = imread('E:\persional\matlab\images\Fig0809(a).bmp');
J1 = imresize(X,0.25);
J2 = imresize(X,3.5);
J3 = imresize(X,[64,40]);%放缩后的图像行列比例
J4 = imresize(X,[64,NaN]);
J5 = imresize(X,1.6,'bilinear');%设置插值法
J6 = imresize(X,1.6,'triangle');
figure,
subplot(221),imshow(I);
subplot(222),imshow(J1);
subplot(223),imshow(I1)
subplot(224),imshow(J2)

insert image description here

Transpose of the image:

The transposition of the image is the exchange of row and column coordinates, for example: the new coordinate point (x1, y1) after conversion of the point (x0, y0) mathematical expression: x1 = x0; y1 = y0; after image transposition, the size will change Change:

function J = transp(I)
[M,N,G] = size(I);
I = im2double(I);
J = ones(N,M,G);
for i = 1:M
    for j = 1:N
        J(j,i,:) = I(i,j,:);
    end
end
%调用函数
close all;clear all;clc;
I=imread('E:\persional\matlab\images\Fig0809(a).bmp'); 					
J1=transp(I);										
set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置
set(0,'defaultFigureColor',[1 1 1])%修改图形背景颜色的设置
figure,
subplot(221),imshow(I);
subplot(222),imshow(J1);

insert image description here

Image rotation:

The rotation transformation of an image belongs to the position transformation of an image, usually taking the center of the image as the origin, and rotating all pixels on the image by the same angle. After rotation, the size of the image generally changes.

B = imrotate(A,angle,method,bbox) — angle: angle; method: 'nearest' nearest neighbor interpolation; 'bilinear' bilinear interpolation, 'bicubic' bicubic interpolation; bbox: return image size ('crop ','loose')

>> I=imread('E:\persional\matlab\images\Fig0809(a).bmp');
>> J1 = imrotate(I,30);
>> J2 = imrotate(I,-30);
>> J3 = imrotate(I,30,'bicubic','crop');
>> J4 = imrotate(I,-30,'bicubic','loose');
>> figure,
>> subplot(221),imshow(J1);
>> subplot(222),imshow(J2);
>> subplot(223),imshow(J3);
>> subplot(224),imshow(J4);

insert image description here

Image cropping:

Crops the image part of interest;
X1 = imcrop(I,[xmin,ymin,width,height])
[X1,map] = (…)
X1 = imcrop(I,map)
[X1,Y1,[ xmin,ymin,width,height]] = imcrop(...)

>>[A,map]=imread('E:\persional\matlab\images\Fig0809(a).bmp');
>> rect = [75 68 130 112];
>> X1 = imcrop(A,rect);
>> figure,
>> subplot,imshow(121),imshow(X1);

>>[A,rect] = imcrop(A);%选择性剪切
>>subplot,imshow(121),imshow(A);

insert image description here

Transformation of the image:

Various types of spatial transformations (translation, scaling, rotation, shearing) and projection transformations can be customized.

B = imtransform(A,TFORM,INTERP)

TFORM: returned by maketform();
INTERP: 'nearest' nearest neighbor interpolation; 'bilinear' bilinear interpolation, 'bicubic' bicubic interpolation.

Let the original image f(x,y) and the transformed image g(x',y')
(x',y') = T(x,y)
x' = a0x+a1x+a2;
y' = b0y+b1y +b2;
Write it as a 3x3 matrix for structure design;

insert image description here

>> [A,map]=imread('E:\persional\matlab\images\Fig0809(a).bmp');
>> Ta = maketform('affine',[cosd(30),-sind(30),0;sind(30),cosd(30) 0;0 0 1])%旋转结构体
>> Ia = imtransform(I,Ta);
>> Tb = maketform('affine',[5 0 0;0 10.5 0;0 0 1]);%平移结构体
>> Ib = imtransform(I,Tb);
>> xform = [0.5 0 0;0 0.5 0;0 0 1];
>> Tc = maketform('affine',xform);%缩放结构体
>>> Ic = imtransform(I,Tc,'XData',[1 (size(I,2)+xform(3,1))],'YData',[1 size(I,1)+xform(3,2)],'FillValues',255);
>> Td = maketform('affine',[1 4 0;2 1 0;0 0 1]);%剪切结构体
>> Id = imtransform(I,Td,'FillValues',255);
>>> figure,
>> subplot(221),imshow(Ia),axis on;
>> subplot(222),imshow(Ib),axis on;
>> subplot(223),imshow(Ic),axis on;
>> subplot(224),imshow(Id),axis on;

insert image description here

Guess you like

Origin blog.csdn.net/weixin_56260304/article/details/127196785