/image_analysisII/bookcode/visionbook/05Preproc/imsharpen.m

http://uppsala-university.googlecode.com/ · Objective C · 75 lines · 63 code · 12 blank · 0 comment · 5 complexity · 7315f234d3f661d59c2e46a1ad0ea379 MD5 · raw file

  1. function im_out=imsharpen(im,C,VERBOSITY);
  2. % IMSHARPEN Image sharpening.
  3. % CMP Vision Algorithms http://visionbook.felk.cvut.cz
  4. % Tomas Svoboda, Petr Nemecek, 2006-2007
  5. % Function imsharpen sharpens an image by emphasizing places
  6. % with high gradient:
  7. % sharpening is computed by subtracting
  8. % a weighted Laplacian from the blurred image.
  9. % Usage: im_out = imsharpen(im,C,VERBOSITY)
  10. % Inputs:
  11. % im [m x n] Input image, gray scale images assumed.
  12. % C 1x1 Multiplicative coefficient: (0.3--1.5) is a reasonable range.
  13. % VERBOSITY (default 0)
  14. % Verbosity of the function, if >0 displays
  15. % images of the gradients.
  16. % Outputs:
  17. % im_out [m x n] Sharpened image.
  18. % See also: gradient.
  19. % History:
  20. % $Id: imsharpen_decor.m 1074 2007-08-14 09:45:42Z kybic $
  21. %
  22. % 2006-01 Petr Nemecek created
  23. % 2007-03-12 Tomas Svoboda (TS), decorated and pedagogically enhanced
  24. % 2007-05-02 TS: new decor
  25. % 2007-05-24 VZ: typo
  26. % 2007-08-09 TS: refinement for better looking of m-files
  27. if nargin<3
  28. VERBOSITY=0;
  29. end
  30. if C<=0
  31. warning('Incorrect input value of parameter C. Parameter C has to be greater than zero.');
  32. end
  33. % First, convert the input image to double precision, integers cause errors in
  34. % gradient computation.
  35. im = double(im);
  36. % Compute the first derivatives:
  37. [gradX,gradY] = gradient(im);
  38. % Compute the second derivatives:
  39. sqgradX = gradient(gradX);
  40. sqgradY = gradient(gradY')';
  41. % Compute the Laplacian
  42. Laplacian = sqgradX + sqgradY;
  43. % and sharpen the image:
  44. im_out = im - C*Laplacian;
  45. % Truncate values smaller than 0 and higher than 255.
  46. % An alternative post-processing may be
  47. % to re-map the new range to the range (0,...,255), see
  48. % hist_equal or imagesc.
  49. %
  50. im_out(im_out>255) = 255;
  51. im_out(im_out<0) = 0;
  52. im_out = uint8(im_out);
  53. if VERBOSITY>0
  54. figure;
  55. subplot(2,2,1), imagesc(gradX), axis image, colormap(gray(256)); title('x-gradient \it \partial I/\partial x')
  56. subplot(2,2,2), imagesc(gradY), axis image, colormap(gray(256)); title('y-gradient \it \partial I/\partial y')
  57. subplot(2,2,3), imagesc(sqgradX), axis image, colormap(gray(256)); title('\it \partial^2 I/\partial x^2')
  58. subplot(2,2,4), imagesc(sqgradY), axis image, colormap(gray(256)); title('\it \partial^2 I/\partial y^2')
  59. figure;
  60. imagesc(Laplacian), axis image, colormap(gray(256));
  61. title('Laplacian: \it \nabla = \partial^2 I/\partial x^2 + \partial^2 I/\partial y^2')
  62. end
  63. return; % end of imsharpen