/Vision/Lab1/Baby Food/baby_food.cpp

https://github.com/Newky/4thYear · C++ · 121 lines · 89 code · 17 blank · 15 comment · 16 complexity · abfa81b0d57a271836c950cf053eb8c4 MD5 · raw file

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #include "cv.h"
  5. #include "highgui.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "../utilities.h"
  9. #define NUM_IMAGES 4
  10. #define THRESHOLD 50
  11. // Locate the red pixels in the source image and return the percentage of red points found.
  12. int find_spoons( IplImage* source, IplImage* result, IplImage* temp )
  13. {
  14. int red_point_count = 0;
  15. int width_step=source->widthStep;
  16. int pixel_step=source->widthStep/source->width;
  17. int number_channels=source->nChannels;
  18. cvZero( result );
  19. unsigned char white_pixel[4] = {255,255,255,0};
  20. int row=0,col=0;
  21. // Find all red points in the image
  22. for (row=0; row < result->height; row++)
  23. for (col=0; col < result->width; col++)
  24. {
  25. unsigned char* curr_point = GETPIXELPTRMACRO( source, col, row, width_step, pixel_step );
  26. if ((curr_point[RED_CH] >= THRESHOLD) && ((curr_point[BLUE_CH] < THRESHOLD) || (curr_point[GREEN_CH] < THRESHOLD)))
  27. {
  28. PUTPIXELMACRO( result, col, row, white_pixel, width_step, pixel_step, number_channels );
  29. }
  30. }
  31. // Apply morphological opening and closing operations to clean up the image
  32. cvMorphologyEx( result, temp, NULL, NULL, CV_MOP_OPEN, 3 );
  33. cvMorphologyEx( temp, result, NULL, NULL, CV_MOP_CLOSE, 3 );
  34. // Count the red points remaining
  35. for (row=0; row < result->height; row++)
  36. for (col=0; col < result->width; col++)
  37. {
  38. unsigned char* curr_point = GETPIXELPTRMACRO( result, col, row, width_step, pixel_step );
  39. if (curr_point[RED_CH] == 255)
  40. {
  41. red_point_count++;
  42. }
  43. }
  44. return (red_point_count*100) / (result->height*result->width);
  45. }
  46. // Write out the number of spoons likely to be in the image on the basis of the percentage of red points
  47. // located. Normally this would be done by using a large number of training images in order to allow
  48. // optimal classification.
  49. void write_number_of_spoons_on_image( IplImage* image, int percentage_of_red_points )
  50. {
  51. int num_spoons = (percentage_of_red_points < 3) ? 0 : (percentage_of_red_points > 7) ? 2 : 1;
  52. char spoons_text[100];
  53. sprintf(spoons_text,"%d spoon%c in can (Redness %% = %d%%)",num_spoons,(num_spoons==1) ? ' ':'s',percentage_of_red_points);
  54. write_text_on_image(image,1,1,spoons_text);
  55. }
  56. int main( int argc, char** argv )
  57. {
  58. int selected_image_num = 1;
  59. IplImage* selected_image = NULL;
  60. IplImage* images[NUM_IMAGES];
  61. IplImage* temp_image = NULL;
  62. IplImage* result_image = NULL;
  63. // Load all the images.
  64. for (int file_num=1; (file_num <= NUM_IMAGES); file_num++)
  65. {
  66. char filename[100];
  67. sprintf(filename,"../Baby Food/BabyFoodCan%d.jpg",file_num);
  68. if( (images[file_num-1] = cvLoadImage(filename,-1)) == 0 )
  69. return 0;
  70. }
  71. // Explain the User Interface
  72. printf( "Hot keys: \n"
  73. "\tESC - quit the program\n");
  74. printf( "\t1..%d - select image\n",NUM_IMAGES);
  75. // Create display windows for images
  76. cvNamedWindow( "Original", 1 );
  77. cvNamedWindow( "Processed Image", 1 );
  78. // Create images to do the processing in.
  79. selected_image = cvCloneImage( images[selected_image_num-1] );
  80. result_image = cvCloneImage( selected_image );
  81. temp_image = cvCloneImage( selected_image );
  82. // Setup mouse callback on the original image so that the user can see image values as they move the
  83. // cursor over the image.
  84. cvSetMouseCallback( "Original", on_mouse_show_values, 0 );
  85. window_name_for_on_mouse_show_values="Original";
  86. image_for_on_mouse_show_values=selected_image;
  87. int user_clicked_key = 0;
  88. do {
  89. // Process image (i.e. setup and find the number of spoons)
  90. cvCopyImage( images[selected_image_num-1], selected_image );
  91. cvShowImage( "Original", selected_image );
  92. image_for_on_mouse_show_values=selected_image;
  93. int red_percentage = find_spoons( selected_image, result_image, temp_image );
  94. write_number_of_spoons_on_image( result_image, red_percentage );
  95. cvShowImage( "Processed Image", result_image );
  96. // Wait for user input
  97. user_clicked_key = cvWaitKey(0);
  98. if ((user_clicked_key >= '1') && (user_clicked_key <= '0'+NUM_IMAGES))
  99. {
  100. selected_image_num = user_clicked_key-'0';
  101. }
  102. } while ( user_clicked_key != ESC );
  103. return 1;
  104. }