Matlab image translation, rotation, scaling, cropping

%%----------------------Matlab image translation, rotation, scaling, cropping -------------- -----------------

%-------------------head File---------------------------- -

clc ; % clear screen

clear ; % delete all variables

close all ; % close all open pictures

%-------------------image translation imtranslate-------------------------

A = imread('1.jpg') ;

subplot( 1, 3, 1 ) ;

imshow( A ) ;

title('Original image') ;

%Translate 135 pixels down and 125 pixels to the right (the direction of movement is opposite when the value is negative)

B = imtranslate( A, [ 135, 125 ] ) ;

subplot( 1, 3, 2 ) ;

imshow( B ) ;

title('Figure 1') ;

% By default, imtranslate displays the transformed image within the bounds (or limits) of the original image. This causes some shifted images to be clipped. Setting the 'OutputView' parameter to 'full' prevents the panned image from being clipped.

C = imtranslate( A, [ 135, 125 ], 'OutputView', 'full' ) ;

subplot( 1, 3, 3 ) ;

imshow( C ) ;

title('Figure 2') ;

Effect comparison chart

%----------------------image rotation imrotate------------------------ ----

X = imread('3.jpg');

subplot( 1, 3, 1 ) ;

imshow( X ) ;

title('Original image') ;

Y1 = imrotate( X, 30 ) ; %rotate the image 30° counterclockwise (the rotation direction is opposite when the value is negative)

subplot( 1, 3, 2 ) ;

imshow( Y1 ) ;

title('Figure 1') ;

Y2 = imrotate( X, 30, 'crop' ) ; %rotate 30°, and crop the image so that the resulting image is the same size as the original image

subplot( 1, 3, 3 ) ;

imshow( Y2 ) ;

title('Figure 2') ;

Effect comparison chart

%-------------------image scaling imresize--------------------------- ---

M = imread('2.jpg');

figure ; % The zoom of the image cannot be split and displayed in a window by using the graphics window, so use figure to create a new window

imshow( M ) ;

title('Original image') ;

N1 = imresize( M, 2 ) ; % enlarge the image by 2 times

figure ;

imshow( N1 ) ;

title('Figure 1') ;

N2 = imresize( M, 0.5 ) ; % enlarge the image by 0.5 times

figure ;

imshow( N2 ) ;

title('Figure 2') ;

Effect comparison chart

%---------------------------image cropping imcrop------------------- -

P = imread('4.jpg');

% Set the clipping area area, the four values ​​represent the coordinates of the upper left vertex of the clipping area [50,20], width 150, height 100

%图像本身以左上角顶点为原点向下向右建立坐标系

area = [50, 20 ,150, 100] ;

subplot( 1, 2, 1 ) ;

imshow( P ) ;

title( '原图' ) ;

rectangle('Position', area, 'LineWidth', 2, 'EdgeColor', 'r') %用rectangle创建矩形,显示剪切区域

Q = imcrop( P, area ) ; %裁剪

subplot( 1, 2, 2 ) ;

imshow( Q ) ;

title( '图1' ) ;

%如不加参数area,将使用鼠标在原图像上划矩形框的方法来实现图像的剪裁,双击确定后所划矩形框内的图像将在新窗口中输出

R = imcrop( P ) ;

figure;

imshow( R ) ;

title( '自由裁剪' ) ;

效果对比图

%-------------------图像翻转 flip------------------

H = imread('5.jpg');

subplot( 1, 3, 1 ) ;

imshow( H ) ;

title( '原图' ) ;

T1 = flip( H , 1 ) ; %参数1表示上下翻转

subplot( 1, 3, 2 ) ;

imshow( T1 ) ;

title( '上下翻转' ) ;

T2 = flip( H , 2 ) ; %参数2表示左右翻转

subplot( 1, 3, 3) ;

imshow( T2 ) ;

title( '左右翻转' ) ;

效果对比图

Guess you like

Origin blog.csdn.net/starryskyzl/article/details/129087189