matlab实现手绘风格(简笔画风格、漫画风格)的曲线绘图
matlab实现手绘风格(简笔画风格、漫画风格)的曲线绘图
问题的起源是在国外网站的一些论坛上,为了绘出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 网站上的实现方法
如果上外网比较慢,也可以在优快云上也可以看到该方法的代码
https://blog.youkuaiyun.com/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,