将椭圆形图像归一化为圆形图像

      在计算机视觉领域,很多图像中使用的Interested Detector得到的图像都是椭圆形的,为了能够方便提取图像特征,首先通常会将这个椭圆形的区域归一化为圆形区域,也可以理解为一个正方形区域。下面给出了这个功能的Matlab代码实现。

      getEllipseImage.m

function [ imageEllipse, imageCircle ] = getEllipseImage( image, A, center, R )
%GETELLIPSEIMAGE Summary of this function goes here
%   Detailed explanation goes here
% Input:
% image  原始图像 
%        The original image
% A      中心在原点的椭圆方程系数 x'Ax = 1
%        The coefficients of the ellipse equation centered at the origin 
% center 椭圆中心
%        The center of the ellipse
% R      归一化之后的圆半径
%        The radius after the ellipse is normalized
%
% Output:
% imageEllipse 生成的椭圆图像
%              The genderated ellipse image
% imageCircle  生成的归一化圆形图像
%              The generated circular image
%
% Author:
% Rao Cong

%% 计算由椭圆到圆形的平面变换矩阵
Transform = inv( sqrtm( A ) ) ./ R;

%% 生成椭圆图像
M = size( image, 1 );
N = size( image, 2 );

% 取出在椭圆区域内的点
pts = [];
for y = 1 : M
    for x = 1 : N
        pt = [ x; y ];
        ptRel = [ x; y ] - center;
        val = ptRel' * A * ptRel;
        if val <= 1
            pts = [ pts pt ]; %#ok
        end
    end
end

% 估算椭圆图像的大小
xmin = min( pts( 1, : ) );
ymin = min( pts( 2, : ) );
xmax = max( pts( 1, : ) );
ymax = max( pts( 2, : ) );

w = xmax - xmin + 1;
h = ymax - ymin + 1;

% 为椭圆图像的像素赋值
imageEllipse = zeros( h, w, size( image, 3 ), 'uint8' );
for i = 1 : length( pts )
    imageEllipse( pts( 2, i ) - ymin + 1, pts( 1, i ) - xmin + 1, : ) = image( pts( 2, i ), pts( 1, i ), : );
end

%% 生成圆形图像
normSize = 2 * R;
imageCircle = zeros( normSize, normSize, size( image, 3 ), 'uint8' );
for x = 1 : normSize
    for y = 1 : normSize
        % 计算出变换后的点在原始图像上的位置
        ptRet = Transform * ( [ x; y ] - [ normSize / 2; normSize / 2 ] );
        val = ptRet' * A * ptRet;
        if val <= 1
            pt = ptRet + center;
            imageCircle( y, x, : ) = interpolation( image, pt, 1 );
        end
        
    end
end

end

%% 像素插值函数
% Function for performing pixel value interpolation
function pixelVal = interpolation( image, pt, type )

H = size( image, 1 );
W = size( image, 2 );
pixelVal = zeros( 1, size( image, 3 ) );

switch type
    % 最邻近插值 Nearest neigbour interpolation
    case 0
        pt = round( pt );
        if pixelInImage( pt, W, H );
            pixelVal = image( pt( 2 ), pt( 1 ), : );
        else
            pixelVal = 0;
        end
    % 双线性插值 Bilinear interpolation
    case 1
        ptTL = floor( pt );
        ptBR = ceil( pt );
        ptTR = [ ceil( pt(1) ); floor( pt(2) ) ];
        ptBL = [ floor( pt(1) ); ceil( pt(2) ) ];
        if pixelInImage( ptTL, W, H ) && pixelInImage( ptBR, W, H ) && pixelInImage( ptTR, W, H ) && pixelInImage( ptBL, W, H );
            diff = pt - ptTL;
            Coeffs1 = [ ( 1 - diff(1) ), diff(1) ];
            Coeffs2 = [ ( 1 - diff(2) ); diff(2) ];
            for i = 1 : size( image, 3 )
                Pixels = [ image( ptTL( 2 ), ptTL( 1 ), i ) image( ptTR( 2 ), ptTR( 1 ), i ); image( ptBL( 2 ), ptBL( 1 ), i ) image( ptBR( 2 ), ptBR( 1 ), i ) ];
                pixelVal(i) = uint8( Coeffs1 * double(Pixels) * Coeffs2 );
            end
        else
            pixelVal = 0;
        end
end

end

%% 判断像素是否在图像中的函数
% Function for indicating whether a pixel is in some image
function [ boolIn ] = pixelInImage( pt, W, H )

boolIn =  pt(1) >= 1 && pt(1) <= W && pt(2) >= 1 && pt(1) <= H;

end

      TestScript.m

clc;
close all;
clear all;

image = imread( 'T.jpg' );

A = [ 0.0006 0.0002; 0.0002 0.0004 ];
M = size( image, 1 );
N = size( image, 2 );
EllipseCenter = [ N / 2; M / 2 ];
R = 100;

[ imageEllipse, imageCircle ] = getEllipseImage( image, A, EllipseCenter, R );

figure(1), imshow( image );
figure(2), imshow( imageEllipse );
figure(3), imshow( imageCircle );
      实验结果


你可能感兴趣的:(计算机视觉)