效果:
步骤
1.参数设定及图片导入
可以只更改背景图片其实
bkgPic=imread(‘test.jpg’);%图片地址
lineDensity=0.6; %故障线条出现概率
lineLenRange=[50,80]; %故障线条长度范围
greenMoveLen=10; %绿移距离
原始图片:
2.图片灰度化及红蓝背景构造
[m,n,k]=size(bkgPic);
if k~=1
bkgPic=rgb2gray(bkgPic);
end
matSize=[n,m];
vector=[1,0];
colorList=[21 27 143
68 22 113
94 72 151
175 152 192
221 188 204
217 156 174
203 82 104
232 31 37
151 5 11];
colorMat=vColorMat(matSize,vector,colorList);
blueRedPic=uint8(double(colorMat).*double(bkgPic)./200);
其中所需要的渐变图构造函数可以看这篇博客:
MATLAB 各类二维渐变图
所用到的渐变图构造函数在这:
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);
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
end
红蓝渐变图效果:
当然大家可以自己取色制作其他渐变
背景叠加效果:
3.绿移
将绿色通道向左侧移动几像素,为了方便起见就没有在右侧补充像素,因此绿移后的图像宽度会少几个像素(有保留原图像大小需求的可以尝试自行改写我的代码,只需要将图像两侧提前扩充好再进行像素移动即可)
Rchannel=blueRedPic(:,1+greenMoveLen:end,1);
Gchannel=blueRedPic(:,1:end-greenMoveLen,2);
Bchannel=blueRedPic(:,1+greenMoveLen:end,3);
gm_brPic(:,:,1)=Rchannel;
gm_brPic(:,:,2)=Gchannel;
gm_brPic(:,:,3)=Bchannel;
绿移效果:
可以看出人物主体出现了明显绿色偏移
4.添加故障线条
先随机抽取一定量不重复整数,在被抽中的行使用’motion’动态模糊滤波器,模糊长度由lineLenRange参数决定:
tempRand=rand(1,m);
[~,movePos]=sort(tempRand);
movePos=movePos(1:floor(m*lineDensity));
movePos=sort(movePos);
movePic=gm_brPic(movePos,:,1);
for i=1:size(movePic,1)
H = fspecial('motion',randi(lineLenRange,[1,1]),0);
movePic(i,:) = imfilter(movePic(i,:),H,'replicate');
end
gm_brPic(movePos,:,1)=movePic;
imshow(gm_brPic);
效果:
5.完整代码
两个m文件:
BlueRed.m
function BlueRed
bkgPic=imread('test6.jpg');%图片地址
lineDensity=0.6; %故障线条出现概率
lineLenRange=[50,80]; %故障线条长度范围
greenMoveLen=10; %绿移距离
[m,n,k]=size(bkgPic);
if k~=1
bkgPic=rgb2gray(bkgPic);
end
matSize=[n,m];
vector=[1,0];
colorList=[21 27 143
68 22 113
94 72 151
175 152 192
221 188 204
217 156 174
203 82 104
232 31 37
151 5 11];
colorMat=vColorMat(matSize,vector,colorList);
blueRedPic=uint8(double(colorMat).*double(bkgPic)./200);
Rchannel=blueRedPic(:,1+greenMoveLen:end,1);
Gchannel=blueRedPic(:,1:end-greenMoveLen,2);
Bchannel=blueRedPic(:,1+greenMoveLen:end,3);
gm_brPic(:,:,1)=Rchannel;
gm_brPic(:,:,2)=Gchannel;
gm_brPic(:,:,3)=Bchannel;
tempRand=rand(1,m);
[~,movePos]=sort(tempRand);
movePos=movePos(1:floor(m*lineDensity));
movePos=sort(movePos);
movePic=gm_brPic(movePos,:,1);
for i=1:size(movePic,1)
H = fspecial('motion',randi(lineLenRange,[1,1]),0);
movePic(i,:) = imfilter(movePic(i,:),H,'replicate');
end
gm_brPic(movePos,:,1)=movePic;
imshow(gm_brPic);
end
vColorMat.m
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);
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
end
最前面提到的参数调整的不同会有不一样的效果哟
转载:https://blog.csdn.net/slandarer/article/details/116325539
查看评论