• Remix
  • Share
  • New Entry

on 14 Nov 2023
  • 17
  • 49
  • 2
  • 1
  • 1671
drawframe(1);
Write your drawframe function below
function drawframe(n)
persistent oriPic nailNum randNum brightAdd nailPos degreeMat XSet YSet outCircleMesh
if n==1
I=imshow('pout.tif');
oriPic=I.CData;
oriPic=imresize(oriPic,[400,400]);
oriPic=double(oriPic-min(min(oriPic)));
oriPic=oriPic./max(max(oriPic)).*255;
nailNum=280; % 钉子数量
randNum=60; % 一次采样数量
brightAdd=20; % 每次经过某像素增加的亮度值
[rows,cols,~]=size(oriPic);
midX=(1+cols)/2;
midY=(1+rows)/2;
% 绘制钉子
t=linspace(0,2*pi,nailNum+1);
t(end)=[];t=t(:);
nailPos=[(midX-0.5).*(cos(t)+1),(midY-0.5).*(sin(t)+1)]+0.5;
scatter(nailPos(:,1),nailPos(:,2),5,'filled','CData',[0 0 0])
hold on;axis tight;set(gca,'XColor','none','YColor','none')
% 将原本图像切割成椭圆形
% 并构造椭圆形蒙版
degreeMat=oriPic(end:-1:1,:);
[XMesh,YMesh]=meshgrid((1:cols)-midX,(1:rows)-midY);
normXMesh=XMesh./(midX-0.5);
normYMesh=YMesh./(midY-0.5);
l2Mesh=normXMesh.^2+normYMesh.^2;
outCircleMesh=l2Mesh>1;
degreeMat=double(degreeMat);
[XSet,YSet]=meshgrid(1:cols,1:rows);
end
% 主要循环
if n<=46
for i=22*(n-1)+1:22*n
% 抽取randNum个组合
randiNail=randi([1,nailNum],[randNum,2]);
randiNail(randiNail(:,1)==randiNail(:,2),:)=[];
% 计算每条直线经过位置亮度和
lumSet=inf.*ones(1,size(randiNail,1));
for j=1:size(randiNail,1)
pnt1=nailPos(randiNail(j,1),:);
pnt2=nailPos(randiNail(j,2),:);
v=[pnt2(2)-pnt1(2),pnt1(1)-pnt2(1)];v=v./norm(v);
onLine=abs((XSet-pnt1(1)).*v(1)+(YSet-pnt1(2)).*v(2))<1;
onLine=onLine&(~outCircleMesh);
if sum(sum(onLine))>10
lumMean=mean(degreeMat(onLine));
lumSet(j)=lumMean;
else
lumSet(j)=inf;
end
end
% 找到亮度和最高的组合,并将直线附近像素亮度值提高brightAdd
[~,index]=sort(lumSet);
S_pnt1=nailPos(randiNail(index(1),1),:);
S_pnt2=nailPos(randiNail(index(1),2),:);
S_v=[S_pnt2(2)-S_pnt1(2),S_pnt1(1)-S_pnt2(1)];S_v=S_v./norm(S_v);
S_onLine=abs((XSet-S_pnt1(1)).*S_v(1)+(YSet-S_pnt1(2)).*S_v(2))<0.6;
S_onLine=S_onLine&(~outCircleMesh);
degreeMat(S_onLine)=degreeMat(S_onLine)+brightAdd;% 提高亮度值
% 绘制带有透明度的线条
plot([S_pnt1(1),S_pnt2(1)],[S_pnt1(2),S_pnt2(2)],'Color',[0.2,0.2,0.2,0.4],'LineWidth',0.3)
end
end
end
Animation
Remix Tree