=================================================== Math 451 Laboratory 3: Images and interpolation Date: 2013-03-22 Due: 2013-03-29 =================================================== Our class has focused on 1-dimensional methods so far: 1-d root-finding, quadrature (1-d integrals), and 1-d interpolation. However, many, if not most, applications use data sets that are two or more dimensions. Many of our techniques can be extended from 1 to 2 or more dimensions, although they become more complicated. One good example of this is image-processing. In it's simplest form, a digital monochrome image is a two-dimensional array of positive numbers representing intensity. A digital color image is a set of two-dimensional arrays, one for each color. Here, we will focus on monochrome image of Stanislaw Ulam, famous mathematician. Goals ---------------------------- - Learn the basics of using matrices to represent images in Matlab. - Use bilinear interpolation to smooth a low resolution image. - Explore interpolation artifacts for bilinear interpolation. - Explore Matlab's interp2 command, and different kinds of interpolation. In the instructions that follow, comments begin with a "%" and questions you should answer begin with "?". =============================================================================== Part 1) Loading and manipulating an image. -------------------------------------------------------------------------------- % There are many different image formats available, many of which use % sophisticated image-compression algorithms to reduce image size. The simplest % image format is the "Tagged Image File Format", aka TIFF. This is format % primarily used in scientific image processing fields, like astronomy. Simple % monochrome TIFF's represent each pixel by an integer between 0 and 255. For the first part of our lab, we will work with a well-known image of a famous mathematician who worked in the Manhattan project. Go to the link below and save the image to your Desktop. http://www.math.psu.edu/treluga/451/StanislawUlam.tif Now, open Matlab, and change your working directory to the Desktop. Then type the following commands. >> pic = imread('StanislawUlam.tif'); >> figure(1); clf; >> imshow(pic,[0,255]); >> pic >> size(pic) imread() reads an image into Matlab. imshow() displays the image into Matlab. If you now look at the actual entries of pic (which should have been displayed by the second to last line), you will see that they are just numbers and that pic is a matrix. size(pic) tells us that this image is 600 rows by 642 columns, almost 400,000 pixels. We can manipulate an image just like a matrix. To shrink the image horizontally, >> imshow(pic(:,1:2:end)); To shrink the image vertically, >> imshow(pic(1:2:end,:)); To lighten the image, try >> imshow( (pic+200)/2, [0,255] ); To extract a subimage, >> imshow( pic(200:400,200:400) ); Now, let's go back to the original image >> imshow( pic ); For we would like to lower the resolution of our image, as an example of a bad image we might want to improve. Do the following. >> s = 5; >> low_pic = pic(1:s:end,1:s:end); >> figure(2); >> clf; >> imshow(low_pic); ? Now, compare the two images. Find 3 features of the original image that are ? not recognizable once we reduced the resolution, and describe these features. Part 2) Bilinear Interpolation of StanislawUlam.tif -------------------------------------------------------------------------------- % We will use bilinear interpolation as our simplest example of 2-dimensional % interpolation. Bilinear interpolation is a nonlinear interpolation method % that uses 4 nearest-neighbor points on a regular lattice. Details are at % http://en.wikipedia.org/wiki/Bilinear_interpolation Please download the bilinear interpolation script from the link below and save it to your desktop. http://www.math.psu.edu/treluga/451/bilinear_img.m Now, in Matlab, do the following. >> interp_pic = bilinear_img(low_pic,s); >> figure(3); >> clf; >> imshow(interp_pic,[0,255]); NOTE: This might be SLOW(depending on your version of Matlab or Octave) ! Be patient, and wonder why it's slow. You should now have 3 different version of the image, which you should compare to each other closely. Save these figures to be turned in with your lab. ? For each of the 3 features you identified in Part 1, describe how well that ? feature is recovered in the interpolated image. Part 3) Closer inspection of bilinear interpolation -------------------------------------------------------------------------------- To get a better idea of what bilinear interpolation does, run the following script. Save the figures and include them in your lab. First, we figure(4); clf; x = rand(5,5); subplot(1,2,1); imshow(x); title('Original'); Next, we will make a new image with 16-times higher resolution using bilinear interpolation. ? Take a moment to describe what you expect the interpolated ? image too look like, and how it will differ from the ? current image. Now, run the following. y=bilinear_img(x,16); subplot(1,2,2); imshow(y); title('Bilinear interpolant'); ? Does the interpolated image match your expectation? ? Describe what kinds of "artifacts" you observe in the ? interpolated image, and if you think the interpolation is ? doing a good job of meeting your expectations. To get some more information about how this method changes the image pixel-value distribution, run the following comparison. figure(5); clf; xd =reshape(x,1,''); xd = sort(xd); yd =reshape(y,1,''); yd = sort(yd); Lxd = length(xd); Lyd = length(yd); plot((1:Lxd)/Lxd,xd,'r-',(1:Lyd)/Lyd,yd,'b-'); legend('Original', 'Bilinear interpolant'); Part 4) Matlab's interp2 command -------------------------------------------------------------------------------- % There are actually many different interpolation algorithms out there. % Several are already implemented in Matlab using interp2(), and these are MUCH % FASTER than the version you downloaded above because they have been optimized % for our computers. Implement the following script. clear all; figure(6); clf; m = 5; % example image size pic = rand(m,m); s = 16; [m, n] = size(pic); [X,Y] = meshgrid(1:m); [XI,YI] = meshgrid(linspace(1,m,s*m-1)); pic_nearest = interp2(X,Y,pic,XI,YI,'nearest'); pic_linear = interp2(X,Y,pic,XI,YI,'linear'); pic_cubic = interp2(X,Y,pic,XI,YI,'cubic'); pic_spline = interp2(X,Y,pic,XI,YI,'spline'); subplot(2,5,3); imshow(pic,[0,1]); title('Original'); subplot(2,5,5+1); imshow(pic_nearest,[0,1]); title('Nearest'); subplot(2,5,5+2); imshow(pic_linear,[0,1]); title('Linear'); subplot(2,5,5+4); imshow(pic_cubic,[0,1]); title('Cubic'); subplot(2,5,5+3); imshow(pic_spline,[0,1]); title('Spline'); ? Compare and constrast the results of these interpolation algorithms. ================================================================================ ~ ~ ~ ~