|
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!
fnkkm14iqjf6402988711.jpg
好玩的matlab
带你解锁不一样的matlab新玩法
今天介绍一下怎么利用Matlab制作圣诞节专属头像,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】
效果图
woqzyqjdbym6402988811.png
scljytns33n6402988913.gif
程序介绍
主界面
function addHatmFig = figure('units', 'pixels', 'position', [400 300 400 600], ... 'Numbertitle', 'off', 'menubar', 'none', 'resize', 'on', ... 'name', '公众号:好玩的Matlab');axes('parent', mFig, 'position', [0 0 1 1], ... 'XLim', [0 400], ... 'YLim', [-200 400], ... 'NextPlot', 'add', ... 'YDir', 'reverse', ... 'XTick', [], ... 'YTick', [],... 'layer', 'bottom', ... 'Visible', 'on');[avatar, ~, ~] = imread('tx1.jpg');[decorImg, ~, alphaImg] = imread('./img/2.png');ImgResult=image([0 400], [0 400], avatar);添加帽子hatPos = [200, 40];[n, m, ~] = size(decorImg);enlarge=0.08;DrawHatHdl = image([hatPos(1) - m* enlarge hatPos(1) + m* enlarge], ... [hatPos(2) - n* enlarge hatPos(2) + n* enlarge], decorImg, 'AlphaData', alphaImg);DrawHatHdl.HitTest = 'on';鼠标监听代码set(gcf,'WindowButtonDownFcn', @clickBoat); function clickBoat(~, ~) clickType = get(gcf, 'SelectionType'); XY = get(gca, 'CurrentPoint'); if strcmp(clickType, 'normal') Y = XY(1, 2); X = XY(1, 1); set(DrawHatHdl, 'XData', [X - m* enlarge X + m* enlarge], 'YData', [Y - n* enlarge Y + n* enlarge]); elseif strcmp(clickType, 'alt') Y = XY(1, 2); X = XY(1, 1); enlarge=enlarge*1.1; set(DrawHatHdl, 'XData', [X - m*enlarge X + m*enlarge], 'YData', [Y - n*enlarge Y + n*enlarge]); end endend
操作方法:鼠标右击放大帽子鼠标左击移动帽子位置全部代码
function addHatmFig = figure('units', 'pixels', 'position', [400 300 400 600], ... 'Numbertitle', 'off', 'menubar', 'none', 'resize', 'on', ... 'name', '公众号:好玩的Matlab');axes('parent', mFig, 'position', [0 0 1 1], ... 'XLim', [0 400], ... 'YLim', [-200 400], ... 'NextPlot', 'add', ... 'YDir', 'reverse', ... 'XTick', [], ... 'YTick', [],... 'layer', 'bottom', ... 'Visible', 'on');[avatar, ~, ~] = imread('tx1.jpg');[decorImg, ~, alphaImg] = imread('./img/2.png');ImgResult=image([0 400], [0 400], avatar);hatPos = [200, 40];[n, m, ~] = size(decorImg);enlarge=0.08;DrawHatHdl = image([hatPos(1) - m* enlarge hatPos(1) + m* enlarge], ... [hatPos(2) - n* enlarge hatPos(2) + n* enlarge], decorImg, 'AlphaData', alphaImg);DrawHatHdl.HitTest = 'on';
set(gcf,'WindowButtonDownFcn', @clickBoat); function clickBoat(~, ~) clickType = get(gcf, 'SelectionType'); XY = get(gca, 'CurrentPoint'); if strcmp(clickType, 'normal') Y = XY(1, 2); X = XY(1, 1); set(DrawHatHdl, 'XData', [X - m* enlarge X + m* enlarge], 'YData', [Y - n* enlarge Y + n* enlarge]); elseif strcmp(clickType, 'alt') Y = XY(1, 2); X = XY(1, 1); enlarge=enlarge*1.1; set(DrawHatHdl, 'XData', [X - m*enlarge X + m*enlarge], 'YData', [Y - n*enlarge Y + n*enlarge]); end endend
5vvfohsfw2r6402989013.png
小编根据原理又做了APP操作界面
0ojckalgrcr6402989113.png
- -THE END- -
源码下载:gitee下载:https://gitee.com/LoveMatlab/addHat
olsdzbjv3e36402989214.png
QQ 群下载:
zwtldubsqey6402989314.png
参考资料:
【1】https://slandarer.blog.csdn.net/article/details/118368777【2】https://mp.weixin.qq.com/s/mRIImZK-bLJWrZwVtkrhfw |
|