您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 医学现状与发展 > 图像处理matlab程序实例
程序实例1旋转:x=imread('d:\MATLAB7\work\flower.jpg');y=imrotate(x,200,'bilinear','crop');subplot(1,2,1);imshow(x);subplot(1,2,2);imshow(y)2.图像的rgbclear[x,map]=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shh1.jpg');y=x(90:95,90:95);imshow(y)R=x(90:95,90:95,1);G=x(90:95,90:95,2);B=x(90:95,90:95,3);R,G,B3.加法运算clearI=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shh3.jpg');J=imnoise(I,'gaussian',0,0.02);%向图片加入高斯噪声subplot(1,2,1),imshow(I);%显示图片subplot(1,2,2),imshow(J);K=zeros(242,308);%产生全零的矩阵,大小与图片的大小一样fori=1:100%循环100加入噪声J=imnoise(I,'gaussian',0,0.02);J1=im2double(J);K=K+J1;endK=K/100;figure,imshow(K);save4.减法clearI=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao.jpg');J=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao1.jpg');K=imsubtract(I,J);%实现两幅图相减K1=255-K;%将图片求反显示figure;imshow(I);title('有噪声的图');figure;imshow(J);title('原图');figure;imshow(K1);title('提取的噪声');save5.图像的乘法H=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao.jpg');I=immultiply(H,1.2);将此图片乘以1.2J=immultiply(H,2);subplot(1,3,1),imshow(H);title('原图');subplot(1,3,2),imshow(I);title('·放大1.2');subplot(1,3,3),imshow(J);title('放大2倍');6除法运算moon=imread('moon.tif');I=double(moon);J=I*0.43+90;K=I*0.1+90;L=I*0.01+90;moon2=uint8(J);moon3=uint8(K);moon4=uint8(L);J=imdivide(moon,moon2);K=imdivide(moon,moon3);L=imdivide(moon,moon4);subplot(2,2,1),imshow(moon);subplot(2,2,2),imshow(J,[]);subplot(2,2,3),imshow(K,[]);subplot(2,2,4),imshow(L,[]);7.图像的缩放clearI=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao1.jpg');figure,imshow(I);scale=0.5;J=imresize(I,scale);figure,imshow(J);8.图像的旋转(1)I=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao1.jpg');figure,imshow(I);theta=30;K=imrotate(I,theta);%Tryvaryingtheangle,theta.figure,imshow(K)(2)clearI=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao1.jpg');J=imrotate(I,60,'bilinear');K=imrotate(I,60,'bilinear','crop');subplot(1,3,1),imshow(I);subplot(1,3,2),imshow(J);subplot(1,3,3),imshow(K);1.傅里叶变换clearI=zeros(256,256);I(28:228,108:148)=1;imshow(I)求原始图像的傅里叶频谱J=fft2(I);F=abs(J);J1=fftshift(F);figureimshow(J1,[5,50]clearI=zeros(256,256);I(28:228,108:148)=1;%对原始图像进行旋转J=imrotate(I,315,’bilinear’,’crop’);figureimshow(J)求原始图像的傅里叶频谱J1=fft2(J);F=abs(J1);J2=fftshift(F);figureimshow(J2,[5,50])1.图象的FFT变换clc;I=imread('005.bmp');subplot(1,2,1)imshow(I);title('原图');subplot(1,2,2)imhist(I);title('直方图');colorbar;J=fft2(I);figure;subplot(1,2,1)imshow(J);title('FFT变换结果');subplot(1,2,2)K=fftshift(J);imshow(K);title('零点平移');figure;imshow(log(abs(K)),[]),colormap(jet(64)),colorbar;title('系数分布图');2.图象的DCT变换RGB=imread('005.bmp');figure;subplot(1,2,1)imshow(RGB);title('彩色原图');a=rgb2gray(RGB);subplot(1,2,2)imshow(a);title('灰度图');figure;b=dct2(a);imshow(log(abs(b)),[]),colormap(jet(64)),colorbar;title('DCT变换结果');figure;b(abs(b)10)=0;%idctc=idct2(b)/255;imshow(c);title('IDCT变换结果');3.小波变换clccloseallcleara=imread('005.bmp');subplot(1,2,1);imshow(a);title('原始图象');I=rgb2gray(a);subplot(1,2,2);imshow(I);title('原始图象的灰度图');%进行二维小波变换[a,b]=wavedec2(I,2,'bior3.7');%提取各层低频信息figure;c=appcoef2(a,b,'bior3.7',1);subplot(1,2,1);imshow(c,[]);title('一层小波变换结果');d=appcoef2(a,b,'bior3.7',2);subplot(1,2,2);imshow(d,[]);title('二层小波变换结果');2.尺度变换clc;clear;I=zeros(256,256);I(8:248,110:136)=5;figure(1)imshow(I)a=0.1;J3=fft2(I);F2=abs(J3);J4=fftshift(F2);figure(2)imshow(J4,[5,30])%乘以比例尺度fori=1:256forj=1:256I(i,j)=I(i,j)*a;endendJ2=fft2(I);F1=abs(J2);J3=fftshift(F1);figure(3)imshow(J3,[5,30])3傅里叶变换实例(1)乘以指数将图像亮度整体变暗,并求其中心移到零点的频谱图clearI=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shh3.jpg');figure(1);imshow(I)P=I*exp(-1);figure(2);imshow(P)P1=fftshift(fft2(P));figure(3);imshow(log(abs(P1)),[8,10])(2)加入高斯噪声求频谱图I=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao1.jpg');I=imnoise(I,'gaussian',0,0.01);figure;imshow(I);P=fftshift(fft2(I));figure;imshow(log(abs(P),[8,10])(3)求相位谱和幅值谱I=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao.jpg');figure(1)imshow(real(I));I=I(:,:,3);fftI=fft2(I);sfftI=fftshift(fftI);RRfdp1=real(sfftI);IIfdp1=imag(sfftI);a=sqrt(RRfdp1.^2+IIfdp1.^2);a=(a-min(min(a)))/(max(max(a))-min(min(a)))*225;figure(2)imshow(real(a));b=angle(fftI);figure(3)imshow(real(b));theta=30;RR1=a*cos(theta);II1=a*sin(theta);fftI1=RR1+i.*II1;C=ifft2(fftI1)*255;figure(4)imshow(real(C));MM=150;RR2=MM*cos(angle(fftI));II2=MM*sin(angle(fftI));fftI2=RR2+i.*II2;D=ifft2(fftI2);fogure(5)imshow(real(D));图像增强1.反转变换:img1=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\shao.jpg');figure,imshow(img1);title('originalimage');img2=imcomplement(img1);figure,imshow(img2);title('negativemage');2.基于直方图处理的图像增强I=imread('D:\ProgramFiles\MATLAB\R2012a\bin\shaohaihe\zhinv.jpg');I=rgb2gray(I);K=16;H=histeq(I,K);figure,subplot(2,2,1),imshow(I,[]);subplot(2,2,2),imshow(H,[]);subplot(2,2,3),hist(double(I),16);subplot(2,2,4),hist(double(H),16);(2)在matlab环境中,程序首先读取图像,然后调用直方图函数,设置相关参数,再输出处理后的图像。I=imread('cameraman.tif');%读取图像subplot(1,2,1),imshow(I)%输出图像title('原始图像')%在原始图像中加标题subplot(1,2,2),imhist(I)%输出原图直方图title('原始图像直方图')%在原图直方图上加标题(3)灰度均衡
本文标题:图像处理matlab程序实例
链接地址:https://www.777doc.com/doc-2031012 .html