- We load an image of the Mars moon Phobos:
im1 = imread('phobos.png');
imagesc(im1);
- Since the image was taken with a black-and-white CCD, we change the colormap:
colormap(gray);
- Im1 contains values from 0 to 255, representing arbitrary colors in a
color table. But in our case these are just lightness values, so we
change the representation to a usual array, containing double values
between 0 and 1.
im2 = double(im1) / 255;
- We control our variables and find that the new image consumes 8x more memory:
whos
Name Size Bytes Class
im1 292x510 148920 uint8 array
im2 292x510 1191360 double array
Grand total is 297840 elements using 1340280 bytes
- Obviously the edges contain bad data, so we cut them off:
im3 = im2(3:289, 5:509);
imagesc(im3);
- Some strange values are due to defect CCD pixels. To find them, we
create a smoothed image where every pixel is replaced by the middle
value of its neighbours ("median filter") and look for large
differences between the original and the smooth image:
smooth = medfilt2(im3);
diffs = abs(im3 -smooth);
badspots = diffs > 0.2;
imagesc(badspots);
- We replace the values at the bad spots with the corresponding smoothed values:
im3(find(badspots)) = smooth(find(badspots));
imagesc(im3);
- Finally, we save our cleaned image:
imwrite(im3, 'clean.png');

Peter Junglas 8.3.2000