当前位置:   article > 正文

数字图像处理之matlab大作业:美图秀秀_matlab图像处理系统滤镜怀旧色功能

matlab图像处理系统滤镜怀旧色功能

1、放大缩小

从变量上看,图片是放大缩小了,但显示出来有点问题,应该是显示设置的原因。缩小的这张图不就是马赛克么~ 

  1. clear,clc,close all;
  2. Image=im2double(imread('lisa.jpg'));
  3. subplot(1,3,1),imshow(Image,'InitialMagnification');
  4. NewImage1=imresize(Image,[40,40]);
  5. NewImage2=imresize(Image,[6000,6000]);
  6. subplot(1,3,2),imshow(NewImage1,'InitialMagnification');
  7. subplot(1,3,3),imshow(NewImage2,'InitialMagnification');

2、翻转和旋转

  1. Image2=imread('lisa.jpg');
  2. HImage=flipdim(Image2,2);
  3. VImage=flipdim(Image2,1);
  4. CImage=flipdim(HImage,1);
  5. subplot(221),imshow(Image2);
  6. subplot(222),imshow(HImage);
  7. subplot(223),imshow(VImage);
  8. subplot(224),imshow(CImage);

  

  1. Image=im2double(imread('lisa.jpg'));
  2. NewImage1=imrotate(Image,15);
  3. NewImage2=imrotate(Image,40,'bilinear');
  4. subplot(1,2,1),imshow(NewImage1);
  5. subplot(1,2,2),imshow(NewImage2);
  6. imwrite(NewImage1,'rotate11.jpg');
  7. imwrite(NewImage2,'rotate12.jpg');

3、图像剪切 

  1. [I,map]=imread('lisa.jpg');
  2. figure;
  3. subplot(121);imshow(I,map);
  4. %指定剪切区域的大小和位置,剪切,返回xy坐标和裁剪区域
  5. [x,y,I2,rect]=imcrop(I,map,[100 200 40 40]);%位置和区域大小
  6. subplot(122);imshow(I2);

4、图像增强(提高对比度)

  1. picture=imread("lisa.jpg");
  2. picture1=histeq(picture);
  3. figure;
  4. subplot(121);
  5. imshow(picture);
  6. subplot(122);
  7. imshow(picture1);
  8. figure;
  9. subplot(121);
  10. imhist(picture);
  11. subplot(122);
  12. imhist(picture1);

5、磨皮

  1. noisyImg=imread('tina.jpg');
  2. r=10;
  3. level=5;
  4. sigma=10+level^2;
  5. dnImg=denoiseBasedLocalStat(noisyImg,r,sigma);
  6. figure,imshow(noisyImg);
  7. figure,imshow(dnImg);
  8. mask=faceMask(noisyImg,5);
  9. figure,imshow(mask);
  10. w=0.5;
  11. mergedImg=uint8(double(noisyImg).*(1-w*mask)+w*double(dnImg).*mask);
  12. figure,imshow(mergedImg);
  13. function dnImg = denoiseBasedLocalStat(img,r,sigma)
  14. % 基于局部统计信息的图像滤波去噪
  15. if size(img,3)==1
  16. img=double(img);
  17. dnImg=denoiseBasedLocalStat_gray(img,r,sigma);
  18. dnImg=uint8(dnImg);
  19. end
  20. if size(img,3)==3
  21. ycc=rgb2ycbcr(img);
  22. y=ycc(:,:,1);
  23. dnY=denoiseBasedLocalStat_gray(double(y),r,sigma);
  24. ycc(:,:,1)=uint8(dnY);
  25. dnImg=ycbcr2rgb(ycc);
  26. end
  27. end
  28. function dnImg=denoiseBasedLocalStat_gray(img,r,sigma)
  29. % 基于局部统计信息的灰度图像滤波去噪
  30. dnImg=zeros(size(img));
  31. img=double(img);
  32. paddedImg=padarray(img,[r,r],'symmetric','both');
  33. [Yim,YYim]=intergalMap(paddedImg);
  34. for i=r+1:size(paddedImg,1)-r
  35. for j=r+1:size(paddedImg,2)-r
  36. rectSum=Yim(i+r+1,j+r+1)+Yim(i-r,j-r)-Yim(i-r,j+r+1)-Yim(i+r+1,j-r);
  37. rectSquareSum=YYim(i+r+1,j+r+1)+YYim(i-r,j-r)-YYim(i-r,j+r+1)-YYim(i+r+1,j-r);
  38. num=(2*r+1)^2;
  39. avg=rectSum/num;
  40. var=rectSquareSum/num-avg^2;
  41. k=var/(var+sigma^2);
  42. dnImg(i-r,j-r)=(1-k)*avg+k*img(i-r,j-r);
  43. end
  44. end
  45. end
  46. function [Yim,YYim]=intergalMap(img)
  47. % 生成积分图
  48. paddedImg=padarray(img,[1 1],0,'pre');
  49. Yim=zeros(size(paddedImg));
  50. YYim=Yim;
  51. for i=2:size(paddedImg,1)
  52. for j=2:size(paddedImg,2)
  53. Yim(i,j)=Yim(i,j-1)+Yim(i-1,j)-Yim(i-1,j-1)+paddedImg(i,j);
  54. YYim(i,j)=YYim(i,j-1)+YYim(i-1,j)-YYim(i-1,j-1)+paddedImg(i,j)^2;
  55. end
  56. end
  57. end
  58. function mask=faceMask(img,r)
  59. % 人脸掩模
  60. mask=zeros(size(img,1),size(img,2));
  61. mask(img(:,:,1)>20 & img(:,:,2)>40 & img(:,:,3)>50)=1;
  62. mask=myBoxFilt(mask,r);
  63. mask=mask(:,:,ones(1,3));
  64. end
  65. function fImg=myBoxFilt(img,r)
  66. % 盒式滤波
  67. paddedImg=padarray(img,[r r],'symmetric','both');
  68. [Yim,~]=intergalMap(paddedImg);
  69. for i=r+1:size(paddedImg,1)-r
  70. for j=r+1:size(paddedImg,2)-r
  71. rectSum=Yim(i+r+1,j+r+1)+Yim(i-r,j-r)-Yim(i-r,j+r+1)-Yim(i+r+1,j-r);
  72. avg=rectSum/(2*r+1)^2;
  73. fImg(i-r,j-r)=avg;
  74. end
  75. end
  76. end

6、美白

参考:【数字图像处理】实验(3)——图像综合应用:皮肤美化(MATLAB实现)_虚神公子的博客-CSDN博客_matlab图像美颜

这部分的代码也同时实现了磨皮的功效,感觉前后两个功能差不多,但实现方式不一样。 

  1. clear,clc,close all;
  2. ImageOrigin=im2double(imread('tina.jpg'));
  3. figure,imshow(ImageOrigin),title('原图');
  4. DBImage=DBfilt(ImageOrigin); %双边滤波
  5. SkinImage1=FirstFilter(ImageOrigin); %%初步过滤
  6. SkinArea=SecondFilter(SkinImage1); %%YCgCr空间范围肤色检测
  7. SkinFuse=Fuse(ImageOrigin,DBImage,SkinArea);%图像融合
  8. SkinBeautify=Sharp(SkinFuse); %图像锐化
  9. Meibai=MB(SkinBeautify);
  10. %2.图像平滑
  11. function Out=DBfilt(In)
  12. [height,width,c] = size(In);
  13. win=15; % 定义双边滤波窗口宽度
  14. sigma_s=6; sigma_r=0.1; % 双边滤波的两个标准差参数
  15. [X,Y] = meshgrid(-win:win,-win:win);
  16. Gs = exp(-(X.^2+Y.^2)/(2*sigma_s^2));%计算邻域内的空间权值
  17. Out=zeros(height,width,c);
  18. for k=1:c
  19. for j=1:height
  20. for i=1:width
  21. temp=In(max(j-win,1):min(j+win,height),max(i-win,1):min(i+win,width),k);
  22. Gr = exp(-(temp-In(j,i,k)).^2/(2*sigma_r^2));%计算灰度邻近权值
  23. % W为空间权值Gs和灰度权值Gr的乘积
  24. W = Gr.*Gs((max(j-win,1):min(j+win,height))-j+win+1,(max(i-win,1):min(i+win,width))-i+win+1);
  25. Out(j,i,k)=sum(W(:).*temp(:))/sum(W(:));
  26. end
  27. end
  28. end
  29. figure,imshow(Out),title('双边滤波');
  30. end
  31. %3.皮肤区域分割
  32. %3.1基于RGB空间的非肤色像素初步过滤
  33. function Out=FirstFilter(In)
  34. Out=In;
  35. [height,width,c] = size(In);
  36. IR=In(:,:,1); IG=In(:,:,2);IB=In(:,:,3);
  37. for j=1:height
  38. for i=1:width
  39. if (IR(j,i)<160/255 && IG(j,i)<160/255 && IB(j,i)<160) && (IR(j,i)>IG(j,i) && IG(j,i)>IB(j,i))
  40. Out(j,i,:)=0;
  41. end
  42. if IR(j,i)+IG(j,i)>500/255
  43. Out(j,i,:)=0;
  44. end
  45. if IR(j,i)<70/255 && IG(j,i)<40/255 && IB(j,i)<20/255
  46. Out(j,i,:)=0;
  47. end
  48. end
  49. end
  50. figure,imshow(Out);title('非肤色初步过滤');
  51. end
  52. %3.2基于YCgCr空间范围肤色分割
  53. function Out=SecondFilter(In)
  54. IR=In(:,:,1); IG=In(:,:,2);IB=In(:,:,3);
  55. [height,width,c] = size(In);
  56. Out=zeros(height,width);
  57. for i=1:width
  58. for j=1:height
  59. R=IR(j,i); G=IG(j,i); B=IB(j,i);
  60. Cg=(-81.085)*R+(112)*G+(-30.915)*B+128;
  61. Cr=(112)*R+(-93.786)*G+(-18.214)*B+128;
  62. if Cg>=85 && Cg<=135 && Cr>=-Cg+260 && Cr<=-Cg+280
  63. Out(j,i)=1;
  64. end
  65. end
  66. end
  67. Out=medfilt2(Out,[3 3]);
  68. figure,imshow(Out),title('YCgCr空间范围肤色检测');
  69. end
  70. %4.图像融合
  71. function Out=Fuse(ImageOrigin,DBImage,SkinArea)
  72. Skin=zeros(size(ImageOrigin));
  73. Skin(:,:,1)=SkinArea;
  74. Skin(:,:,2)=SkinArea;
  75. Skin(:,:,3)=SkinArea;
  76. Out=DBImage.*Skin+double(ImageOrigin).*(1-Skin);
  77. figure,imshow(Out);title('肤色与背景图像融合');
  78. end
  79. %5.图像锐化
  80. function Out=Sharp(In)
  81. H=[0 -1 0;-1 4 -1;0 -1 0]; %Laplacian锐化模板
  82. Out(:,:,:)=imfilter(In(:,:,:),H);
  83. Out=Out/3+In;
  84. % imwrite(Out,'man4.jpg');
  85. figure,imshow(Out),title('Laplacia锐化图像');
  86. end
  87. %6.皮肤亮白处理
  88. function Out=MB(SkinBeautify)
  89. %im = imread('face9.jpg');
  90. Out=rgb2ycbcr(SkinBeautify);%将图片的RGB值转换成YCbCr值%
  91. YY=Out(:,:,1);
  92. Cb=Out(:,:,2);
  93. Cr=Out(:,:,3);
  94. [x y z]=size(SkinBeautify);
  95. tst=zeros(x,y);
  96. Mb=mean(mean(Cb));
  97. Mr=mean(mean(Cr));
  98. %计算Cb、Cr的均方差%
  99. Tb = Cb-Mb;
  100. Tr = Cr-Mr;
  101. Db=sum(sum((Tb).*(Tb)))/(x*y);
  102. Dr=sum(sum((Tr).*(Tr)))/(x*y);
  103. %根据阀值的要求提取出near-white区域的像素点%
  104. cnt=1;
  105. for i=1:x
  106. for j=1:y
  107. b1=Cb(i,j)-(Mb+Db*sign(Mb));
  108. b2=Cr(i,j)-(1.5*Mr+Dr*sign(Mr));
  109. if (b1<abs(1.5*Db) && b2<abs(1.5*Dr))
  110. Ciny(cnt)=YY(i,j);
  111. tst(i,j)=YY(i,j);
  112. cnt=cnt+1;
  113. end
  114. end
  115. end
  116. cnt=cnt-1;
  117. iy=sort(Ciny,'descend');%将提取出的像素点从亮度值大的点到小的点依次排列%
  118. nn=round(cnt/10);
  119. Ciny2(1:nn)=iy(1:nn);%提取出near-white区域中10%的亮度值较大的像素点做参考白点%
  120. %提取出参考白点的RGB三信道的值%
  121. mn=min(Ciny2);
  122. for i=1:x
  123. for j=1:y
  124. if tst(i,j)<mn
  125. tst(i,j)=0;
  126. else
  127. tst(i,j)=1;
  128. end
  129. end
  130. end
  131. R=SkinBeautify(:,:,1);
  132. G=SkinBeautify(:,:,2);
  133. B=SkinBeautify(:,:,3);
  134. R=double(R).*tst;
  135. G=double(G).*tst;
  136. B=double(B).*tst;
  137. %计算参考白点的RGB的均值%
  138. Rav=mean(mean(R));
  139. Gav=mean(mean(G));
  140. Bav=mean(mean(B));
  141. Ymax=double(max(max(YY)))*0.15;%计算出图片的亮度的最大值%
  142. %计算出RGB三信道的增益%
  143. Rgain=Ymax/Rav;
  144. Ggain=Ymax/Gav;
  145. Bgain=Ymax/Bav;
  146. %通过增益调整图片的RGB三信道%
  147. SkinBeautify(:,:,1)=SkinBeautify(:,:,1)*Rgain;
  148. SkinBeautify(:,:,2)=SkinBeautify(:,:,2)*Ggain;
  149. SkinBeautify(:,:,3)=SkinBeautify(:,:,3)*Bgain;
  150. figure,imshow(Out),title('皮肤美化');
  151. end

7、素描效果

参考:

Matlab编程实现图像滤镜效果(浮雕、怀旧色、连环画、羽化、素描、强光等)_jstlovely的博客-CSDN博客_matlab滤镜代码

 

  1. img=imread('lisa.jpg')
  2. img9_gray0= rgb2gray(img);%转为灰度图
  3. img9_gray1 = 255 - img9_gray0;%反色
  4. w = fspecial('gaussian',[5 5],5);%构造一个高斯滤波器
  5. img9_gray2 = imfilter(img9_gray1,w);%高斯模糊
  6. [m,n]=size(img9_gray0)
  7. %模糊后的图像叠加模式选择颜色减淡效果。
  8. for i=1:m
  9. for j=1:n
  10. img9(i,j) = uint8(min(uint16(img9_gray0(i,j)) + (uint16(img9_gray0(i,j))*uint16(img9_gray2(i,j))) / (255 - uint16(img9_gray2(i,j))),255));
  11. end
  12. end
  13. subplot(1,2,1),imshow(img );
  14. subplot(1,2,2),imshow(img9 );

8、羽化

  1. img=imread('lisa.jpg')
  2. img9_gray0= rgb2gray(img);%转为灰度图
  3. [m,n]=size(img9_gray0)
  4. mSize = 0.6;
  5. centerX = n/2;
  6. centerY = m/2;
  7. diff = (centerX*centerX + centerY*centerY) * mSize;
  8. if n>m
  9. ratio = m/n;
  10. else
  11. ratio = n/m;
  12. end
  13. for i=1:m
  14. for j=1:n
  15. dx = centerX - j;
  16. dy = centerY - i;
  17. if n>m
  18. dx = dx * ratio;
  19. else
  20. dy = dy * ratio;
  21. end
  22. dstSq = dx * dx + dy * dy;
  23. V = 255 * dstSq / diff;
  24. img8(i,j,1) = img(i,j,1) + V;
  25. img8(i,j,2) = img(i,j,2) + V;
  26. img8(i,j,3) = img(i,j,3) + V;
  27. end
  28. end
  29. subplot(1,2,1),imshow(img );
  30. subplot(1,2,2),imshow(img8 );

9、浮雕

  1. img=imread('lisa.jpg')
  2. [m,n,d]=size(img)
  3. for k=1:d % d--通道数
  4. for i=2:m-1 % m--行数
  5. for j=2:n-1 % n--列数
  6. img2(i,j,k) = img(i+1,j+1,k)-img(i-1,j-1,k)+128;%浮雕效果算法
  7. if img2(i,j,k)>255
  8. img2(i,j,k) = 255; %像素值超过255的都置为255
  9. elseif img2(i,j,k)<0
  10. img2(i,j,k) = 0; %像素值小于0的都置为0
  11. else
  12. img2(i,j,k) = img2(i,j,k);
  13. end
  14. end
  15. end
  16. end
  17. subplot(1,2,1),imshow(img );
  18. subplot(1,2,2),imshow(img2 );

10、油画效果

  1. Image=imread('lisa.jpg')
  2. se=strel('ball',5,5);%选取球形结构元素
  3. result2=imerode(Image,se); %腐蚀灰度图像
  4. imshow(Image);title('原始灰度图像');
  5. figure,imshow(result2);title('腐蚀后的图像');

 11、图像合成

注意两张图片需要尺寸一致

  1. img = imread('lisa.jpg');
  2. background = imread('tina.jpg');
  3. img2=0.6*img+0.4*background;
  4. subplot(1,3,1),imshow(img ),title('前景');
  5. subplot(1,3,2),imshow(background ),title('背景');
  6. subplot(1,3,3),imshow(img2),title('合成的图片');

12、贴纸

矢量图库iconfont-阿里巴巴矢量图标库

png 显示 全黑 

13、相框

14、特征点定位

15、瘦脸

16、涂口红

17、底片效果

  1. I=imread('lisa.jpg');
  2. subplot(1,2,1),imshow(I),title("原图像");
  3. J=253-I;
  4. subplot(1,2,2),imshow(im2gray(J)),title("底片效果");

18、GUI界面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/257567
推荐阅读
相关标签
  

闽ICP备14008679号