matlab实现手绘风格(简笔画风格、漫画风格)的曲线绘图

matlab实现手绘风格(简笔画风格、漫画风格)的曲线绘图

问题的起源是在国外网站的一些论坛上,为了绘出xkcd漫画风格的曲线,尝试用不同的绘图软件进行尝试。

下图为xkcd漫画中的一幅曲线插图。
xkcd漫画

比如在Create xkcd style diagram in TeX中,用TeX绘制这种曲线
https://tex.stackexchange.com/questions/74878/create-xkcd-style-diagram-in-tex
比如用python绘制曲线
https://stackoverflow.com/questions/29061729/gnuplot-how-to-mimic-sketch-graphs
在这里插入图片描述
还有利用matlab进行尝试的
https://stackoverflow.com/questions/12701841/xkcd-style-graphs-in-matlab
在这里插入图片描述
接下来对上面matlab网站的代码进行实现,并自己进行一些改进

1 网站上的实现方法

如果上外网比较慢,也可以在CSDN上也可以看到该方法的代码
https://blog.csdn.net/meatball1982/article/details/80536909
下面我对代码进行了分析和优化:
算法的思路如下:

1 绘制坐标轴,加粗
2 进行曲线绘制,加粗
3 对新叠加的曲线进行加白边的处理
4 添加手绘效果的字体
5 对整体图片进行随机扭动处理

优化的代码如下:

%定义曲线
x = 1:0.1:10;
y1 = sin(x).*exp(-x/3) + 3;
y2 = 3*exp(-(x-7).^2/2) + 1;

%绘制曲线
fh = figure('color','w');
hold on
plot(x,y1,'b','lineWidth',3);
plot(x,y2,'w','lineWidth',7);
plot(x,y2,'r','lineWidth',3);
%设置坐标轴范围
xlim([0.95 10])
ylim([0 5])
%更改坐标轴字体
set(gca,'fontName','Comic Sans MS','fontSize',18,'lineWidth',3,'box','off')
%设置输出图片大小
set(fh,'Units','pixels','Position',[100 100 360 270])
%添加文字 
 annotation(fh,'textarrow',[0.3 0.45],[0.55 0.40],...
     'string',sprintf('text%shere',char(10)),'headStyle','none','lineWidth',2,...
     'fontName','Comic Sans MS','fontSize',14,'verticalAlignment','middle','horizontalAlignment','left')

%保存原图并输入矩阵
saveas(gcf,'test.bmp');
close all
im=imread('test.bmp');


%增加一点白边防止后面黑边
im = padarray(im,[15 15 0],255);

%做随机扭动特效
sfc = size(im);
[yy,xx]=ndgrid(1:7:sfc(1),1:7:sfc(2));
pts = [xx(:),yy(:)];
tf = fitgeotrans(pts+2*randn(size(pts)),pts,'lwm',12);
w = warning;
warning off images:inv_lwm:cannotEvaluateTransfAtSomeOutputLocations
imt = imwarp(im,tf);
warning(w)

%去除之前白边
imt = imt(16:end-15,16:end-15,:);
%绘图
figure('color','w')
imshow(imt)

这是输出未进行随机扭曲的图像
在这里插入图片描述
之后对原图像进行随机扭曲,得到下图类似水波纹的效果:
在这里插入图片描述

2 自己的实现方法

在之前方法的基础上加入了perlin噪声,使得曲线有更大尺度的震荡
原函数绘制如下:
在这里插入图片描述
去掉坐标轴,加入自己绘制的坐标轴,并加入较大尺度的柏林噪声,得到:
在这里插入图片描述
之后将图像整体进行扭曲
在这里插入图片描述
matlab的代码如下

%自己尝试的
%定义函数
x = 1:0.1:10;
y1 = sin(x).*exp(-x/3) + 3;
y2 = 3*exp(-(x-7).^2/2) + 1;
xlimtmat=[0.95 10];
ylimtmat=[0 5];

%变化参数
D=0.03;%perlin噪声振幅
N=3;%perlin噪声密度
S=1.2;%波动特效
axislabel_x=2:2:8;
axislabel_y=0:2:5;

%第一个图
fh1 = figure('color','w');
hold on
plot(x,y1,'b','lineWidth',3);
plot(x,y2,'r','lineWidth',3);
hold off
set(fh1,'Units','pixels','Position',[100 100 360 270])
saveas(gcf,'test1.bmp');
close all


%第二个图
fh2 = figure('color','w');
hold on

%坐标轴绘制
xaxis_x=linspace(xlimtmat(1),xlimtmat(end),101);
xaxis_y=linspace(ylimtmat(1), ylimtmat(1) ,101);
yaxis_x=linspace(xlimtmat(1), xlimtmat(1) ,101);
yaxis_y=linspace(ylimtmat(1),ylimtmat(end),101);
plot(xaxis_x,xaxis_y+randfun2(xaxis_x,N)*D*diff(ylimtmat),'k','lineWidth',3)%x轴
plot(yaxis_x+randfun2(yaxis_y,N)*D*diff(xlimtmat),yaxis_y,'k','lineWidth',3)%y轴

%曲线绘制
yrand=randfun2(x,N)*D*diff(ylimtmat);
plot(x,y1+randfun2(x,N)*D*diff(ylimtmat),'b','lineWidth',3);
plot(x,y2+yrand,'w','lineWidth',7);
plot(x,y2+yrand,'r','lineWidth',3);
xlim([xlimtmat(1)-0.8*D*diff(xlimtmat),xlimtmat(end)])
ylim([ylimtmat(1)-0.8*D*diff(ylimtmat),ylimtmat(end)])
axis off



%文字
annotation(fh2,'textarrow',[0.3 0.45],[0.55 0.40],...
     'string',sprintf('text%shere',char(10)),'headStyle','none','lineWidth',2,...
     'fontName','Comic Sans MS','fontSize',14,'verticalAlignment','middle','horizontalAlignment','left')
%坐标文字
stringcell_x=num2cell(axislabel_x);
stringcell_y=num2cell(axislabel_y);
text(axislabel_x,0*axislabel_x-diff(xlimtmat)*0.03,stringcell_x,'FontSize',14,'fontName','Comic Sans MS')
text(0*axislabel_y-diff(ylimtmat)*0,axislabel_y,stringcell_y,'FontSize',14,'fontName','Comic Sans MS')%,'HorizontalAlignment','right')

hold off
set(fh2,'Units','pixels','Position',[100 100 360 270])

%保存
saveas(gcf,'test2.bmp');
close all
%读取为矩阵
im=imread('test2.bmp');
%增加一点边防止后面黑边
im = padarray(im,[15 15 0],255);

%做随机扭动特效
sfc = size(im);
[yy,xx]=ndgrid(1:7:sfc(1),1:7:sfc(2));
pts = [xx(:),yy(:)];
tf = fitgeotrans(pts+S*randn(size(pts)),pts,'lwm',12);
w = warning;
warning off images:inv_lwm:cannotEvaluateTransfAtSomeOutputLocations
imt = imwarp(im,tf);
warning(w)

%去除之前白边
imt = imt(16:end-15,16:end-15,:);
%第三个图
fh3=figure('color','w');
imshow(imt)
set(fh3,'Units','pixels','Position',[100 100 360 270])
saveas(gcf,'test3.bmp');


%生成随机曲线
function yr=randfun1(x,N)
%利用样条函数随机噪声,效果不好
yR=rand(1,N+2);
xR=x(1)+rand(1,N)*(x(end)-x(1));
xR=[x(1),xR,x(end)];
yr=interp1(xR,yR,x,'spline');
end

function yr=randfun2(x,N)
%利用perlin噪声
%分配插值点
xP=(x-x(1))/(x(end)-x(1))*N;
%为插值点分配随机梯度
uxmat=rand(1,N+2)*2-1;
%设立空矩阵
zmat=0*xP;
for j=1:length(xP)
    %计算每个角点的点积
    %n0
    ddx=xP(j)-floor(xP(j));
    ux=uxmat(1,floor(xP(j))-0+1);
    n0=(ux*ddx);
    %n1
    ddx=xP(j)-floor(xP(j))-1;
%     if floor(xP(j))-0+2==12
%         xP
%     end
    ux=uxmat(1,floor(xP(j))-0+2);
    n1=(ux*ddx);
    %6加权
    zmat(j)=lerp(n0,n1,xP(j)-floor(xP(j)));
end
yr=zmat/N*(x(end)-x(1))+x(1);
end

function u=lerp(a,b,t)%权重相加
    tw=6*t.^5-15*t.^4+10*t.^3;%6*t.^5-15*t.^4+10*t.^3;%3*t.^2-2*t.^3;%
    u=(1-tw)*a + tw*b;
end

猜你喜欢

转载自blog.csdn.net/weixin_42943114/article/details/86670508
今日推荐