imgmanipbs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "imgmanipbs.h"
  2. #include <list>
  3. ImgManipBS::ImgManipBS() {
  4. width = height = 0;
  5. }
  6. ImgManipBS::ImgManipBS(const char* fileName)
  7. {
  8. mainImage = CImg<>(fileName);
  9. width = mainImage.width();
  10. height = mainImage.height();
  11. noOfWSquares = width / squareSize;
  12. noOfHSquares = height / squareSize;
  13. whiteMeanValues = new int*[noOfWSquares];
  14. blackMeanValues = new int*[noOfWSquares];
  15. for (int i = 0; i < noOfWSquares; ++i) {
  16. whiteMeanValues[i] = new int[noOfHSquares];
  17. blackMeanValues[i] = new int[noOfHSquares];
  18. }
  19. // Output image
  20. outImages[0] = CImg<>(width,height); // B&W Image with same size: White
  21. outImages[1] = CImg<>(width,height); // B&W Image with same size: Black
  22. printf("Original image size %dx%d\n",width,height);
  23. EvaluateImage();
  24. CreateGradiantImage();
  25. outImages[0].save_jpeg("ref_white.jpg");
  26. outImages[1].save_jpeg("ref_black.jpg");
  27. printf("Done.\n");
  28. }
  29. void ImgManipBS::CreateGradiantImage() {
  30. for(auto yy = 0; yy < noOfHSquares-1; yy++)
  31. {
  32. for (auto xx = 0; xx < noOfWSquares-1; xx++ )
  33. {
  34. int x = (xx*squareSize) + squareMargin;
  35. int y = (yy*squareSize) + squareMargin;
  36. DrawGradRect(0, x,y,whiteMeanValues[xx][yy],whiteMeanValues[xx+1][yy],whiteMeanValues[xx][yy+1],whiteMeanValues[xx+1][yy+1]);
  37. DrawGradRect(1, x,y,blackMeanValues[xx][yy],blackMeanValues[xx+1][yy],blackMeanValues[xx][yy+1],blackMeanValues[xx+1][yy+1]);
  38. if( yy == 0 ) // Only top row
  39. {
  40. DrawTopGradRect(0,x,0,whiteMeanValues[xx][yy],whiteMeanValues[xx+1][yy]);
  41. DrawTopGradRect(1,x,0,blackMeanValues[xx][yy],blackMeanValues[xx+1][yy]);
  42. }
  43. }
  44. }
  45. }
  46. void ImgManipBS::DrawGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
  47. {
  48. CImg<unsigned char> tempImage = CImg<>(squareSize,squareSize); // Black and white image
  49. const unsigned char white[] = { 255,255,255 };
  50. const float opacity=1.0;
  51. float fp1 = (1.0 / 255) * p1;
  52. float fp2 = (1.0 / 255) * p2;
  53. float fp3 = (1.0 / 255) * p3;
  54. float fp4 = (1.0 / 255) * p4;
  55. tempImage.draw_triangle(0,0,0,squareSize,squareSize,squareSize,white,fp1,fp3,fp4,opacity);
  56. tempImage.draw_triangle(0,0,squareSize,0,squareSize,squareSize,white,fp1,fp2,fp4,opacity);
  57. outImages[imageNo].draw_image(x,y,tempImage);
  58. }
  59. void ImgManipBS::DrawTopGradRect(int imageNo,int x,int y, unsigned char p1, unsigned char p2)
  60. {
  61. CImg<unsigned char> tempImage = CImg<>(squareSize,squareMargin); // Black and white image
  62. const unsigned char white[] = { 255,255,255 };
  63. const float opacity=1.0;
  64. float fp1 = (1.0 / 255) * p1;
  65. float fp2 = (1.0 / 255) * p2;
  66. tempImage.draw_triangle(0,0,0,squareSize,squareSize,squareSize,white,fp1,fp1,fp2,opacity);
  67. tempImage.draw_triangle(0,0,squareSize,0,squareSize,squareSize,white,fp1,fp2,fp2,opacity);
  68. outImages[imageNo].draw_image(x,y,tempImage);
  69. }
  70. void ImgManipBS::EvaluateImage() {
  71. printf("Starting to evaluate image....\n\n");
  72. for(auto yy = 0; yy < noOfHSquares; yy++)
  73. {
  74. for (auto xx = 0; xx < noOfWSquares; xx++ )
  75. {
  76. int x = (xx*squareSize) + squareMargin;
  77. int y = (yy*squareSize) + squareMargin;
  78. double mW,mB;
  79. findNearValues(x,y,mW,mB);
  80. whiteMeanValues[xx][yy] = mW;
  81. blackMeanValues[xx][yy] = mB;
  82. //printf("%d,%d #%x%x%x ",x,y,(int)mW,(int)mW,(int)mW);*/
  83. //MarkPixelInOutImage(0, x,y);
  84. }
  85. }
  86. printf("\n");
  87. }
  88. void ImgManipBS::findNearValues(int x,int y, double &meanWhite, double &meanBlack) {
  89. std::list <double> brList;
  90. for(auto yy=y-(measureSquareSize/2);yy<=y+(measureSquareSize/2);yy++) {
  91. for(auto xx=x-(measureSquareSize/2);xx<=x+(measureSquareSize/2);xx++) {
  92. double br = GetBrightness(xx,yy);
  93. brList.push_back( br );
  94. //MarkPixelInOutImage(0, xx,yy);
  95. }
  96. }
  97. brList.sort();
  98. // Iterate through the list from the front (most black)
  99. std::list<double>::iterator it=brList.begin();
  100. double brSum=0;
  101. int cnt=0;
  102. for( auto i=0; i < noOfMeanValues && it != brList.end(); i++ ) {
  103. brSum += *it;
  104. cnt++;
  105. it++;
  106. }
  107. meanBlack = brSum / cnt;
  108. // Iterate through the list from the end (most white)
  109. it=brList.end();
  110. brSum=0;
  111. cnt=0;
  112. for( auto i=0; i < noOfMeanValues && it != brList.begin(); i++ ) {
  113. brSum += *it;
  114. cnt++;
  115. it--;
  116. }
  117. meanWhite = brSum / cnt;
  118. }
  119. void ImgManipBS::MarkPixelInOutImage(int imageNo,int x,int y) {
  120. outImages[imageNo](x,y,0,0) = 255;
  121. outImages[imageNo](x,y,0,1) = 255;
  122. //outImages[imageNo](x,y,0,2) = 255;
  123. }
  124. double ImgManipBS::GetBrightness(unsigned int x,unsigned int y) {
  125. return( (mainImage(x,y,0,0)+mainImage(x,y,0,1)+mainImage(x,y,0,2))/3 );
  126. }