# 毕业季，用MATLAB制作一款环形相册吧 !

2021-06-05 09:15:39其他编程人已围观

### 完整步骤

#### 1.图片准备及导入

``````path='.\album\';%文件夹路径
files=dir(fullfile(path,'*.jpg'));
picNum=size(files,1);

%遍历路径下每一幅图像
for i=1:picNum
fileName = strcat(path,files(i).name);
imgSet.(['p',num2str(i)])=img;
end
``````

#### 2.为每张图片制作遮罩层

``````for i=1:length(BlockNum)%圆环层数
blockNum=BlockNum(i);%每一层图片数量
Rrange=R(i,:);%每次一层半径范围
for j=1:blockNum
tempBoard=ones(2401,2401)==1;
tempBoard=tempBoard&(disMesh>Rrange(1))&(disMesh<Rrange(2));
tempBoard=tempBoard&(thetaMesh>((j-1)*2*pi/blockNum))&(thetaMesh<(j*2*pi/blockNum));
end
end
``````

• 离中心点半径处于[r,R]范围内
• 与x轴正半轴夹角处于[theta1,theta2]之间

``````[XMesh,YMesh]=meshgrid(-1200:1:1200,-1200:1:1200);
``````

``````disMesh=sqrt(XMesh.^2+YMesh.^2);
``````

theta角矩阵：

``````thetaMesh=atan2(YMesh,XMesh)+pi;
thetaMesh=thetaMesh(:,end:-1:1);
``````

#### 3.调整每张图大小

``````for i=1:length(BlockNum)%圆环层数
blockNum=BlockNum(i);%每一层图片数量
Rrange=R(i,:);%每一层半径范围
for j=1:blockNum
tempBoard=ones(2401,2401)==1;
tempBoard=tempBoard&(disMesh>Rrange(1))&(disMesh<Rrange(2));
tempBoard=tempBoard&(thetaMesh>((j-1)*2*pi/blockNum))&(thetaMesh<(j*2*pi/blockNum));
TrueX=find(sum(tempBoard,1)>0);
TrueY=find(sum(tempBoard,2)>0);
x1=YMesh(min(TrueX),min(TrueY));
y1=XMesh(min(TrueX),min(TrueY));
x2=YMesh(max(TrueX),max(TrueY));
y2=XMesh(max(TrueX),max(TrueY));
xdiff=x2-x1;
ydiff=y2-y1;

pic=imgSet.(['p',num2str(tempPic)]);
[rows,cols,~]=size(pic);
ratio=[ydiff+1,xdiff+1]./[rows,cols];
newsize=ceil([rows,cols].*max(ratio));
offset=floor((newsize-[ydiff+1,xdiff+1])./2);
pic=imresize(pic,newsize);
pic=pic((1:ydiff+1)+offset(1),(1:xdiff+1)+offset(2),:);
end
end
``````

#### 3.绘图及绘图参数详解

BlockNum=[7,11];%每层扇形数量
R=[300,670;%第一层半径范围
670,1090];%第二层半径范围
lineColor=[0.98,0.98,0.98];线颜色
lineWidth=2;%线粗细

``````tempPic=1;
for i=1:length(BlockNum)
blockNum=BlockNum(i);
for j=1:blockNum
tempPic=tempPic+1;
tempPic=mod(tempPic-1,picNum)+1;
end
end
``````

``````t=0:0.001:(2*pi+0.001);
for i=1:length(BlockNum)
blockNum=BlockNum(i);
Rrange=R(i,:);
for j=1:blockNum
plot(cos(j*2*pi/blockNum).*Rrange,sin(j*2*pi/blockNum).*Rrange,'Color',lineColor,'LineWidth',lineWidth)
end
plot(cos(t).*Rrange(1),sin(t).*Rrange(1),'Color',lineColor,'LineWidth',lineWidth)
plot(cos(t).*Rrange(2),sin(t).*Rrange(2),'Color',lineColor,'LineWidth',lineWidth)
end
``````

#### 4.完整代码

``````function ringAlbum
BlockNum=[7,11];
R=[300,670;
670,1090];
lineColor=[0.98,0.98,0.98];
lineWidth=2;

path='.\album\';%文件夹名称
files=dir(fullfile(path,'*.jpg'));
picNum=size(files,1);

%遍历路径下每一幅图像
for i=1:picNum
fileName = strcat(path,files(i).name);
imgSet.(['p',num2str(i)])=img;
end

fig=figure('units','pixels',...
'position',[20 60 560 560],...
'Color',[1 1 1]);
ax=axes('Units','pixels',...
'parent',fig,...
'Color',[1 1 1],...
'Position',[0 0 560,560],...
'XLim',[-1200,1200],...
'YLim',[-1200,1200],...
'XColor','none',...
'YColor','none');
hold(ax,'on')
ax.YDir='reverse';
ax.XDir='normal';

[XMesh,YMesh]=meshgrid(-1200:1:1200,-1200:1:1200);
disMesh=sqrt(XMesh.^2+YMesh.^2);
thetaMesh=atan2(YMesh,XMesh)+pi;
thetaMesh=thetaMesh(:,end:-1:1);

tempPic=1;
t=0:0.001:(2*pi+0.001);
for i=1:length(BlockNum)
blockNum=BlockNum(i);
Rrange=R(i,:);
for j=1:blockNum
tempBoard=ones(2401,2401)==1;
tempBoard=tempBoard&(disMesh>Rrange(1))&(disMesh<Rrange(2));
tempBoard=tempBoard&(thetaMesh>((j-1)*2*pi/blockNum))&(thetaMesh<(j*2*pi/blockNum));
TrueX=find(sum(tempBoard,1)>0);
TrueY=find(sum(tempBoard,2)>0);
x1=YMesh(min(TrueX),min(TrueY));
y1=XMesh(min(TrueX),min(TrueY));
x2=YMesh(max(TrueX),max(TrueY));
y2=XMesh(max(TrueX),max(TrueY));
xdiff=x2-x1;
ydiff=y2-y1;

pic=imgSet.(['p',num2str(tempPic)]);
[rows,cols,~]=size(pic);
ratio=[ydiff+1,xdiff+1]./[rows,cols];
newsize=ceil([rows,cols].*max(ratio));
offset=floor((newsize-[ydiff+1,xdiff+1])./2);
pic=imresize(pic,newsize);
pic=pic((1:ydiff+1)+offset(1),(1:xdiff+1)+offset(2),:);

tempPic=tempPic+1;
tempPic=mod(tempPic-1,picNum)+1;
end
for j=1:blockNum
plot(cos(j*2*pi/blockNum).*Rrange,sin(j*2*pi/blockNum).*Rrange,'Color',lineColor,'LineWidth',lineWidth)
end
plot(cos(t).*Rrange(1),sin(t).*Rrange(1),'Color',lineColor,'LineWidth',lineWidth)
plot(cos(t).*Rrange(2),sin(t).*Rrange(2),'Color',lineColor,'LineWidth',lineWidth)
end

end
``````

Tags：matlab 相片 图像处理

## 文章评论

 留言与评论（共有 0 条评论）

 验证码：

## 点击排行

“当初要是早有人跟我说这些......“

## 站点信息

• 文章统计247968篇文章
• 浏览统计18356次浏览
• 评论统计1个评论
• 标签管理标签云
• 统计数据：统计代码
• 微信公众号：扫描二维码，关注我们