图像旋转_matlab实现

function img_behind = im_rotate_2(img_front,degree,typename)
    %函数说明:图像旋转
    %输入参数:旋转前图像img_front,旋转角度degree(顺时针),图片类型('gray'or'color')typename
    %输出参数:旋转后图像img_behind
    if strcmp(typename,'gray')
        size_front = size(img_front);
        degree = degree*pi/180;
        M = size(img_front,1);
        N = size(img_front,2);
        diag_longth = sqrt(M^2+N^2)/2;
        alpha = atan(M/N);
        beta = atan(N/M);
        if abs(degree) > pi/2
            degree_mid = pi - abs(degree);
        else
            degree_mid = abs(degree);
        end
        size_behind = round([2*diag_longth*sin(alpha+degree_mid) 2*diag_longth*sin(beta+degree_mid)]);
        img_behind = zeros(size_behind);
        rotation_matrix = [cos(degree) sin(degree);-sin(degree) cos(degree)];
        point_front = round(size_front'/2);
        point_behind = round(size_behind'/2);
        for x = 1:size_behind(1)
            for y = 1:size_behind(2)
                coord_mid = rotation_matrix*([x;y]-point_behind);
                coord_end = coord_mid+point_front;
                x0 = round(coord_end(1));
                y0 = round(coord_end(2));
                if (x0 <= size_front(1) && x0 >= 1)&&(y0 <= size_front(2) && y0 >= 1 )
                    img_behind(x,y) = img_front(x0,y0);
                end
            end
        end
    else if strcmp(typename,'color')
            img_behind_1 = im_rotate_2(img_front(:,:,1),degree,'gray');
            img_behind_2 = im_rotate_2(img_front(:,:,2),degree,'gray');
            img_behind_3 = im_rotate_2(img_front(:,:,3),degree,'gray');
            img_behind = zeros(size(img_behind_1));
            img_behind(:,:,1) = img_behind_1;
            img_behind(:,:,2) = img_behind_2;
            img_behind(:,:,3) = img_behind_3;
        end
    end
end
            

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值