PageRenderTime 94ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/source/3rd_party/opencv/sources/samples/c/morphology.c

https://gitlab.com/Ruggero/SparkEngine_Desktop
C | 130 lines | 101 code | 23 blank | 6 comment | 19 complexity | 0b0997267fe89baf339090c62ef764f2 MD5 | raw file
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc_c.h"
  3. #include <stdio.h>
  4. IplImage* src = 0;
  5. IplImage* dst = 0;
  6. IplConvKernel* element = 0;
  7. int element_shape = CV_SHAPE_RECT;
  8. //the address of variable which receives trackbar position update
  9. int max_iters = 10;
  10. int open_close_pos = 0;
  11. int erode_dilate_pos = 0;
  12. // callback function for open/close trackbar
  13. static void OpenClose(int pos)
  14. {
  15. int n = open_close_pos - max_iters;
  16. int an = n > 0 ? n : -n;
  17. (void)pos;
  18. element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
  19. if( n < 0 )
  20. {
  21. cvErode(src,dst,element,1);
  22. cvDilate(dst,dst,element,1);
  23. }
  24. else
  25. {
  26. cvDilate(src,dst,element,1);
  27. cvErode(dst,dst,element,1);
  28. }
  29. cvReleaseStructuringElement(&element);
  30. cvShowImage("Open/Close",dst);
  31. }
  32. // callback function for erode/dilate trackbar
  33. static void ErodeDilate(int pos)
  34. {
  35. int n = erode_dilate_pos - max_iters;
  36. int an = n > 0 ? n : -n;
  37. (void)pos;
  38. element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
  39. if( n < 0 )
  40. {
  41. cvErode(src,dst,element,1);
  42. }
  43. else
  44. {
  45. cvDilate(src,dst,element,1);
  46. }
  47. cvReleaseStructuringElement(&element);
  48. cvShowImage("Erode/Dilate",dst);
  49. }
  50. static void help(void)
  51. {
  52. printf( "This program demonstrated the use of the morphology operator, especially open, close, erode, dilate operations\n"
  53. "Morphology operators are built on max (close) and min (open) operators as measured by pixels covered by small structuring elements.\n"
  54. "These operators are very efficient.\n"
  55. "This program also allows you to play with elliptical, rectangluar and cross structure elements\n"
  56. "Usage: \n"
  57. "./morphologyc [image_name -- Default baboon.jpg]\n"
  58. "\nHot keys: \n"
  59. "\tESC - quit the program\n"
  60. "\tr - use rectangle structuring element\n"
  61. "\te - use elliptic structuring element\n"
  62. "\tc - use cross-shaped structuring element\n"
  63. "\tSPACE - loop through all the options\n" );
  64. }
  65. int main( int argc, char** argv )
  66. {
  67. char* filename = 0;
  68. help();
  69. filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
  70. if( (src = cvLoadImage(filename,1)) == 0 )
  71. {
  72. printf("Cannot load file image %s\n", filename);
  73. help();
  74. return -1;
  75. }
  76. dst = cvCloneImage(src);
  77. //create windows for output images
  78. cvNamedWindow("Open/Close",1);
  79. cvNamedWindow("Erode/Dilate",1);
  80. open_close_pos = erode_dilate_pos = max_iters;
  81. cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
  82. cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
  83. for(;;)
  84. {
  85. int c;
  86. OpenClose(open_close_pos);
  87. ErodeDilate(erode_dilate_pos);
  88. c = cvWaitKey(0);
  89. if( (char)c == 27 )
  90. break;
  91. if( (char)c == 'e' )
  92. element_shape = CV_SHAPE_ELLIPSE;
  93. else if( (char)c == 'r' )
  94. element_shape = CV_SHAPE_RECT;
  95. else if( (char)c == 'c' )
  96. element_shape = CV_SHAPE_CROSS;
  97. else if( (char)c == ' ' )
  98. element_shape = (element_shape + 1) % 3;
  99. }
  100. //release images
  101. cvReleaseImage(&src);
  102. cvReleaseImage(&dst);
  103. //destroy windows
  104. cvDestroyWindow("Open/Close");
  105. cvDestroyWindow("Erode/Dilate");
  106. return 0;
  107. }