数字图像处理 - 图像的基本操作与图像变换

本文介绍了四个图像处理任务:1)使用矩阵运算实现彩色图像的逆时针旋转;2)通过图像差分分析物体(橙子)在动画中的运动方向;3)通过点除法改善光源不均的文档图像;4)运用特定公式将彩色图像转换为灰度图像。这些例子展示了基本的图像处理技术在实际问题中的应用。
摘要由CSDN通过智能技术生成

数字图像处理 - 图像的基本操作与图像变换

1.将彩色图像f以其图像中心为原点逆时针旋转angle角度后赋值给图像g。

function g = rgbRotate(f,angle)
[r,c,d] = size(f);%获得图像的层数,如果是rgb图像需要进行cat连接
%关于rgb的中心旋转
nH = round(r*abs(cosd(angle))+c*abs(sin(angle)));
nW = round(c*abs(cosd(angle))+r*abs(sind(angle)));
g = zeros(nH,nW,d);
M1 = [1 0 0;0 -1 0;-0.5*nW 0.5*nH 1];%做平移变化,即将坐标原点移到中心后,得到对应的位置
M2 = [cosd(angle) -sind(angle) 0;sind(angle) cosd(angle) 0;0 0 1];%做旋转变换后的位置
M3 = [1 0 0;0 -1 0;0.5*c 0.5*r 1];%再得到平移回来的位置
for i =1:nH
    for j=1:nW
        temp=[j i 1]*M1*M2*M3;
        x = temp(1,2);
        y = temp(1,1);
        y = round(y);
        x = round(x);
        if(x>=1 && x<=r && y>=1 && y<=c)
            g(i,j,:) = f(x,y,:);
        end
    end
end
end

效果图:
在这里插入图片描述

2.三幅图片是一段动画中的3个相邻的帧。编写程序将这些图像分别后一张减去前一张,通过结果分析图像中橙子的运动方向。

clear;
clc;
f1 = tofloat(imread('第4题素材\第1张图.jpg'));
f2 = tofloat(imread('第4题素材\第2张图.jpg'));
f3 = tofloat(imread('第4题素材\第3张图.jpg'));
g1 = (f2-f1);
g2 = (f3-f2);
g3 = (f3-f1);
subplot(2,3,1); imshow(f1); title('1');
subplot(2,3,2); imshow(f2); title('2');
subplot(2,3,3); imshow(f3); title('3');
subplot(2,3,4); imshow(g1); title('2-1');
subplot(2,3,5); imshow(g2); title('3-2');
subplot(2,3,6); imshow(g3); title('3-1');

效果图
在这里插入图片描述

3.document文件是光源不均下的文档图像,light文件是同一光源下的背景图像,编程将该文件夹中的document图像点除以light图像,并分析结果图像,说明为什么文档图像得到改善。

%利用除法改善图像,并说明为什么图像得到了改变
clear;
clc;
light = tofloat(imread('第5题素材\light.jpg'));
document = tofloat(imread('第5题素材\document.jpg'));
g = document ./ light;
subplot(2,3,1); imshow(document); title('document');
subplot(2,3,2); imshow(light); title('light');
subplot(2,3,3); imshow(g); title('document ./ light');
%通过直方图的显示来说明为什么图像得到了改善
subplot(2,3,4); imhist(document); title('document');
subplot(2,3,5); imhist(light); title('light');
subplot(2,3,6); imhist(g); title('document ./ light');

效果图:
在这里插入图片描述

4.编写程序将一幅彩色图像转换为一幅灰度图像并显示结果,对于灰度图像中的每个像素亮度按以下公式转换:

g r a y = 0.2125 R + 0.7154 G + 0.0721 B gray = 0.2125R+0.7154G+0.0721B gray=0.2125R+0.7154G+0.0721B

clear;
clc;
%展示原图
figure(1);
%彩色图像转灰度图像
f = imread("第1张图.jpg");
imshow(f);
R = f(:,:,1);
G = f(:,:,2);
B = f(:,:,3);
g = 0.2125*R+0.7154*G+0.0721*B;
figure(2);
imshow(g);

效果图:
在这里插入图片描述
5.编写函数实现一个椭圆相框函数M = ellipseMask(a, b),其中a和b分别是椭圆的长半轴和短半轴,该函数返回矩阵M,矩阵M位于椭圆内部的元素值为1,外部的元素值为0。读入一张图像作为椭圆的外切矩形,实现裁剪掉位于椭圆相框以外的图像部分。

clear;
clc;
f = imread("第1张图.jpg");

[r,c,p] = size(f);
M = ellipseMask(200,150,r,c);
M=repmat(M,[1 1 3]);
g = f;
g(M==0)=0;
imshow(g);

ellipseMask.m

function M = ellipseMask(a, b,r,c)
%返回一个具有MXN大小,a长半轴,b为短半轴
e = imshow(zeros(r,c));%获取图像的空白模板
%得到数据的大小
height=r;
width=c;
%得到中心值
cX=width/2;
cY=(height)/2;

a=imellipse(gca,[(1/2-a/width)*width, (1/2-b/height)*height,(2*a/width)*width,(2*b/height)*height]);
%得到逻辑矩阵
mymask=createMask(a,e);
M = uint8(mymask*1);
end

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Afraidlight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值