fork download
  1. #include <iostream>
  2. #include "opencv2\opencv.hpp"
  3.  
  4. using namespace std;
  5. using namespace cv;
  6.  
  7. class Strategy
  8. {
  9. public:
  10. //uchar *RGB;
  11. int mean,_count,k;
  12. virtual ~Strategy() {}
  13. virtual Mat process_image(Mat &image) = 0;
  14. };
  15.  
  16. class Context
  17. {
  18. public:
  19. Mat ContextInterface(Strategy *pStrategy,Mat &image);
  20. };
  21.  
  22. class original : public Strategy
  23. {
  24. public:
  25. virtual ~original() {}
  26.  
  27. virtual Mat process_image(Mat &image);
  28. };
  29.  
  30. class gray : public Strategy
  31. {
  32. public:
  33. virtual ~gray() {}
  34.  
  35. virtual Mat process_image(Mat &image);
  36. };
  37.  
  38. class threshhold : public Strategy
  39. {
  40. public:
  41. virtual ~threshhold() {}
  42.  
  43. virtual Mat process_image(Mat &image);
  44. };
  45.  
  46. Mat Context::ContextInterface(Strategy *pStrategy,Mat &image)
  47. {
  48. return pStrategy->process_image(image);
  49. }
  50.  
  51. Mat original::process_image(Mat &image)
  52. {
  53. return image;
  54. }
  55.  
  56. Mat gray::process_image(Mat &image)
  57. {
  58. mean=_count=0;
  59. for(int i=0; i<image.rows; i++)
  60. {
  61. for(int j=0; j<image.cols; j++)
  62. {
  63. k=0.333*(image.at<Vec3b>(i,j)[2]+image.at<Vec3b>(i,j)[1]+image.at<Vec3b>(i,j)[0]);
  64. image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=k;
  65. mean+=k;
  66. ++_count;
  67. }
  68. }
  69. return image;
  70. }
  71.  
  72. Mat threshhold::process_image(Mat &image)
  73. {
  74. for(int i=0; i<image.rows; i++)
  75. {
  76. for(int j=0; j<image.cols; j++)
  77. {
  78. if(image.at<Vec3b>(i,j)[2]<mean)
  79. {
  80. image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=0;
  81. }
  82. else
  83. {
  84. image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=255;
  85. }
  86. }
  87. }
  88. return image;
  89. }
  90.  
  91.  
  92.  
  93. int main(void)
  94. {
  95. Mat image=imread("pic3.png", CV_LOAD_IMAGE_COLOR); // Read the file
  96. Context* pContext = new Context();
  97. Strategy* pStrategy;
  98. int mean;
  99.  
  100. if(! image.data ) // Check for invalid input
  101. {
  102. cout << "Could not open or find the image" << std::endl ;
  103. return -1;
  104. }
  105.  
  106. cout<<image.rows;
  107.  
  108. //show original picture
  109. pStrategy = new original();
  110. namedWindow( "original", CV_WINDOW_AUTOSIZE );// Create a window for display.
  111. imshow( "original", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
  112. delete pStrategy;
  113.  
  114. //show gray picture
  115. pStrategy = new gray();
  116. namedWindow( "gray", CV_WINDOW_AUTOSIZE );// Create a window for display.
  117. imshow( "gray", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
  118. mean=(pStrategy->mean)/(pStrategy->_count);
  119. delete pStrategy;
  120.  
  121. //show threshhold picture
  122. pStrategy = new threshhold();
  123. (pStrategy->mean)=mean;
  124. namedWindow( "threshhold", CV_WINDOW_AUTOSIZE );// Create a window for display.
  125. imshow( "threshhold", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
  126. delete pStrategy;
  127.  
  128. waitKey(0); // Wait for a keystroke in the window
  129. return 0;
  130. }
  131.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty