效果如下:


步驟
1.匯入圖片,獲取每個區域的平均顏色,構造亂數矩陣
匯入圖片后我們首先將圖片劃分成很多個15x15(可自行調整大小)的小格子,并求取每個格子的顏色平均值,作為柱狀圖每個小柱子的顏色,構造亂數矩陣作為每個小柱子的高度
oriPic=imread('test.jpg');
pixLen=15;
[H,W,~]=size(oriPic);
XList=1:pixLen:W-pixLen;
YList=1:pixLen:H-pixLen;
samplePic=zeros(length(YList),length(XList),3);
for x=XList
for y=YList
sampleSquareR=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,1));
sampleSquareG=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,2));
sampleSquareB=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,3));
samplePic(y==YList,x==XList,1)=mean(sampleSquareR(:));
samplePic(y==YList,x==XList,2)=mean(sampleSquareG(:));
samplePic(y==YList,x==XList,3)=mean(sampleSquareB(:));
end
end
randiMat=randi([0,250],[length(YList),length(XList)]);
原始圖片:

如果用方塊顏色矩陣和方塊高度矩陣直接去畫圖的話,會因為每個顏色只有一個點,而出現如下的效果,

為了畫出一個一個小平臺的效果,我們要將每個點擴充成15X15個顏色相同,高度相同的點集
2.點集擴充,曲面繪制
expCMat=zeros(length(YList)*pixLen,length(XList)*pixLen,3);
expZMat=zeros(length(YList)*pixLen,length(XList)*pixLen);
for x=XList
for y=YList
expCMat(y:y-1+pixLen,x:x-1+pixLen,1)=samplePic(y==YList,x==XList,1);
expCMat(y:y-1+pixLen,x:x-1+pixLen,2)=samplePic(y==YList,x==XList,2);
expCMat(y:y-1+pixLen,x:x-1+pixLen,3)=samplePic(y==YList,x==XList,3);
expZMat(y:y-1+pixLen,x:x-1+pixLen)=randiMat(y==YList,x==XList);
end
end
[XMesh,YMesh]=meshgrid(1:length(XList)*pixLen,1:length(YList)*pixLen);
surf(XMesh,expZMat,YMesh(end:-1:1,:),'CData',expCMat./255,...
'FaceColor','interp','edgeColor','none',...
'SpecularStrength',0,'AmbientStrength',1,'DiffuseStrength',1)
效果:
我們會發現此時每個小方塊界限并不明顯,而且當正面看時,曲面幾乎看不出任何突出效果,這兩點可以通過設定前縮透視法,和增添光照來解決


3.坐標區屬性設定及前縮透視法
以下大部分代碼為了調整視角和顏色,ax.Projection=‘perspective’; 用來設定前縮透視
ax=gca;
hold(ax,'on')
ax.Projection='perspective';
ax.Color=[0.95 0.95 0.95];
ax.DataAspectRatioMode='manual';
ax.DataAspectRatio=[1,0.1,1];
ax.XLim=[0,W];
ax.ZLim=[0,H];
ax.View=[-0.4830 -0.0717];
ax.XTick=[];
ax.YTick=[];
ax.ZTick=[];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';
效果:

4.光照設定
可以根據自己的喜好微調哦
h = light;
h.Style='local';
h.Color=[1,1,1].*0.5;
h.Position=[W/2,-1000,0];
效果:

5.完整代碼
function pieceWisePic
oriPic=imread('test4.jpg');
pixLen=15;
[H,W,~]=size(oriPic);
XList=1:pixLen:W-pixLen;
YList=1:pixLen:H-pixLen;
samplePic=zeros(length(YList),length(XList),3);
for x=XList
for y=YList
sampleSquareR=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,1));
sampleSquareG=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,2));
sampleSquareB=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,3));
samplePic(y==YList,x==XList,1)=mean(sampleSquareR(:));
samplePic(y==YList,x==XList,2)=mean(sampleSquareG(:));
samplePic(y==YList,x==XList,3)=mean(sampleSquareB(:));
end
end
randiMat=randi([0,250],[length(YList),length(XList)]);
expCMat=zeros(length(YList)*pixLen,length(XList)*pixLen,3);
expZMat=zeros(length(YList)*pixLen,length(XList)*pixLen);
for x=XList
for y=YList
expCMat(y:y-1+pixLen,x:x-1+pixLen,1)=samplePic(y==YList,x==XList,1);
expCMat(y:y-1+pixLen,x:x-1+pixLen,2)=samplePic(y==YList,x==XList,2);
expCMat(y:y-1+pixLen,x:x-1+pixLen,3)=samplePic(y==YList,x==XList,3);
expZMat(y:y-1+pixLen,x:x-1+pixLen)=randiMat(y==YList,x==XList);
end
end
[XMesh,YMesh]=meshgrid(1:length(XList)*pixLen,1:length(YList)*pixLen);
surf(XMesh,expZMat,YMesh(end:-1:1,:),'CData',expCMat./255,...
'FaceColor','interp','edgeColor','none',...
'SpecularStrength',0,'AmbientStrength',1,'DiffuseStrength',1)
ax=gca;
hold(ax,'on')
ax.Projection='perspective';
ax.Color=[0.95 0.95 0.95];
ax.DataAspectRatioMode='manual';
ax.DataAspectRatio=[1,0.1,1];
ax.XLim=[0,W];
ax.ZLim=[0,H];
ax.View=[-0.4830 -0.0717];
ax.XTick=[];
ax.YTick=[];
ax.ZTick=[];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';
h = light;
h.Style='local';
h.Color=[1,1,1].*0.5;
h.Position=[W/2,-1000,0];
end
嘗試的其他幾個圖

當方塊取的較小時(5x5)

另:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283028.html
標籤:AI
