MATLAB--图像处理

1.对tu1、tu2、tu3进行图像合成,利用几何变换、算术运算生成合成图像。(要求合成图像的图书馆及湖面合理拼接,天鹅处于湖中)

x=imread('D:\桌面\新建文件夹\tu1.jpg');
y=imread('D:\桌面\新建文件夹\tu2.jpg');
z=imread('D:\桌面\新建文件夹\tu3.jpg');
%对三张图进行输入
[m1,n1,w1]=size(x);
x1=x(:,1:n1/5*3,:);%取tu1的部分
a=[x1 y];
figure(1);imshow(a);%tu1部分tu2叠加
z1=imresize(z,0.2);%将图三缩小
a1=a(201:275,243:342,:);%取合成后的图部分
k=imlincomb(0.1,a1,0.9,z1);%图片叠加
a(201:275,243:342,:)=k;
figure(1);imshow(a);

MATLAB--图像处理_第1张图片

2. 利用色彩技术给tu4添加滤镜,实现碧绿(Aqua)、棕褐色(Sepia)、暗调(Darkness)的图片效果。

%碧绿(Aqua)
clear all;close all;clc;
img=imread('D:\桌面\新建文件夹\tu4.jpg');
[h w k]=size(img);
imshow(img);
R=double(img(:,:,1));
G=double(img(:,:,2));
B=double(img(:,:,3));
newR = (G-B).*(G-B)/128;
newG = (R-B).*(R-B)/128;
newB = (R-G).* (R-G)/128;
randR=rand()*0.5+0.5;
randG=rand()*0.5+0.5;
randB=rand()*0.5+0.5;
imgn=zeros(h,w,k);
imgn(:,:,1)=randR*newR+(1-randR)*R;
imgn(:,:,2)=randG*newG+(1-randG)*G;
imgn(:,:,3)=randB*newB+(1-randB)*B;
figure;imshow(uint8(imgn));

%棕褐色(Sepia)
clear all;close all;clc;
img=imread('D:\桌面\新建文件夹\tu4.jpg');
[h w k]=size(img);
imshow(img);
R=double(img(:,:,1));
G=double(img(:,:,2));
B=double(img(:,:,3));
rR=R*0.393+G*0.769+B*0.198;
rG=R*0.349+G*0.686+B*0.168;
rB=R*0.272+G*0.534+B*0.131;
randR=rand()*0.5+0.5;
randG=rand()*0.5+0.5;
randB=rand()*0.5+0.5;
imgn=zeros(h,w,k);
imgn(:,:,1)=randR*rR+(1-randR)*R;
imgn(:,:,2)=randG*rG+(1-randG)*G;
imgn(:,:,3)=randB*rB+(1-randB)*B;
figure;imshow(uint8(imgn));

%暗调(Darkness)
clear all;close all;clc;
img=imread('D:\桌面\新建文件夹\tu4.jpg');
[h w k]=size(img);
imshow(img);
R=double(img(:,:,1));
G=double(img(:,:,2));
B=double(img(:,:,3));
newR = (R .* R / 255);
newG = (G .* G / 255);
newB = (B .* B / 255);
randR=rand()*0.5+0.5;
randG=rand()*0.5+0.5;
randB=rand()*0.5+0.5;
imgn=zeros(h,w,k);
imgn(:,:,1)=randR*newR+(1-randR)*R;
imgn(:,:,2)=randG*newG+(1-randG)*G;
imgn(:,:,3)=randB*newB+(1-randB)*B;
figure;imshow(uint8(imgn));   %

3.将tu4图像分别进行错切、缩放、镜像、旋转(参数可调),并进行图像显示与分析。

%错切
im=(imread('D:\桌面\新建文件夹\tu4.jpg'));
im1=rgb2gray(im);
figure,imshow(im1);
[row,col]=size(im1); %获取行数 和 列数
%图像的水平错切
G=zeros(row,col);
a=pi/6; %水平错切30度
b=tan(a);
for m=1:row
for n=1:col
G(round(m+b*n),n)=im1(m,n);
end
end
figure,imshow(uint8(G));
%图像的垂直错切
G=zeros(row,col);
a=pi/6; %水平错切30度
b=tan(a);
for m=1:row
for n=1:col
G(n,round(m+b*n))=im1(m,n);
end
end
figure,imshow(uint8(G));

%缩放
imagedata = imread('D:\桌面\新建文件夹\tu4.jpg');                      
imageDeel = imresize(imagedata,[200 300]);                     
imshow(imageDeel); 


%镜像
img = imread('D:\桌面\新建文件夹\tu4.jpg');
img = im2double(img);
subplot(1,2,1);
imshow(img);
[M,N,Z]=size(img);g=zeros(M,N,Z);
for i=1:M
    for j=1:N
        for s=1:Z
            g(i,j,s) = img(i,N-j+1,s);
        end       
    end
end
subplot(1,2,2);
imshow(g);

%旋转
% 读入图片
im = imread('D:\桌面\新建文件夹\tu4.jpg');
 
% 求出旋转矩阵
a = 30 / 180 * pi;
R = [cos(a), -sin(a); sin(a), cos(a)];
R = R'; % 求出旋转矩阵的逆矩阵进行逆向查找
% 计算原图大小
sz = size(im);
h = sz(1);
w = sz(2);
ch = sz(3);
c1 = [h; w] / 2;
% 计算显示完整图像需要的画布大小
hh = floor(w*sin(a)+h*cos(a))+1;
ww = floor(w*cos(a)+h*sin(a))+1;
c2 = [hh; ww] / 2;
% 初始化目标画布
im2 = uint8(ones(hh, ww, 3)*128);
for k = 1:ch
    for i = 1:hh
       for j = 1:ww
          p = [i; j];
          pp = (R*(p-c2)+c1);
          mn = floor(pp);
          ab = pp - mn;
          a = ab(1);
          b = ab(2);
          m = mn(1);
          n = mn(2);
          % 线性插值方法
          if (pp(1) >= 2 && pp(1) <= h-1 && pp(2) >= 2 && pp(2) <= w-1)
             im2(i, j, k) = (1-a)*(1-b)*im(m, n, k) + a*(1-b)*im(m+1, n, k)...
                          + (1-a)*b*im(m, n, k)     + a*b*im(m, n, k);
          end
       end
    end
end
% 显示图像
figure;
imshow(im2);     %参数可调,结果不上传显示

4.利用图像滤镜(算法)技术给tu5添加滤镜,实现素描、油画和浮雕的图片效果,多种形式展现图书馆的优美。

% 素描
I=imread('D:\桌面\新建文件夹\tu5.jpg','jpg');
imshow(I);
figure();
info_size=size(I);
height=info_size(1);
width=info_size(2);
N=zeros(height,width);
g=zeros(height,width);
imggray=rgb2gray(I);
out=zeros(height,width);
spec=zeros(height,width,3);
for i=1:height
    for j=1:width
        
        N(i,j)=255-imggray(i,j);
        
    end
end
for i=2:height-1
    for j=2:width-1
        sum=0;
        sum=1*double(N(i-1,j-1))+2*double(N(i-1,j))+1*double(N(i-1,j+1));
        sum=sum+2*double(N(i,j-1))+4*double(N(i,j))+2*double(N(i,j+1));
        sum=sum+1*double(N(i+1,j-1))+2*double(N(i+1,j))+1*double(N(i+1,j+1));
        sum=sum/16;
        g(i,j)=sum;
    end
end
for i=1:height
    for j=1:width
        b=double(g(i,j));
        a=double(imggray(i,j));
        temp=a+a*b/(256-b);
        out(i,j)=uint8(min(temp,255));
        
    end
end
imshow(out/255);

% 油画
imag=imread('D:\桌面\新建文件夹\tu5.jpg','jpg');
size_info=size(imag);
height=size_info(1);
width=size_info(2);
N=2;
out=zeros(height,width,3);
for i=1:height
    for j=1:width
       temp=uint8(rand()*(N^2-1));
       m=temp/N;
       n=mod(temp,N);
       h=mod(double(i-1)+double(m),double(height));
       w=mod(double(j-1)+double(n),double(width));
       if w==0;
           w=width;
       end
        if h==0
            h=height;
        end
       out(i,j,:)=imag(h,w,:);
    end
end
imshow(out/255);

% 浮雕
img=imread('D:\桌面\新建文件夹\tu4.jpg');
size_info=size(img);
height=size_info(1);
width=size_info(2);
spec_img=zeros(height,width,3);
img_temp=rgb2gray(img);
for i=2:height-1
    for j=2:width-1
        spec_img(i,j,:)=double(img(i-1,j-1,:))-double(img(i+1,j+1,:))+128;
    end
end
imshow(spec_img/255);

MATLAB--图像处理_第2张图片MATLAB--图像处理_第3张图片

 MATLAB--图像处理_第4张图片

 MATLAB--图像处理_第5张图片

支持可以关注我哦,持续分享编写的代码。

你可能感兴趣的:(MTLAB,matlab,图像处理,计算机视觉)