当前位置:文档之家› 数字图像处理程序若干(matlab)

数字图像处理程序若干(matlab)

主程序clear allclose allclc!echo 本次图像处理的菜单如下:!!echo 1,imgsharping!!echo 2,imgnegative!!echo 3,imgsmoothing!!echo 4,imgsubtracting!!echo 5,medianfilting!!echo 6,contraststrength!!echo 7,lineartransform!!echo 8,imgfilp!!echo 9,imgindextransform!iptsetpref('ImshowBorder', 'tight')a=1;img = imread('C:\Users\huanhuan\Desktop\司马.jpg');figure(1), imshow(img);while(a==1)choose=input('please enter your choosing ranging from 1 to 9: '); switch choosecase 1,imgsharping(img);case 2,imgnegative(img);case 3,imgsmoothing(img);case 4,imgsubstract(img);case 5,medianfilting(img);case 6,contraststrength();case 7,lineartransform();case 8,imgfilp(img);case 9,imgindextransform();otherwisedisp('Unknown method.');enda=input('continue(1) or quit(2)');end函数1.Imgsharpingfunction imgsharping(img)h = [0 -1 0; -1 5 -1; 0 -1 0]; % Laplacian filterimgHSV = rgb2hsv(img);v = imgHSV(:,:,3);v = imfilter(v,h);imgHSV(:,:,3) = v;img3 = hsv2rgb(imgHSV);figure; imshow(img3);2.Imgnegativefunction imgnegative(img)figureimg2 = imcomplement(img);imshow(img2);title('negative image');zoom on3,imgsmoothingfunction imgsmoothing(img)img2 = uint8(zeros(size(img)));img2(:,:,1) = imfilter(img(:,:,1), fspecial('average',5)); img2(:,:,2) = imfilter(img(:,:,2), fspecial('average',5)); img2(:,:,3) = imfilter(img(:,:,3), fspecial('average',5)); figure; imshow(img2);4,imgsubtractingfunction imgsubstract(img)level = graythresh(img);BW= im2bw(img,level);figure(2), imshow(BW)5,medianfiltingfunction medianfilting(I)J = imnoise(I,'salt & pepper',0.02);imshow(I)figure, imshow(J)K = filter2(fspecial('average',3),J)/255;L = medfilt2(J,[3 3]);figure, imshow(K)figure, imshow(L)6,contraststrengthfunction contraststrength()clear;zx=imread('C:\Users\huanhuan\Desktop\huan.jpg'); imshow(zx);a=double(zx);for i=1:256for j=1:256b(i,j)=4*a(i,j);if (b(i,j)>=255)b(i,j)=255;endendendb=uint8(b);figure(2);imshow(b);7,lineartransformfunction lineartransform()iptsetpref('ImshowBorder', 'tight')img = imread('C:\Users\huanhuan\Desktop\huan.jpg'); figure; imshow(img);[r,c,p]=size(img);img=double(img);img=img/256;for a=1:pfor b=1:rfor d=1:cif(img(b,d,a)<0.2)img(b,d,a)=4*img(b,d,a);endif(img(b,d,a)>0.2)if(img(b,d,a)<1)img(b,d,a)=0.25*img(b,d,a)+0.75;endendendendendfigure;imshow(img);8,imgfilpfunction imgfilp(img)[r,c,p]=size(img);img1=zeros(size(img));for a=1:pfor b=1:rfor d=1:cimg1(b,d,a)=100-1-img(b,d,a);endendendfigure;imshow(img1);9,imgindextransformfunction imgindextransform()iptsetpref('ImshowBorder', 'tight')img = imread('C:\Users\huanhuan\Desktop\sl.jpg'); img=rgb2gray(img);figure;imshow(img);[h,c]=size(img);img=double(img);for i=1:hfor j=1:cimg1(i,j)=0.01*power(img(i,j),0.9);img2(i,j)=0.01*power(img(i,j),1.2);endendfigure;imshow(img1);figure;imshow(img2);10,zhifangtufunction zhifangtu()I = imread('C:\Users\huanhuan\Desktop\lena256.bmp'); subplot(3,2,1);imshow(I);title('原图');subplot(3,2,2);imhist(I);title('原图直方图');[m,n] = size(I);hf = zeros(1,256);pa = zeros(1,256);for i = 1:mfor j = 1:nhf(I(i,j))=hf(I(i,j))+1;%统计各灰度像素个数endendpa(1)= 0;for k = 2:256pf(k) =hf(k)/(m*n);%算灰度分布概率pa(k) =pa(k-1)+pf(k);%灰度累计分布概率endG = zeros(size(I));for i = 1:mfor j = 1:nG(i,j)=255*pa(I(i,j)); %将原来f的灰度值映射到新的灰度值g(i,j) = 255*pa(f(i,j))endendH = mat2gray(G);K = uint8(G);subplot(3,2,3);imshow(K);title('用课本方法得到的均衡图');subplot(3,2,4);imhist(K);title('用课本方法的直方图');subplot(3,2,5);imshow(histeq(I));title('用histep函数得到的均衡图'); subplot(3,2,6);imhist(histeq(I));title('用histep函数得到的直方图');11,facedetectclear all;close all;iptsetpref('ImshowBorder', 'tight')img = imread('C:\Users\huanhuan\Desktop\宇多田光.bmp');figure; imshow(img);R = img(:,:,1);G = img(:,:,2);B = img(:,:,3);faceRgn1 = (R>95) & (G>40) & (B>20) & max(img, [], 3)-min(img, [], 3)>15 & abs(R-G)>15 & R>B;figure; imshow(faceRgn1);程序结果图Imgsharping锐化Imgnegative取反Imgsmoothing图像平均Imgsubtracting边缘提取MedianfiltingContraststrength对比度增强Lineartransform(分段线性)Imgfilp图像翻转Imgindextransform指数变换原图用对数处理后Facedetect 原图识别后。

相关主题