>> I=imread('G:\\MATLAB7\\cunfang\\lena压缩.tif'); >> J=rgb2gray(I); >> J1=double(J); >> J2=J1*127/255; >> J3=J1*63/255; >> J4=J1*31/255; >> J5=J1*15/255; >> J6=J1*7/255; >> J7=J1*3/255; >> J8=J1*1/255;
>> subplot(3,3,1),imshow(I),title('原图');
>> subplot(3,3,2),imshow(J1,[0 256]),title('灰度级:256'); >> subplot(3,3,3),imshow(J2,[0 128]),title('灰度级:128'); >> subplot(3,3,4),imshow(J3,[0 ]),title('灰度级:'); >> subplot(3,3,5),imshow(J4,[0 32]),title('灰度级:32'); >> subplot(3,3,6),imshow(J5,[0 16]),title('灰度级:16'); >> subplot(3,3,7),imshow(J6,[0 8]),title('灰度级:8'); >> subplot(3,3,8),imshow(J7,[0 4]),title('灰度级:4'); >> subplot(3,3,9),imshow(J8,[0 2]),title('灰度级:2');
原图灰度级:256灰度级:灰度级:32灰度级:8灰度级:4
灰度级:128灰度级:16灰度级:2
第1题 把制定的图像由256级灰度级减少到2级灰度级,并依次显示出来,每幅图像有使用的灰度级标注。
>> I=imread('G:\\MATLAB7\\cunfang\\lena压缩.tif'); >> J=rgb2gray(I); >> J1=double(J); >> J2=J1*127/255; >> J3=J1*63/255; >> J4=J1*31/255; >> J5=J1*15/255; >> J6=J1*7/255; >> J7=J1*3/255; >> J8=J1*1/255;
>> subplot(3,3,1),imshow(I),title('原图');
>> subplot(3,3,2),imshow(round(J1),[0 256]),title('灰度级:256'); >> subplot(3,3,2),imshow(J),title('灰度级:256');
>> subplot(3,3,3),imshow(round(J2),[0 128]),title('灰度级:128'); >> subplot(3,3,4),imshow(round(J3),[0 ]),title('灰度级:'); >> subplot(3,3,5),imshow(round(J4),[0 32]),title('灰度级:32'); >> subplot(3,3,6),imshow(round(J5),[0 16]),title('灰度级:16'); >> subplot(3,3,7),imshow(round(J6),[0 8]),title('灰度级:8'); >> subplot(3,3,8),imshow(round(J7),[0 4]),title('灰度级:4'); >> subplot(3,3,9),imshow(round(J8),[0 2]),title('灰度级:2');
原图灰度级:256灰度级:灰度级:32灰度级:8灰度级:4
灰度级:128灰度级:16灰度级:2
第二题 (1)灰度变换:选择一幅对比度不足的图像,对该图像进行伽玛变换,增强对比度,显示增强前、后的图像以及它们的灰度直方图。可以选择一系列的伽玛参数进行比较.
>> I=imread('G:\\MATLAB7\\cunfang\\Gamma3.tif'); >> J1=imadjust(I,[],[],0.7); >> J2=imadjust(I,[],[],7);
>> subplot(3,2,1),imshow(I),title('原图');
>> subplot(3,2,2),imhist(I),title('原图灰度图'); >> subplot(3,2,3),imshow(J1),title('gamma=0.7'); >> subplot(3,2,4),imhist(J1),title('gamma=0.7灰度图'); >> subplot(3,2,5),imshow(J2),title('gamma=7'); >> subplot(3,2,6),imhist(J2),title('gamma=7灰度图');
原图原图灰度图500000gamma=0.710000500000gamma=710000500000100200100200gamma=7灰度图100200gamma=0.7灰度图
(2)直方图均衡:选择一幅直方图不均匀的图像,对该图像做直方图均衡处理,显示处理前后的图像以及它们的灰度直方图。
>> I=imread('G:\\MATLAB7\\cunfang\\Gamma3.tif'); >> J=histeq(I);
>> subplot(2,2,1),imshow(I),title('原图');
>> subplot(2,2,2),imhist(I),title('原图灰度直方图'); >> subplot(2,2,3),imshow(J),title('均衡处理图');
>> subplot(2,2,4),imhist(J),title('图均衡处理灰度直方图');
原图800060004000200000均衡处理图10000100200原图灰度直方图图均衡处理灰度直方图500000100200
第3题 本次实验对于图像增强方法的理解,比如,灰度变换与直方图均衡后的差异
答:① 灰度变换可使图像动态范围增大,图像对比度扩展,图像变清晰,特征明显。当γ<1时,亮区对比度减小,暗区对比度增大,亮度均匀压缩,原图像好像被漂白过.当γ>1时,亮区对比度增大,暗区对比度减小,亮度均匀扩张,图像明显偏暗.
② 直方图均衡化后的图像与原图像反差增大了,许多细节更加清晰,对应的直方图变得平坦多了.