要构造二维渐变图,我们首先需要一个将[0,1]之间数值映射到相对应RGB数值的函数,这个函数可以用以下生成器生成:
function colorFunc=colorFuncFactory(colorList)
x=(0:size(colorList,1)-1)./(size(colorList,1)-1);
y1=colorList(:,1);y2=colorList(:,2);y3=colorList(:,3);
colorFunc=@(X)[interp1(x,y1,X,'linear')',interp1(x,y2,X,'linear')',interp1(x,y3,X,'linear')'];
end
用法:
c=[195 53 93
211 102 141
231 179 192
229 182 172
227 178 137
238 191 147
236 195 113];
colorFunc=colorFuncFactory( c);
newC=colorFunc(0:0.001:1);
其中c是RGB数值,colorFunc为由c构造的映射函数,newC是新生成 1000个RGB数值
效果:
随着方向向量不同,得到的结果不同:
vector=[1,12];
vector=[3,4];
matSize=[800,600];
vector=[3,4];
colorList=[195 53 93
211 102 141
231 179 192
229 182 172
227 178 137
238 191 147
236 195 113];
colorMat=vColorMat(matSize,vector,colorList);
imshow(colorMat)
函数代码:
function colorMat=vColorMat(matSize,vector,colorList)
% matSize=[800,600];
% vector=[1,12];
% colorList=[195 53 93
% 211 102 141
% 231 179 192
% 229 182 172
% 227 178 137
% 238 191 147
% 236 195 113];
% colorMat=vColorMat(matSize,vector,colorList)
% imshow(colorMat)
vector=vector./norm(vector);
[xMesh,yMesh]=meshgrid(0:matSize(2)-1,0:matSize(1)-1);
zMesh=xMesh.*vector(2)+yMesh.*vector(1);
zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh)));
colorFunc=colorFuncFactory(colorList);
colorMesh=colorFunc(zMesh);
colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1));
colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1));
colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1));
colorMat=uint8(colorMat);
end
matSize=[800,600];
point=[400,100];
colorList=[195 53 93
211 102 141
231 179 192
229 182 172
227 178 137
238 191 147
236 195 113];
colorMat=cColorMat(matSize,point,colorList);
imshow(colorMat)
函数代码:
function colorMat=cColorMat(matSize,point,colorList)
% matSize=[800,600];
% point=[400,100];
% colorList=[195 53 93
% 211 102 141
% 231 179 192
% 229 182 172
% 227 178 137
% 238 191 147
% 236 195 113];
% colorMat=cColorMat(matSize,point,colorList);
% imshow(colorMat)
[xMesh,yMesh]=meshgrid(1:matSize(2),1:matSize(1));
zMesh=sqrt((xMesh-point(2)).^2+(yMesh-point(1)).^2);
zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh)));
colorFunc=colorFuncFactory(colorList);
colorMesh=colorFunc(zMesh);
colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1));
colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1));
colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1));
colorMat=uint8(colorMat);
end
matSize=[800,600];
point=[400,100];
colorList=[195 53 93
211 102 141
231 179 192
229 182 172
227 178 137
238 191 147
236 195 113];
colorMat=sColorMat(matSize,point,colorList);
imshow(colorMat)
函数代码:
function colorMat=sColorMat(matSize,point,colorList)
% matSize=[800,600];
% point=[400,100];
% colorList=[195 53 93
% 211 102 141
% 231 179 192
% 229 182 172
% 227 178 137
% 238 191 147
% 236 195 113];
% colorMat=sColorMat(matSize,point,colorList);
% imshow(colorMat)
[xMesh,yMesh]=meshgrid(1:matSize(2),1:matSize(1));
zMesh=abs(xMesh-point(2))+abs(yMesh-point(1));
zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh)));
colorFunc=colorFuncFactory(colorList);
colorMesh=colorFunc(zMesh);
colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1));
colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1));
colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1));
colorMat=uint8(colorMat);
end
x=-2:0.01:2;
y1=sqrt(2sqrt(x.2)-x.2);
y2=-2.14sqrt(sqrt(2)-sqrt(abs(x)));
x=x.*100+250;
y1=y1.*100+300;
y2=y2.*100+300;
pointList=[[x’,y1’];[x’,y2’]];
matSize=[600,500];
colorList=[195 53 93
211 102 141
231 179 192
229 182 172
227 178 137
238 191 147
236 195 113];
colorMat=lColorMat(matSize,pointList,colorList);
imshow(colorMat)
函数代码:
function colorMat=lColorMat(matSize,pointList,colorList)
% x=-2:0.01:2;
% y1=sqrt(2*sqrt(x.^2)-x.^2);
% y2=-2.14*sqrt(sqrt(2)-sqrt(abs(x)));
%
% x=x.*100+250;
% y1=y1.*100+300;
% y2=y2.*100+300;
% pointList=[[x',y1'];[x',y2']];
%
% matSize=[600,500];
% colorList=[195 53 93
% 211 102 141
% 231 179 192
% 229 182 172
% 227 178 137
% 238 191 147
% 236 195 113];
% colorMat=lColorMat(matSize,pointList,colorList);
% imshow(colorMat)
[xMesh,yMesh]=meshgrid(1:matSize(2),1:matSize(1));
zMesh=sqrt((xMesh-pointList(1,2)).^2+(yMesh-pointList(1,1)).^2);
for i=2:size(pointList,1)
tempMesh=sqrt((xMesh-pointList(i,2)).^2+(yMesh-pointList(i,1)).^2);
zMesh(zMesh-tempMesh>0)=tempMesh(zMesh-tempMesh>0);
end
zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh)));
colorFunc=colorFuncFactory(colorList);
colorMesh=colorFunc(zMesh);
colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1));
colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1));
colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1));
colorMat=uint8(colorMat);
end
img=imread(‘test.jpg’);
newImg=imgColorMat(img);
imshow(newImg)
函数代码:
function newImg=imgColorMat(img)
size(img)
[rows,cols,~]=size(img);
xList=[1:10:rows-10,rows];
yList=[1:10:cols-10,cols];
simplePoints=img(xList,yList,:);
simplePoints=imgaussfilt(simplePoints,4);
Rchannel=simplePoints(:,:,1);
Gchannel=simplePoints(:,:,2);
Bchannel=simplePoints(:,:,3);
[xMesh,yMesh]=meshgrid(yList,xList);
[newXMesh,newYMesh]=meshgrid(1:cols,1:rows);
newR=interp2(xMesh,yMesh,double(Rchannel),newXMesh,newYMesh,'spline');
newG=interp2(xMesh,yMesh,double(Gchannel),newXMesh,newYMesh,'spline');
newB=interp2(xMesh,yMesh,double(Bchannel),newXMesh,newYMesh,'spline');
newImg(:,:,1)=newR;
newImg(:,:,2)=newG;
newImg(:,:,3)=newB;
newImg=uint8(newImg);
end
注:
各颜色数据可以由我之前博客发布的各类RGB颜色提取器提取: