天天看點

MATLAB--數字圖像處理--圖像的幾何變換

圖像的幾何變換

圖像的幾何變換是将一幅圖像中的坐标映射到另外一幅圖像中的新坐标位置,它不改變圖像的像素值,隻是改變像素所在的幾何位置,使原始圖像按照需要産生位置、形狀和大小的變換。

1、圖像的平移

在MATLAB中,沒有提供具體圖像平移函數,直接運用MATLAB指令程式設計即可實作圖像的平移操作。

MATLAB實作

示例:

構造圖像平移函數

function [output_image] = move(input_image,m,n)
%定義move函數,input_image為輸入圖像,m為輸入圖像沿着水準方向移動的距離,n為輸入圖像沿着垂直方向移動的距離,output_image為平移後的輸出圖像
%不考慮圖像平移以後的溢出情況,找不到對應點的地方都指派為1
[M,N,G]=size(input_image);    %擷取輸入圖像的大小
double_image=im2double(input_image);   %将資料圖像類型轉換成雙精度
new_image=ones(M,N,G);    %初始化新圖像矩陣全為1,大小與輸入圖像相同
for i=1:M
    for j=1:N
        if((i+m)>=1&&(i+m)<=M&&(j+n)>=1&&(j+n)<=N)      %判斷平移以後行列坐标是否超出範圍
            new_image(i+m,j+n,:)=double_image(i,j,:);    %進行圖像平移
        end
    end
end
output_image=new_image;
           

調用函數,實作圖像平移

clear all; close all; clc;
input=imread('G:\picture\Fig0450(a)(woman_original).tif');
output1=move(input,100,100);
output2=move(input,-100,-100);
subplot(1,3,1); imshow(input); title('原圖像');
subplot(1,3,2); imshow(output1); title('右下平移後的圖像');
subplot(1,3,3); imshow(output2); title('左上平移後的圖像');
           

運作結果:

MATLAB--數字圖像處理--圖像的幾何變換

2、圖像的鏡像

圖像的鏡像分為垂直鏡像和水準鏡像。

MATLAB實作

示例:

構造圖像鏡像函數

function [outImage] = mirror(InImage,n)
%定義mirror函數實作圖像鏡像變換
%參數n為1時,實作水準鏡像變換
%參數n為2時,實作垂直鏡像變換
%參數n為3時,實作水準垂直鏡像變換
[M,N,G]=size(InImage); %擷取輸入圖像InImage的大小
transImage=InImage;
if n==1
    for i=1:M
        for j=1:N
            transImage(i,M+1-j,:)=InImage(i,j,:);
        end
    end
elseif n==2
        for i=1:M
            for j=1:N
                transImage(M+1-i,j,:)=InImage(i,j,:);
            end
        end
elseif n==3
        for i=1:M
            for j=1:N
                transImage(M+1-i,M+1-j,:)=InImage(i,j,:);
            end
        end
else
    error('參數n輸入不正确,n取1、2、3');
end
outImage=transImage;
           

調用函數,實作圖像鏡像

clear all; close all; clc;
input=imread('G:\picture\Fig0450(a)(woman_original).tif');
output1=mirror(input,1);
output2=mirror(input,2);
output3=mirror(input,3);
subplot(2,2,1); imshow(input); title('原圖像');
subplot(2,2,2); imshow(output1); title('水準鏡像');
subplot(2,2,3); imshow(output2); title('垂直鏡像');
subplot(2,2,4); imshow(output3); title('水準垂直鏡像');
           

運作結果:

MATLAB--數字圖像處理--圖像的幾何變換

3、圖像的縮放

圖像的縮放是指将給定的圖像在x軸方向按比例縮放fx倍,在y軸方向按比例縮放fy倍,進而獲得一幅新的圖像。如果fx=fy,即在x軸方向和y軸方向縮放的比例相同,即為圖像的全比例縮放。如果fx≠fy,圖像比例縮放會改變原始圖像像素間的相對位置,産生幾何畸變。

–imresize()函數

MATLAB實作

示例:

clear all; close all; clc;
InImage=imread('G:\picture\Fig0450(a)(woman_original).tif');
shrink1=imresize(InImage,0.5);  %設定縮放比例,縮小圖像
enlarge1=imresize(InImage,2);  %設定縮放比例,擴大圖像
shrink2=imresize(InImage,[500 600]);  %設定縮放後的圖像行列,實作縮放圖像并顯示
shrink3=imresize(InImage,[NaN 600]);  %函數按照輸入圖像縱橫比生成行數,實作縮放圖像并顯示
enlarge2=imresize(InImage,1.5,'bilinear');  %采用雙線性插值法對圖像進行縮放
enlarge3=imresize(InImage,1.5,'triangle');  %采用三角型核函數插值對圖像進行縮放

subplot(2,4,1); imshow(InImage); title('原圖像');
subplot(2,4,2); imshow(shrink1); title('shrink1');
subplot(2,4,3); imshow(enlarge1); title('enlarge1');
subplot(2,4,4); imshow(shrink2); title('shrink2');
subplot(2,4,5); imshow(shrink3); title('shrink3');
subplot(2,4,6); imshow(enlarge2); title('enlarge2');
subplot(2,4,7); imshow(enlarge3); title('enlarge3');
           

運算結果:

MATLAB--數字圖像處理--圖像的幾何變換
MATLAB--數字圖像處理--圖像的幾何變換

4、圖像的轉置

圖像轉置即為圖像的行列坐标互換,進行圖像轉置後,圖像的大小會發生變化。

MATLAB實作

示例:

function [outputImage] = transposition(inputImage)
% inputImage為輸入圖像
% outputImage為對輸入圖像轉置的輸出圖像
[M,N,G]=size(inputImage);
inputImage=im2double(inputImage);
transp=ones(N,M,G);
for i=1:M
    for j=1:N
        transp(j,i,:)=inputImage(i,j,:);
    end
end
outputImage=transp;
end
           

運作結果:

MATLAB--數字圖像處理--圖像的幾何變換

5、圖像的旋轉

圖像的旋轉變換屬于圖像的位置變換,通常是以圖像的中心為原點,将圖像上的所有像素都旋轉一個相同的角度。旋轉後,圖像的大小一般會改變。

–imrotate()函數

MATLAB實作

示例:

clear all; close all; clc;
InImage=imread('G:\picture\Fig0227(a)(washington_infrared).tif');
rotate1=imrotate(InImage,20); %将圖像以其中心為原點逆時針旋轉20°,采用最近鄰插值生成完整旋轉圖像
rotate2=imrotate(InImage,-20); %将圖像以其中心為原點順時針旋轉20°,采用最近鄰插值生成完整旋轉圖像
rotate3=imrotate(InImage,20,'bilinear'); %将圖像以其中心為原點逆時針旋轉20°,采用雙線性插值生成完整旋轉圖像
rotate4=imrotate(InImage,20,'bilinear','crop'); %将圖像以其中心為原點逆時針旋轉20°,采用最近鄰插值,對旋轉後圖像裁剪保證輸出圖像與輸入圖像大小相等
rotate5=imrotate(InImage,20,'bilinear','loose'); %将圖像以其中心為原點逆時針旋轉20°,采用最近鄰插值生成完整旋轉圖像

subplot(2,3,1); imshow(InImage); 
subplot(2,3,2); imshow(rotate1); 
subplot(2,3,3); imshow(rotate2);
subplot(2,3,4); imshow(rotate3);
subplot(2,3,5); imshow(rotate4);
subplot(2,3,6); imshow(rotate5); 
           

運作結果:

MATLAB--數字圖像處理--圖像的幾何變換
MATLAB--數字圖像處理--圖像的幾何變換

6、圖像的剪切

–imcrop()函數

MATLAB實作

示例:

clear all; close all; clc;
InImage=imread('G:\picture\Fig0227(a)(washington_infrared).tif');
rect=[445 625 130 200]; %定義剪切區域
X=imcrop(InImage,rect); %進行圖像剪切
subplot(1,2,1); imshow(InImage); title('原圖像');
rectangle('Position',rect,'LineWidth',2,'EdgeColor','r'); %将圖像的剪切區域标出
subplot(1,2,2); imshow(X); title('剪切圖像');
           

運作結果:

MATLAB--數字圖像處理--圖像的幾何變換

7、圖像的空間變換

在MATLAB的圖像處理工具箱中提供了一個專門的函數 intransform(),可以通過定義參數實作多種類型的空間變換,包括仿射變換(如平移、縮放、旋轉、剪切)、投影變換等。

繼續閱讀