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

/examples/opencv/src/ffilldemo.c

https://gitlab.com/juan-cardelino/misc_projects
C | 187 lines | 165 code | 22 blank | 0 comment | 13 complexity | 16916410fa538e61c7d72c77b049bb48 MD5 | raw file
  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #define CV_NO_BACKWARD_COMPATIBILITY
  5. #ifndef _EiC
  6. #include "cv.h"
  7. #include "highgui.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #endif
  11. IplImage* color_img0;
  12. IplImage* mask;
  13. IplImage* color_img;
  14. IplImage* gray_img0 = NULL;
  15. IplImage* gray_img = NULL;
  16. int ffill_case = 1;
  17. int lo_diff = 20, up_diff = 20;
  18. int connectivity = 4;
  19. int is_color = 1;
  20. int is_mask = 0;
  21. int new_mask_val = 255;
  22. void on_mouse( int event, int x, int y, int flags, void* param )
  23. {
  24. if( !color_img )
  25. return;
  26. switch( event )
  27. {
  28. case CV_EVENT_LBUTTONDOWN:
  29. {
  30. CvPoint seed = cvPoint(x,y);
  31. int lo = ffill_case == 0 ? 0 : lo_diff;
  32. int up = ffill_case == 0 ? 0 : up_diff;
  33. int flags = connectivity + (new_mask_val << 8) +
  34. (ffill_case == 1 ? CV_FLOODFILL_FIXED_RANGE : 0);
  35. int b = rand() & 255, g = rand() & 255, r = rand() & 255;
  36. CvConnectedComp comp;
  37. if( is_mask )
  38. cvThreshold( mask, mask, 1, 128, CV_THRESH_BINARY );
  39. if( is_color )
  40. {
  41. CvScalar color = CV_RGB( r, g, b );
  42. cvFloodFill( color_img, seed, color, CV_RGB( lo, lo, lo ),
  43. CV_RGB( up, up, up ), &comp, flags, is_mask ? mask : NULL );
  44. cvShowImage( "image", color_img );
  45. }
  46. else
  47. {
  48. CvScalar brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
  49. cvFloodFill( gray_img, seed, brightness, cvRealScalar(lo),
  50. cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
  51. cvShowImage( "image", gray_img );
  52. }
  53. printf("%g pixels were repainted\n", comp.area );
  54. if( is_mask )
  55. cvShowImage( "mask", mask );
  56. }
  57. break;
  58. }
  59. }
  60. int main( int argc, char** argv )
  61. {
  62. char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
  63. if( (color_img0 = cvLoadImage(filename,1)) == 0 )
  64. return 0;
  65. printf( "Hot keys: \n"
  66. "\tESC - quit the program\n"
  67. "\tc - switch color/grayscale mode\n"
  68. "\tm - switch mask mode\n"
  69. "\tr - restore the original image\n"
  70. "\ts - use null-range floodfill\n"
  71. "\tf - use gradient floodfill with fixed(absolute) range\n"
  72. "\tg - use gradient floodfill with floating(relative) range\n"
  73. "\t4 - use 4-connectivity mode\n"
  74. "\t8 - use 8-connectivity mode\n" );
  75. color_img = cvCloneImage( color_img0 );
  76. gray_img0 = cvCreateImage( cvSize(color_img->width, color_img->height), 8, 1 );
  77. cvCvtColor( color_img, gray_img0, CV_BGR2GRAY );
  78. gray_img = cvCloneImage( gray_img0 );
  79. mask = cvCreateImage( cvSize(color_img->width + 2, color_img->height + 2), 8, 1 );
  80. cvNamedWindow( "image", 0 );
  81. cvCreateTrackbar( "lo_diff", "image", &lo_diff, 255, NULL );
  82. cvCreateTrackbar( "up_diff", "image", &up_diff, 255, NULL );
  83. cvSetMouseCallback( "image", on_mouse, 0 );
  84. for(;;)
  85. {
  86. int c;
  87. if( is_color )
  88. cvShowImage( "image", color_img );
  89. else
  90. cvShowImage( "image", gray_img );
  91. c = cvWaitKey(0);
  92. switch( (char) c )
  93. {
  94. case '\x1b':
  95. printf("Exiting ...\n");
  96. goto exit_main;
  97. case 'c':
  98. if( is_color )
  99. {
  100. printf("Grayscale mode is set\n");
  101. cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
  102. is_color = 0;
  103. }
  104. else
  105. {
  106. printf("Color mode is set\n");
  107. cvCopy( color_img0, color_img, NULL );
  108. cvZero( mask );
  109. is_color = 1;
  110. }
  111. break;
  112. case 'm':
  113. if( is_mask )
  114. {
  115. cvDestroyWindow( "mask" );
  116. is_mask = 0;
  117. }
  118. else
  119. {
  120. cvNamedWindow( "mask", 0 );
  121. cvZero( mask );
  122. cvShowImage( "mask", mask );
  123. is_mask = 1;
  124. }
  125. break;
  126. case 'r':
  127. printf("Original image is restored\n");
  128. cvCopy( color_img0, color_img, NULL );
  129. cvCopy( gray_img0, gray_img, NULL );
  130. cvZero( mask );
  131. break;
  132. case 's':
  133. printf("Simple floodfill mode is set\n");
  134. ffill_case = 0;
  135. break;
  136. case 'f':
  137. printf("Fixed Range floodfill mode is set\n");
  138. ffill_case = 1;
  139. break;
  140. case 'g':
  141. printf("Gradient (floating range) floodfill mode is set\n");
  142. ffill_case = 2;
  143. break;
  144. case '4':
  145. printf("4-connectivity mode is set\n");
  146. connectivity = 4;
  147. break;
  148. case '8':
  149. printf("8-connectivity mode is set\n");
  150. connectivity = 8;
  151. break;
  152. }
  153. }
  154. exit_main:
  155. cvDestroyWindow( "test" );
  156. cvReleaseImage( &gray_img );
  157. cvReleaseImage( &gray_img0 );
  158. cvReleaseImage( &color_img );
  159. cvReleaseImage( &color_img0 );
  160. cvReleaseImage( &mask );
  161. return 1;
  162. }
  163. #ifdef _EiC
  164. main(1,"ffilldemo.c");
  165. #endif