赞
踩
1、熟悉并掌握MATLAB工具的使用;
2、实现图像的读取、显示、存储、平移、镜像、放大、缩小及旋转操作;
3、掌握常用的插值方法,并了解其优缺点。
读入一幅RGB图像,变换为灰度图像和二值图像,并在同一个窗口内分别显示RGB图像和灰度图像,注上文字标题,并将结果以文件形式存到磁盘上。
I = imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg');
subplot(3,1,1);
imshow(I);
title('原图像');
i = rgb2gray(I);
subplot(3,1,2);
imshow(i);
title('灰度图像');
imwrite(i,'D:\Backup\Documents\My Pictures\Saved Pictures\picture2.jpg');
a = im2bw(I,0.5);
subplot(3,1,3);
imshow(a);
title('二值图像');
imwrite(a,'D:\Backup\Documents\My Pictures\Saved Pictures\picture3.jpg');
对图像执行平移、镜像(水平镜像、垂直镜像)放大、缩小及旋转操作,其中放大、旋转操作分别采用最近邻插值及双线性插值方法实现,要求根据算法自己编写代码实现,并分析两种插值方法的优缺点。
I=imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg'); subplot(2,1,1); imshow(I); title('原图像'); [M,N,C]=size(I); g=zeros(M,N,C); for color=1:3 for i=1:M for j=1:N newx=i+30; newy=j+30; if((newx<=M)&&(newy<=N)) g(newx,newy,color)=I(i,j,color); end end end end subplot(2,1,2); imshow(uint8(g)); title('平移');
I=imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg'); subplot(2,1,1); imshow(I); title('原图像'); [M,N,C]=size(I); g=zeros(M,N,C); for color=1:3 for i=1:M for j=1:N-1 g(i,j,color)=I(i,N-j,color); end end end subplot(2,1,2); imshow(uint8(g)); title('水平镜像');
I=imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg'); subplot(2,1,1); imshow(I); title('原图像'); [M,N,C]=size(I); g=zeros(M,N,C); for color=1:3 for i=1:M-1 for j=1:N g(i,j,color)=I(M-i,j,color); end end end subplot(2,1,2); imshow(uint8(g)); title(‘垂直镜像’);
I=imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg');
figure;
imshow(I);
title('原图');
g=imresize(I,2);
figure;
imshow(g);
title('放大');
I=imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg');
figure;
imshow(I);
title('原图');
g=imresize(I,0.5);
figure;
imshow(g);
title('缩放');
im = imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg'); % 读入图片 subplot(2,1,1); imshow(im); title('原图'); % 求出旋转矩阵 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 = round(R*(p-c2)+c1); % 逆向进行像素查找 if (pp(1) >= 1 && pp(1) <= h && pp(2) >= 1 && pp(2) <= w) im2(i, j, k) = im(pp(1), pp(2), k); end end end end % 显示图像 subplot(2,1,2); imshow(im2); title('旋转');
im = imread('D:\Backup\Documents\My Pictures\Saved Pictures\picture1.jpg'); subplot(2,1,1); imshow(im); title('原图'); % 求出旋转矩阵 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 % 显示图像 subplot(2,1,2); imshow(im2); title('线性旋转');
有参考许多csdn文章。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。