数字图像处理之matlab大作业:美图秀秀

1、放大缩小

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

数字图像处理之matlab大作业:美图秀秀_第1张图片

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

2、翻转和旋转

数字图像处理之matlab大作业:美图秀秀_第2张图片

Image2=imread('lisa.jpg');
HImage=flipdim(Image2,2);
VImage=flipdim(Image2,1);
CImage=flipdim(HImage,1);
subplot(221),imshow(Image2);
subplot(222),imshow(HImage);
subplot(223),imshow(VImage);
subplot(224),imshow(CImage);

数字图像处理之matlab大作业:美图秀秀_第3张图片  

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

3、图像剪切 

数字图像处理之matlab大作业:美图秀秀_第4张图片

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

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

数字图像处理之matlab大作业:美图秀秀_第5张图片

picture=imread("lisa.jpg");
picture1=histeq(picture);
figure;
subplot(121);
imshow(picture);
subplot(122);
imshow(picture1);
figure;
subplot(121);
imhist(picture);
subplot(122);
imhist(picture1);

5、磨皮

数字图像处理之matlab大作业:美图秀秀_第6张图片

noisyImg=imread('tina.jpg');
r=10;
level=5;
sigma=10+level^2;
dnImg=denoiseBasedLocalStat(noisyImg,r,sigma);
figure,imshow(noisyImg);
figure,imshow(dnImg);
mask=faceMask(noisyImg,5);
figure,imshow(mask);
w=0.5;
mergedImg=uint8(double(noisyImg).*(1-w*mask)+w*double(dnImg).*mask);
figure,imshow(mergedImg);
function dnImg = denoiseBasedLocalStat(img,r,sigma)
% 基于局部统计信息的图像滤波去噪
if size(img,3)==1
    img=double(img);
    dnImg=denoiseBasedLocalStat_gray(img,r,sigma);
    dnImg=uint8(dnImg);
end
if size(img,3)==3
    ycc=rgb2ycbcr(img);
    y=ycc(:,:,1);
    dnY=denoiseBasedLocalStat_gray(double(y),r,sigma);
    ycc(:,:,1)=uint8(dnY);
    dnImg=ycbcr2rgb(ycc);
end
end
 
function dnImg=denoiseBasedLocalStat_gray(img,r,sigma)
% 基于局部统计信息的灰度图像滤波去噪
dnImg=zeros(size(img));
img=double(img);
paddedImg=padarray(img,[r,r],'symmetric','both');
[Yim,YYim]=intergalMap(paddedImg);
for i=r+1:size(paddedImg,1)-r
    for j=r+1:size(paddedImg,2)-r
        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);
        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);
        num=(2*r+1)^2;
        avg=rectSum/num;
        var=rectSquareSum/num-avg^2;
        k=var/(var+sigma^2);
        dnImg(i-r,j-r)=(1-k)*avg+k*img(i-r,j-r);
    end
end
end
 
function [Yim,YYim]=intergalMap(img)
% 生成积分图
paddedImg=padarray(img,[1 1],0,'pre');
Yim=zeros(size(paddedImg));
YYim=Yim;
for i=2:size(paddedImg,1)
    for j=2:size(paddedImg,2)
        Yim(i,j)=Yim(i,j-1)+Yim(i-1,j)-Yim(i-1,j-1)+paddedImg(i,j);
        YYim(i,j)=YYim(i,j-1)+YYim(i-1,j)-YYim(i-1,j-1)+paddedImg(i,j)^2;
    end
end
end
 
function mask=faceMask(img,r)
% 人脸掩模
mask=zeros(size(img,1),size(img,2));
mask(img(:,:,1)>20 & img(:,:,2)>40 & img(:,:,3)>50)=1;
mask=myBoxFilt(mask,r);
mask=mask(:,:,ones(1,3));
end
 
function fImg=myBoxFilt(img,r)
% 盒式滤波
paddedImg=padarray(img,[r r],'symmetric','both');
[Yim,~]=intergalMap(paddedImg);
for i=r+1:size(paddedImg,1)-r
    for j=r+1:size(paddedImg,2)-r
        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);
        avg=rectSum/(2*r+1)^2;
        fImg(i-r,j-r)=avg;
    end
end
end

6、美白

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

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

clear,clc,close all;
ImageOrigin=im2double(imread('tina.jpg'));
figure,imshow(ImageOrigin),title('原图');
DBImage=DBfilt(ImageOrigin);                 %双边滤波

SkinImage1=FirstFilter(ImageOrigin);            %%初步过滤
SkinArea=SecondFilter(SkinImage1);           %%YCgCr空间范围肤色检测

SkinFuse=Fuse(ImageOrigin,DBImage,SkinArea);%图像融合
SkinBeautify=Sharp(SkinFuse);               %图像锐化

Meibai=MB(SkinBeautify);

%2.图像平滑
function Out=DBfilt(In)
    [height,width,c] = size(In); 
    win=15;       % 定义双边滤波窗口宽度  
    sigma_s=6; sigma_r=0.1; % 双边滤波的两个标准差参数  
    [X,Y] = meshgrid(-win:win,-win:win); 
    Gs = exp(-(X.^2+Y.^2)/(2*sigma_s^2));%计算邻域内的空间权值    
    Out=zeros(height,width,c); 
    for k=1:c
        for j=1:height    
            for i=1:width  
                temp=In(max(j-win,1):min(j+win,height),max(i-win,1):min(i+win,width),k);
                Gr = exp(-(temp-In(j,i,k)).^2/(2*sigma_r^2));%计算灰度邻近权值        
                % W为空间权值Gs和灰度权值Gr的乘积       
                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);      
                Out(j,i,k)=sum(W(:).*temp(:))/sum(W(:));            
            end
        end  
    end
    figure,imshow(Out),title('双边滤波');
end
%3.皮肤区域分割
%3.1基于RGB空间的非肤色像素初步过滤
function Out=FirstFilter(In)
    Out=In;
    [height,width,c] = size(In); 
    IR=In(:,:,1); IG=In(:,:,2);IB=In(:,:,3);
    for j=1:height
        for i=1:width
            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))
                Out(j,i,:)=0;
            end
            if IR(j,i)+IG(j,i)>500/255
                Out(j,i,:)=0;
            end
            if IR(j,i)<70/255 && IG(j,i)<40/255 && IB(j,i)<20/255
                Out(j,i,:)=0;
            end
        end
    end
 
    figure,imshow(Out);title('非肤色初步过滤'); 
end
%3.2基于YCgCr空间范围肤色分割
function Out=SecondFilter(In)
    IR=In(:,:,1); IG=In(:,:,2);IB=In(:,:,3);       
    [height,width,c] = size(In);
    Out=zeros(height,width);
    for i=1:width
        for j=1:height  
           R=IR(j,i); G=IG(j,i); B=IB(j,i);       
           Cg=(-81.085)*R+(112)*G+(-30.915)*B+128;  
           Cr=(112)*R+(-93.786)*G+(-18.214)*B+128;         
           if Cg>=85 && Cg<=135 && Cr>=-Cg+260 && Cr<=-Cg+280       
               Out(j,i)=1;          
           end
        end
    end
    Out=medfilt2(Out,[3 3]);
    
    figure,imshow(Out),title('YCgCr空间范围肤色检测');    
end
%4.图像融合
function Out=Fuse(ImageOrigin,DBImage,SkinArea)
    Skin=zeros(size(ImageOrigin));
    Skin(:,:,1)=SkinArea;   
    Skin(:,:,2)=SkinArea;  
    Skin(:,:,3)=SkinArea;
    Out=DBImage.*Skin+double(ImageOrigin).*(1-Skin);
    
    figure,imshow(Out);title('肤色与背景图像融合');
end
%5.图像锐化
function Out=Sharp(In)
    H=[0 -1 0;-1 4 -1;0 -1 0]; %Laplacian锐化模板
    Out(:,:,:)=imfilter(In(:,:,:),H); 
    Out=Out/3+In;
%     imwrite(Out,'man4.jpg');
    figure,imshow(Out),title('Laplacia锐化图像');
end
%6.皮肤亮白处理
function Out=MB(SkinBeautify)
    %im = imread('face9.jpg');
Out=rgb2ycbcr(SkinBeautify);%将图片的RGB值转换成YCbCr值%
YY=Out(:,:,1);
Cb=Out(:,:,2);
Cr=Out(:,:,3);
[x y z]=size(SkinBeautify);
tst=zeros(x,y);
Mb=mean(mean(Cb));
Mr=mean(mean(Cr));
%计算Cb、Cr的均方差%
Tb = Cb-Mb;
Tr = Cr-Mr;
Db=sum(sum((Tb).*(Tb)))/(x*y);
Dr=sum(sum((Tr).*(Tr)))/(x*y);
%根据阀值的要求提取出near-white区域的像素点%
cnt=1;    
for i=1:x
    for j=1:y
        b1=Cb(i,j)-(Mb+Db*sign(Mb));
        b2=Cr(i,j)-(1.5*Mr+Dr*sign(Mr));
        if (b1

7、素描效果

参考:

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

 数字图像处理之matlab大作业:美图秀秀_第7张图片

img=imread('lisa.jpg')
img9_gray0= rgb2gray(img);%转为灰度图
img9_gray1 = 255 - img9_gray0;%反色
w = fspecial('gaussian',[5 5],5);%构造一个高斯滤波器
img9_gray2 = imfilter(img9_gray1,w);%高斯模糊
[m,n]=size(img9_gray0)
%模糊后的图像叠加模式选择颜色减淡效果。
for i=1:m
    for j=1:n
        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));
    end
end
subplot(1,2,1),imshow(img );
subplot(1,2,2),imshow(img9 );

8、羽化

数字图像处理之matlab大作业:美图秀秀_第8张图片

img=imread('lisa.jpg')
img9_gray0= rgb2gray(img);%转为灰度图
[m,n]=size(img9_gray0)
mSize = 0.6;
centerX = n/2;
centerY = m/2;
diff = (centerX*centerX + centerY*centerY) * mSize;
if n>m
    ratio = m/n;
else
    ratio = n/m;
end
for i=1:m
    for j=1:n
        dx = centerX - j;
        dy = centerY - i;
        if n>m
            dx = dx * ratio;
        else
            dy = dy * ratio;
        end
        dstSq = dx * dx + dy * dy;
        
        V = 255 * dstSq / diff;
        img8(i,j,1) = img(i,j,1) + V;
        img8(i,j,2) = img(i,j,2) + V;
        img8(i,j,3) = img(i,j,3) + V;
    end
end
subplot(1,2,1),imshow(img );
subplot(1,2,2),imshow(img8 );

9、浮雕

数字图像处理之matlab大作业:美图秀秀_第9张图片

img=imread('lisa.jpg')
[m,n,d]=size(img)
for k=1:d    % d--通道数
    for i=2:m-1 % m--行数
        for j=2:n-1 % n--列数
            img2(i,j,k) = img(i+1,j+1,k)-img(i-1,j-1,k)+128;%浮雕效果算法
            if img2(i,j,k)>255
                img2(i,j,k) = 255; %像素值超过255的都置为255
            elseif img2(i,j,k)<0
                    img2(i,j,k) = 0; %像素值小于0的都置为0
            else
                    img2(i,j,k) = img2(i,j,k);
            end
        end
    end
end
subplot(1,2,1),imshow(img );
subplot(1,2,2),imshow(img2 );

10、油画效果

数字图像处理之matlab大作业:美图秀秀_第10张图片

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

 11、图像合成

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

数字图像处理之matlab大作业:美图秀秀_第11张图片

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

12、贴纸

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

png 显示 全黑 

13、相框

14、特征点定位

15、瘦脸

16、涂口红

17、底片效果

数字图像处理之matlab大作业:美图秀秀_第12张图片

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

18、GUI界面

你可能感兴趣的:(数字图像处理之matlab,matlab,开发语言,算法)