最近编辑时间:2021-5-11
这是一些比较有趣,但是感觉不值得单独发文章的小程序合集:
不定时更新,大家可以闲的没事来淘个宝

这里写目录标题

          • 1.字幕画
          • 2.鱼形曲线
          • 3.鼠标沙盘
          • 4.地图寻路演示
          • 5.樱花树
          • 6.字符画
          • 7.you need MATLAB
          • 8.图片各通道亮度频数直方图

1.字幕画


动图:


代码分为两个m文件
直接运行drawCaption或命令行打出
drawCaption(‘想发的文字’)即可

drawCaption.m

function drawCaption(string)
if nargin<1
    string='欢迎关注我的CSDN';
end
string=[string,' '];
CaptionMat=zeros(25*length(string),25);
for i=1:length(string)
    CaptionMat(25*(i-1)+1:25*i,:)=getWordMatrix(string(i));
end
CaptionMat=[CaptionMat;CaptionMat;CaptionMat];

fig=figure('units','pixels',...
        'position',[100 300 1000 250],...
        'Numbertitle','off',...
        'Color',[0 0 0],...
        'resize','off',...
         'menubar','none');

ax=axes('Units','pixels',...
        'parent',fig,...  
        'Color',[0 0 0],...
        'Position',[0 0 1000 250],...
        'XLim',[0 140],...
        'YLim',[0-5 25+5],...
        'XColor',[0 0 0],...
        'YColor',[0 0 0]);
hold(ax,'on')
[xSet,ySet]=find(CaptionMat~=0);


offset=0;
drawHdl=scatter(xSet+140-offset,ySet,28,'s','filled');

fps=25;
DCtimer=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @refreshWord);
start(DCtimer)
    function refreshWord(~,~)
        offset=offset+1;
        if mod(offset,length(string)*50)==0
            offset=offset-length(string)*25;
        end
        set(drawHdl,'XData',xSet+140-offset)  
    end
end

getWordMatrix.m

function wordMatrix=getWordMatrix(char)
fig=figure('units','pixels',...
        'position',[20 20 160 160],...
        'Numbertitle','off',...
        'Color',[1 1 1],...
        'resize','off',...
        'visible','off',...
         'menubar','none');

ax=axes('Units','pixels',...
        'parent',fig,...  
        'Color',[1 1 1],...
        'Position',[0 0 160 160],...
        'XLim',[0 16],...
        'YLim',[0 16],...
        'XColor',[1 1 1],...
        'YColor',[1 1 1]);
hold(ax,'on')
%,'FontWeight','bold
text(ax,8,8.5,char,'HorizontalAlignment','center','FontSize',120)
if 1
saveas(fig,['.\',char,'.png']);

pic=imread(['.\',char,'.png']);
delete(['.\',char,'.png'])
delete(ax)
close

[rowMax,colMax,~]=size(pic);
picData=pic(:,:,1);
picData(picData<125)=1;
picData(picData>=125)=0;

wordMatrix=zeros(25,25);
for i=1:25
    rowLim=round([i-1,i]./25.*rowMax);
    rowLim(rowLim==0)=1;
    
    for j=1:25
        colLim=round([j-1,j]./25.*colMax);
        colLim(colLim==0)=1;
        wordMatrix(i,j)=sum(sum(picData(rowLim(1):rowLim(2),colLim(1):colLim(2))));
    end
end
wordMatrix(wordMatrix<10)=0;
wordMatrix=wordMatrix';
wordMatrix=wordMatrix(:,end:-1:1);
wordMatrix(wordMatrix~=0)=1;

end
end

2.鱼形曲线

函数来自万能的知乎

代码:

hold on
axis equal
grid on
X=0:1:1023;
Y=0:1:1023;
[gridX,gridY]=meshgrid(X,Y);
FishPatternFcn=@(x,y)mod(abs(x.*sin(sqrt(x))+y.*sin(sqrt(y))).*pi./1024,1);
contour(gridX,gridY,FishPatternFcn(gridX,gridY),[0.7,0.7])

3.鼠标沙盘


代码:

function getPointer
fg=figure;
fg.NumberTitle='off';
fg.MenuBar='none';
fg.Resize='off';
fg.Position=[100 100 500 500];
fg.Name='getPointer';

ax=axes(fg);
ax.Position=[0 0 1 1];
ax.XLim=[-1 101];
ax.YLim=[-1 101];
ax.Color=[0 0 0];
hold(ax,'on');
[xSet,ySet]=meshgrid(0:100,0:100);
xSet=xSet(:);ySet=ySet(:);
oriX=xSet;oriY=ySet;
sc=scatter(xSet,ySet,1,'filled','CData',[1 1 1]);


set(gcf,'WindowButtonMotionFcn',@whilemovefcn)  
    function whilemovefcn(~,~)
        xy=get(gca,'CurrentPoint');
        x=xy(1,1);y=xy(1,2);
        nearPos=sqrt((xSet-x).^2+(ySet-y).^2)<5;
        xySet=[xSet,ySet];
        dir=[xSet,ySet]-[x,y];
        len=sqrt((xSet-x).^2+(ySet-y).^2);
        moveDis=5.8./(len+1);
        newPos=dir.*moveDis+[x,y];
        xySet(nearPos,:)=newPos(nearPos,:);
        set(sc,'XData',xySet(:,1),'YData',xySet(:,2))
        xSet=xySet(:,1);ySet=xySet(:,2);
    end

fps=50;
gptimer=timer('ExecutionMode', 'fixedRate', 'Period',1/fps, 'TimerFcn', @gp);
start(gptimer)

    function gp(~,~)     
        dirX=oriX-xSet;
        dirY=oriY-ySet;
        xSet=xSet+dirX.*(1/15);
        ySet=ySet+dirY.*(1/15);
        set(sc,'XData',xSet,'YData',ySet)
    end
end

4.地图寻路演示


代码:

function maze
fg=gcf;
fg.MenuBar='none';
ax=axes(fg);
ax.XLim=[0.5 30.5];
ax.YLim=[0.5 20];
ax.Position=[0 0 1 1];
ax.YDir='reverse';
ax.Toolbar.Visible='off';
ax.DataAspectRatio=[1 1 1];
ax.XColor=[.98 .98 .98];
ax.YColor=[.98 .98 .98];
hold(ax,'on')
Map=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
     5 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1;
     1 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1;
     1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1;
     1 1 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 1 1 1 0 1 1 1 1 1 0 1;
     1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1 0 0 0 1;
     1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 0 0 0 0 1 1 1 1 0 1 0 1 1 1;
     1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1;
     1 1 1 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1;
     1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 0 0 1;
     1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1;
     1 0 0 1 0 0 0 1 0 0 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 1;
     1 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1;
     1 0 0 1 1 1 1 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1;
     1 1 1 1 0 0 0 0 1 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1;
     1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1;
     1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1;
     1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 1 0 1 1 0 1 0 1 1 1;
     1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 4;
     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
mazeImage=imagesc(Map);
[startPnt.x,startPnt.y]=find(Map==5);
[endPnt.x,endPnt.y]=find(Map==4);
Dir=[1 0;0 -1;-1 0;0 1];
Path=[startPnt.x,startPnt.y];
breakflag=1;
while(breakflag)
    tailPnt=Path(end,:);
    flag=0;
    for i=1:4
        if (tailPnt(1)+Dir(i,1)>=1&&tailPnt(1)+Dir(i,1)<=20&&...
            tailPnt(2)+Dir(i,2)>=1&&tailPnt(2)+Dir(i,2)<=30)&&...     
           (Map(tailPnt(1)+Dir(i,1),tailPnt(2)+Dir(i,2))==0||...
            Map(tailPnt(1)+Dir(i,1),tailPnt(2)+Dir(i,2))==4)
            flag=i;break;   
        end     
    end
    if flag==0
        Map(tailPnt(1),tailPnt(2))=3;
        Path(end,:)=[]; 
    else
        if Map(tailPnt(1)+Dir(flag,1),tailPnt(2)+Dir(flag,2))==4
            breakflag=0;
        else
            Map(tailPnt(1)+Dir(flag,1),tailPnt(2)+Dir(flag,2))=2;
            Path=[Path;tailPnt(1)+Dir(flag,1),tailPnt(2)+Dir(flag,2)];
        end
    end
    pause(0.02)
    delete(mazeImage);
    mazeImage=imagesc(Map);
end



end
5.樱花树

会生成随机形状樱花树

function sakura
hold on,axis equal
axis(0.5+[-10,50,0,50])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color',[0.5020    0.5020    0.5020])

length_trunk=6;
width_trunk=4;
k1=0.9;
k2=0.8;
number_branch=15;
alp=pi/10;
length_branch=k1*length_trunk;
width_branch=k2*width_trunk;
trunk=[12,0;12,length_trunk];
plot(trunk(:,1),trunk(:,2),'color',[0 0 0],'Linewidth',width_trunk)
begins=[trunk(2,:),pi/2,1];
grow=begins;
plotdata=[0 0 0 0 0 0 0 0];
plotdata(1,:)=[];
for i=1:number_branch
    control=randi(25,[length(grow(:,1)),1])>=10;
    ag=grow(:,3);
    l=length(ag);
    parta=[length_branch.*k1.^grow(:,4).*cos(ag+ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag+ones(l,1)*alp),ones(l,1)*alp,ones(l,1)];
    partb=[length_branch.*k1.^grow(:,4).*cos(ag-ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag-ones(l,1)*alp),-ones(l,1)*alp,ones(l,1)];
    parta2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    partb2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    parta=control.*parta+(~control).*parta2;
    partb=control.*partb+(~control).*partb2;
    parta=parta+grow;
    partb=partb+grow;
    congress=[parta;partb];
    grow=[grow;grow];
    judge=[grow,congress];
    judge=unique(judge,'rows');
    grow=judge(:,5:end);
    plotdata=[plotdata;judge];
end
for i=1:number_branch
    temp_w=width_branch*0.8^i;
    temp_branch=plotdata(plotdata(:,4)==i,:);
    plx=[temp_branch(:,1),temp_branch(:,5)];
    ply=[temp_branch(:,2),temp_branch(:,6)];
    plx=plx';ply=ply';
    plot(plx,ply,'color',[0 0 0]+i*[0.3020 0.3020 0.3020]./number_branch,'Linewidth',temp_w)
end

bloom_pos=plotdata(plotdata(:,8)==number_branch+1,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),10,'CData',[0.8549    0.6824    0.6824])
bloom_pos=plotdata(plotdata(:,8)==number_branch,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),8,'CData',[0.7451    0.5961    0.5961].*0.97)




end
6.字符画

将图像转为字符画并存到test.txt内
为了显示全可将txt内文本字体大小调小
由于字间距问题图像会被略微拉长


function StringPic
sizecol=256;
OriPic=imread('图片存储位置');
tempcol=size(OriPic,2);
tempmul=sizecol/tempcol;
OriPic=imresize(OriPic,tempmul,'nearest');
GraPic=sum(OriPic,3)./3;if any(GraPic>1)
    GraPic=GraPic./255;
end
FillChar='$W&@E#8}]=+;;,,..  ';
FillChar_Len=length(FillChar);
GraPic=floor(GraPic./(1/(FillChar_Len-1)))+1;
for i=1:size(GraPic,1)
    for j=1:size(GraPic,2)
        StrPic(i,j)=FillChar(GraPic(i,j));
    end
end
filename='test.txt';
writematrix(StrPic,filename,'delimiter','tab')
end
7.you need MATLAB
function life_is_short_I_choose_Python
error('Life is short, you need MATLAB')
end
function life_is_short_I_choose_Python
ME = MException('MATLAB:test','Life is short, you need MATLAB');throw(ME)
end

上下俩写法效果一样的:

8.图片各通道亮度频数直方图

彩图直方图绘制效果:

灰度图直方图绘制效果:

代码:
使用的时候改变imread中的图片文件路径即可

function HistogramPic
pic=imread('test.jpg');
%pic=rgb2gray(pic);

FreqNum=zeros(size(pic,3),256);
for i=1:size(pic,3)
    for j=0:255
        FreqNum(i,j+1)=sum(sum(pic(:,:,i)==j));
    end
end

ax=gca;
hold(ax,'on')

if size(FreqNum,1)==3
    rBar=bar(0:255,FreqNum(1,:));
    gBar=bar(0:255,FreqNum(2,:));
    bBar=bar(0:255,FreqNum(3,:));
    rBar.FaceColor=[0.6350 0.0780 0.1840];
    gBar.FaceColor=[0.2400 0.5300 0.0900];
    bBar.FaceColor=[0      0.4470 0.7410];
    rBar.FaceAlpha=0.5;
    gBar.FaceAlpha=0.5;
    bBar.FaceAlpha=0.5;
    ax.XLabel.String='RGB brightness';
    
    rrange=find(FreqNum(1,:)~=0);
    rrange=[num2str(rrange(1)-1),' , ',num2str(rrange(end)-1)];
    grange=find(FreqNum(2,:)~=0);
    grange=[num2str(grange(1)-1),' , ',num2str(grange(end)-1)];
    brange=find(FreqNum(3,:)~=0);
    brange=[num2str(brange(1)-1),' , ',num2str(brange(end)-1)];
    legend({['R: range[',rrange,']'],...
            ['G: range[',grange,']'],...
            ['B: range[',brange,']']},...
             'Location','northwest','Color',[0.9412    0.9412    0.9412],...
             'FontName','Cambria','LineWidth',0.8,'FontSize',11);
else 
    kBar=bar(0:255,FreqNum(1,:));
    kBar.FaceColor=[0.50 0.50 0.50];
    kBar.FaceAlpha=0.5;
    ax.XLabel.String='Gray scale';
    krange=find(FreqNum(1,:)~=0);
    krange=[num2str(krange(1)-1),' , ',num2str(krange(end)-1)];
    legend(['Gray: range[',krange,']'],...
           'Location','northwest','Color',[0.9412    0.9412    0.9412],...
           'FontName','Cambria','LineWidth',0.8,'FontSize',11);
end
box on
grid on
ax.LineWidth = 1;
ax.GridLineStyle='--';
ax.XLim=[-5 255];
ax.XTick=[0:45:255,255];


ax.XLabel.FontSize=13;
ax.XLabel.FontName='Cambria';

ax.YLabel.String='Frequency number';
ax.YLabel.FontSize=13;
ax.YLabel.FontName='Cambria';
end

更多推荐

MATLAB有趣或有用小程序合集