fork(2) download
  1. //-----------------------------------------------------------------------------
  2. // Programmer: Braden Andros
  3. // Name: hw5.cpp
  4. // Description: TODO
  5. //-----------------------------------------------------------------------------
  6. #include "Image.h"
  7. #include <iostream>
  8. #include <stack>
  9. using namespace std;
  10.  
  11. // define struct to hold pixel location
  12. struct PixelLocation {
  13. int r;
  14. int c;
  15. };
  16.  
  17.  
  18. // possible function prototypes that you may consider using (optional):
  19. bool findPixelLocationWithGivenValue(const Image &image, int pixelValue, int &foundRow, int &foundCol);
  20. int markConnectedComponent(Image &image, int seedRow, int seedCol, int ccLabel);
  21.  
  22. int main( int argc, char* argv[] )
  23. {
  24. // get input/output image file names from command line
  25. if (argc != 4)
  26. {
  27. std::cout << "Usage instructions: " << std::endl;
  28. std::cout << "> hw5.exe inputFileName.bmp thresholdedOutputFileName.bmp largeCCOutputFileName.bmp" << std::endl;
  29. return -1;
  30. }
  31. std::string inputFileName(argv[1]);
  32. std::string thresholdedOutputFileName(argv[2]);
  33. std::string largeCCOutputFileName(argv[3]);
  34.  
  35. // read image from input file
  36. std::cout << "Reading input image: " << inputFileName << std::endl;
  37. Image myImage;
  38. bool success = myImage.readFromBMPFile(inputFileName);
  39. if (! success)
  40. {
  41. std::cout << "Error reading input image." << std::endl;
  42. return -1;
  43. }
  44.  
  45.  
  46. const int THRESHOLD = 128;
  47. const int LARGE = 500;
  48. int r = myImage.getNumRows();
  49. int c = myImage.getNumCols();
  50.  
  51. for (int i=0; i<r; i++)
  52. {
  53. for (int j=0; j<c; j++)
  54. {
  55. if (myImage.getPixel(i,j)>THRESHOLD)
  56. {
  57. myImage.setPixel(i, j, 255);
  58. }
  59. else
  60. {
  61. myImage.setPixel(i, j, 0);
  62. }
  63.  
  64. }
  65. }
  66.  
  67. myImage.writeToBMPFile(thresholdedOutputFileName);
  68.  
  69.  
  70. int seedRow=0;
  71. int seedCol=0;
  72. int largeLabel=0;
  73. int connectedLabel=1;
  74. int val=0;
  75.  
  76. while (findPixelLocationWithGivenValue(myImage, 255, seedRow, seedCol))
  77. {
  78. val = markConnectedComponent(myImage, seedRow, seedCol, connectedLabel);
  79. connectedLabel++;
  80.  
  81. if (val>=500)
  82. {
  83. largeLabel++;
  84. }
  85. else
  86. {
  87. myImage.setAllPixelsWithOldValToNewVal(255,0);
  88. }
  89. }
  90.  
  91. myImage.switchToRandomColorMapping();
  92.  
  93. myImage.writeToBMPFile(largeCCOutputFileName);
  94.  
  95.  
  96. return 0;
  97. }
  98.  
  99. bool findPixelLocationWithGivenValue(const Image &image, int pixelValue, int &foundRow, int &foundCol)
  100. {
  101.  
  102. int r = image.getNumRows();
  103. int c = image.getNumCols();
  104.  
  105. for (int i = foundRow; i<r; i++)
  106. {
  107. for (int j = foundCol; j<c; j++)
  108. {
  109.  
  110. if (image.getPixel(i,j)==pixelValue)
  111. {
  112. foundRow=i;
  113. foundCol=j;
  114. return true;
  115. }
  116.  
  117. }
  118. }
  119. return false;
  120. }
  121.  
  122. int markConnectedComponent(Image &image, int seedRow, int seedCol, int ccLabel)
  123. {
  124. int numPixels=0;
  125.  
  126. stack<PixelLocation> pixelStack;
  127.  
  128. PixelLocation currPixel = {seedRow, seedCol};
  129.  
  130. const int seedVal = image.getPixel(seedRow, seedCol);
  131.  
  132. pixelStack.push(currPixel);
  133.  
  134. while (!pixelStack.empty())
  135. {
  136. currPixel = pixelStack.top();
  137.  
  138. PixelLocation neighbors[] = {{currPixel.r+1, currPixel.c},{currPixel.r-1, currPixel.c}, {currPixel.r, currPixel.c+1}, {currPixel.r, currPixel.c-1}};
  139.  
  140. size_t numNeighbors = sizeof(neighbors) / sizeof(*neighbors);
  141.  
  142. pixelStack.pop();
  143.  
  144. if (seedVal==image.getPixel(currPixel.r, currPixel.c))
  145. {
  146.  
  147. image.setPixel(currPixel.r, currPixel.c, ccLabel);
  148. for (int i=0; i < numNeighbors; i++)
  149. {
  150. numPixels++;
  151.  
  152. if (image.isInBounds(neighbors[i].r, neighbors[i].c))
  153.  
  154. {
  155.  
  156. pixelStack.push(neighbors[i]);
  157. numPixels++;
  158.  
  159. }
  160. }
  161.  
  162. }
  163. }
  164. return numPixels;
  165. }
  166.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:19: error: Image.h: No such file or directory
prog.cpp:19: error: expected ‘,’ or ‘...’ before ‘&’ token
prog.cpp:19: error: ISO C++ forbids declaration of ‘Image’ with no type
prog.cpp:20: error: ‘Image’ was not declared in this scope
prog.cpp:20: error: ‘image’ was not declared in this scope
prog.cpp:20: error: expected primary-expression before ‘int’
prog.cpp:20: error: expected primary-expression before ‘int’
prog.cpp:20: error: expected primary-expression before ‘int’
prog.cpp:20: error: initializer expression list treated as compound expression
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:37: error: ‘Image’ was not declared in this scope
prog.cpp:37: error: expected `;' before ‘myImage’
prog.cpp:38: error: ‘myImage’ was not declared in this scope
prog.cpp:78: error: ‘markConnectedComponent’ cannot be used as a function
prog.cpp:47: warning: unused variable ‘LARGE’
prog.cpp: At global scope:
prog.cpp:99: error: expected ‘,’ or ‘...’ before ‘&’ token
prog.cpp:99: error: ISO C++ forbids declaration of ‘Image’ with no type
prog.cpp: In function ‘bool findPixelLocationWithGivenValue(int)’:
prog.cpp:102: error: ‘image’ was not declared in this scope
prog.cpp:105: error: ‘foundRow’ was not declared in this scope
prog.cpp:107: error: ‘foundCol’ was not declared in this scope
prog.cpp:110: error: ‘pixelValue’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:122: error: redefinition of ‘int markConnectedComponent’
prog.cpp:20: error: ‘int markConnectedComponent’ previously defined here
prog.cpp:122: error: ‘Image’ was not declared in this scope
prog.cpp:122: error: ‘image’ was not declared in this scope
prog.cpp:122: error: expected primary-expression before ‘int’
prog.cpp:122: error: expected primary-expression before ‘int’
prog.cpp:122: error: expected primary-expression before ‘int’
stdout
Standard output is empty