数字图像处理实验
个人记录,刚学完数字图像处理,实践一下
1、读入一幅RGB图像,将其变换为灰度图像和二值图像,并在同一个窗口内分别显示原图,灰度图像和二值图像,分别标注图像名称。
clear all img = imread('onion.png'); img_gray = rgb2gray(img); figure; subplot(3,1,1);imshow(img);title('原图'); subplot(3,1,2);imshow(img_gray);title('灰度图'); img_bin = imbinarize(img_gray); subplot(3,1,3);imshow(img_bin);title('二值图');
2、任意读取一幅图像,对该幅图像进行灰度变化,实现图像变亮、变暗和负片效果。在同一个窗口内分别显示原图和相应的处理结果,分别标注图像名称。
clear all img = imread('onion.png'); img=double(img); img_neg = zeros(size(img)); img_up = zeros(size(img)); img_down = zeros(size(img)); img_neg = uint8(255-1*img); % s = L-1-r img_up = uint8(1*img.^(2)); img_down = uint8(1*img.^(0.2)); figure; subplot(4,1,1);imshow(img);title('原图像'); subplot(4,1,2);imshow(img_neg);title('负片图像'); subplot(4,1,3);imshow(img_up);title('变亮图像'); subplot(4,1,4);imshow(img_down);title('变暗图像');
3、读入matlab内置图像pout.tif,将其小于30的灰度值不变,将30-150的灰度值拉伸到30-200,将150-255的灰度值压缩到200-255之间。
也可使用for循环,遍历图像像素,依照变换函数,逐个变换
clear all imga = imread('pout.tif'); img = imga; imga = double(imga); img=double(img); idx1=find(imga>=30 &imga<=150); idx2=find(imga>=150&imga<=255); img(idx1) = (img(idx1)-30 )*17/12 +30; img(idx2) = (img(idx2)-150)*55/105+200; imga = uint8(imga); img=uint8(img); figure; subplot(2,1,1);imshow(imga); subplot(2,1,2);imshow(img);
4、读入lena图像,为其随机叠加椒盐噪声,使用中值滤波方法对其进行图像降噪,窗口分别采用3*3,5*5,7*7。在同一窗口内依次显示叠加噪声的图像,和处理后图像。
clear all img = imread('D:\Program Files\MATLAB\R2018a\toolbox\images\imdata\lena.png'); [width,height,z]=size(img); image=rgb2gray(img); noise = rand(size(img)); a=0.1; b=0.2; a1=rand(width,height)<a; a2=rand(width,height)<b; image(a1&a2)=0; image(a1& ~a2)=255; figure; subplot(5,1,1);imshow(img);title('原图'); subplot(5,1,2);imshow(image);title('叠加椒盐噪声的图像'); image_filt_33 = medfilt2(image,[3,3]); image_filt_55 = medfilt2(image,[3,3]); image_filt_77 = medfilt2(image,[3,3]); subplot(5,1,3);imshow(image_filt_33);title('3*3窗口去噪后图像'); subplot(5,1,4);imshow(image_filt_55);title('5*5窗口去噪后图像'); subplot(5,1,5);imshow(image_filt_77);title('7*7窗口去噪后图像');
5、 读取matlab内置的circuit图像,对其进行直方图均衡化操作,并在同一窗口内依次显示原图,原图直方图,均衡化后的图像和均衡化图像的直方图。
clear all img = imread('circuit.tif'); img_eq = histeq(img); figure; subplot(2,2,1);imshow(img);title('原图'); subplot(2,2,2);imhist(img);title('原图的直方图'); subplot(2,2,3);imshow(img_eq(:,:,1));title('均衡化直方图后的图'); subplot(2,2,4);imhist(img_eq);title('均衡化直方图');