suraj0152

TASK2_ARTIFICERS

Jul 6th, 2023
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.30 KB | Software | 0 0
  1. /*
  2.  
  3. In this Task of reading images we focused on every pixel value
  4. Here, Initially we read and store image from Directory in Grayscale format, and convert it in binary form,
  5. 0 pixel value for Black & 255 pixel value for White
  6.  
  7. We count Total no. of rows and columns that contain white pixels and use if else statement to determined whether the posiition of arrow is Horizontal or Vertical
  8. If Total number of ROWS that contain white pixels are LESS than total number of COLUMNS that contain white pixels THEN THE ARROW IS HORIZONTAL ELSE VERTICAL
  9.  
  10. ##### For HORIZONTAL ARROW #####
  11.  
  12. If Addition of Pixels value of First Column having First_white_pixel of image is LESS than Addition of all pixels value of Last Column having First_white_pixel of image
  13. Then the Arrow points towards LEFT DIRECTION Else the Arrow points towards RIGHT DIRECTION
  14.  
  15. ##### SAME ALGORITHM IS ALSO USED FOR VERTICAL ARROW #####
  16.  
  17. */
  18.  
  19.  
  20. #include <iostream>
  21. #include <opencv2/opencv.hpp>
  22. #include <dirent.h>
  23. #include <fstream>
  24. #include <string>
  25. #include <algorithm>
  26. using namespace cv;
  27. using namespace std;
  28.  
  29. // Function declaration
  30. void convertToBinary(cv::Mat& image);            
  31. int sumPixelsInLastWhiteColumn(const cv::Mat& image);
  32. int sumPixelsInFirstWhiteColumn(const cv::Mat& image);
  33. int sumPixelsInFirstWhiteRow(const Mat& image);
  34. int sumPixelsInLastWhiteRow(const Mat& image);
  35.  
  36. int main()
  37. {
  38.     string directory = "./assets/";                                // directory where images are stored
  39.     ofstream MyFile(directory + "Output.txt");                     // Output will save in Output.txt file
  40.  
  41.     string folderPath = "/home/purva/CHECK2/assets/";              // Path to the directory of images
  42.  
  43.     MyFile << "Direction of Arrows: \n";
  44.  
  45.     DIR* dir = opendir(folderPath.c_str());
  46.     if (dir)
  47.     {
  48.         vector<string> imageFiles;                                  // Vector to store image filenames
  49.  
  50.         struct dirent* entry;
  51.         while ((entry = readdir(dir)) != nullptr)
  52.         {
  53.             string filename = entry->d_name;
  54.             if (filename.find(".png") != string::npos)
  55.             {
  56.                 imageFiles.push_back(filename);
  57.             }
  58.         }
  59.         closedir(dir);
  60.  
  61.         sort(imageFiles.begin(), imageFiles.end());                  // Sort the image filenames in ascending order
  62.  
  63.         for (const string& filename : imageFiles)
  64.         {
  65.             string imagePath = folderPath + filename;                // Construct the full image path
  66.  
  67.             Mat image = imread(imagePath, IMREAD_GRAYSCALE);         // Read the image and store it in mat image variable
  68.  
  69.             if (image.empty())                                       // Check if the image was successfully loaded
  70.             {                                                                        
  71.                 cout << "Failed to open the image file: " << imagePath << endl;
  72.                 continue;
  73.             }
  74.            
  75.             convertToBinary(image);                                   // Function call
  76.        
  77.             int countRowsWithWhitePixels(const Mat& image);          
  78.             int countColumnsWithWhitePixels(const Mat& image);
  79.        
  80.             int rowCount = countRowsWithWhitePixels(image);
  81.             int colCount = countColumnsWithWhitePixels(image);
  82.        
  83.             if(rowCount > colCount){
  84.                 // cout << "Vertical Arrow" << endl;
  85.  
  86.                 int sumFR = sumPixelsInFirstWhiteRow(image);          // Addition of Pixels value of First Row having First_white_pixel of image
  87.                 int sumLR = sumPixelsInLastWhiteRow(image);           // Addition of Pixels value of Last Row having Last_white_pixel of image
  88.                
  89.                 if(sumLR > sumFR){
  90.                     MyFile << "TOP\n";
  91.                 }
  92.                 else
  93.                     MyFile << "BOTTOM\n";
  94.             }
  95.        
  96.             else{
  97.                 // cout << "Horizontal Arrow" << endl;
  98.                
  99.                 int sumLC = sumPixelsInLastWhiteColumn(image);         // Addition of Pixels value of Last Column having First_white_pixel of image
  100.                 int sumFC = sumPixelsInFirstWhiteColumn(image);        // Addition of Pixels value of First Column having First_white_pixel of image
  101.  
  102.                 if(sumLC > sumFC){
  103.                     MyFile << "LEFT\n";
  104.                 }
  105.                 else
  106.                     MyFile << "RIGHT\n";
  107.             }
  108.        
  109.         }
  110.     }
  111.     else
  112.     {
  113.         cout << "Failed to open the directory: " << folderPath << endl;
  114.         return -1;
  115.     }
  116.     MyFile.close();
  117.     cout << "Check your Folder for Output file" << endl;
  118.     return 0;
  119. }
  120.  
  121. void convertToBinary(Mat& image)                     // Function to convert a grayscale image to binary
  122. {
  123.     int threshold = 127;                             // Threshold value to determine the binary threshold
  124.     for (int i = 0; i < image.rows; i++)             // Iterate over the pixels of the image
  125.     {  
  126.         for (int j = 0; j < image.cols; j++)
  127.         {                                            // uchar data type is used to represent an 8-bit unsigned integer, which is the data type typically used for grayscale pixel values in OpenCV
  128.             int pixel = image.at<uchar>(i, j);       // Get the pixel value at (i, j)
  129.  
  130.             if (pixel > threshold)                   // Apply thresholding
  131.                 image.at<uchar>(i, j) = 255;         // Set pixel to white
  132.             else
  133.                 image.at<uchar>(i, j) = 0;           // Set pixel to black
  134.         }
  135.     }
  136. }
  137.  
  138. int countRowsWithWhitePixels(const Mat& image)       // This function will count all Rows having white pixels, it iterates over each row of the image
  139. {
  140.     int rowCount = 0;                                // Initially Row count will zero
  141.     for (int i = 0; i < image.rows; i++)             // image.rows is used to retrieve the number of rows in the image
  142.     {
  143.         const uchar* rowPtr = image.ptr<uchar>(i);
  144.        
  145.     // A lambda function (uchar pixel) { return pixel == 255; } is used to define the condition for a white pixel
  146.     // If a row contains at least one white pixel, rowCount is incremented
  147.     // std::any_of func is used to check if the row contains any white pixel
  148.     // bool is the data type used to represent the result of the condition check performed by std::any_of, and it helps determine if any white pixel is found in a row
  149.  
  150.         bool hasWhitePixel = any_of(rowPtr, rowPtr + image.cols, [](uchar pixel) { return pixel == 255; });  
  151.         if (hasWhitePixel)
  152.             rowCount++;                        
  153.     }
  154.     return rowCount;
  155. }
  156.  
  157. int countColumnsWithWhitePixels(const Mat& image)        // This function will count all Columns having white pixels
  158. {
  159.     int colCount = 0;
  160.    
  161.     // The function iterates over each column of the image using the outer for loop
  162.  
  163.     for (int j = 0; j < image.cols; j++)                // image.cols is used to retrieve the number of columns in the image
  164.     {
  165.         bool hasWhitePixel = false;
  166.         for (int i = 0; i < image.rows; i++)            // inner for loop retrieves the pixel value at position (i, j) using image.at<uchar>(i, j)
  167.         {
  168.             uchar pixel = image.at<uchar>(i, j);
  169.             if (pixel == 255)
  170.             {
  171.                 hasWhitePixel = true;                   // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true,
  172.                 break;                                  // and the inner loop is terminated using break
  173.             }
  174.         }
  175.         if (hasWhitePixel)
  176.             colCount++;
  177.     }
  178.     return colCount;
  179. }
  180.  
  181. int sumPixelsInFirstWhiteColumn(const cv::Mat& image)   //  This function is used to determines the first column with white pixels in the image and computes the sum of pixel values in that column
  182. {
  183.     int firstWhiteColumn = -1;
  184.  
  185.     for (int j = 0; j < image.cols; ++j)                // Find the first column with white pixels
  186.     {
  187.         bool hasWhitePixel = false;
  188.         for (int i = 0; i < image.rows; ++i)
  189.         {
  190.             uchar pixel = image.at<uchar>(i, j);
  191.             if (pixel == 255)
  192.             {
  193.                 hasWhitePixel = true;                   // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true,
  194.                 firstWhiteColumn = j;                   // and the inner loop is terminated using break
  195.                 break;  
  196.             }                                    
  197.         }
  198.         if (hasWhitePixel)        //  If it is true, indicating that at least one white pixel was found in the column, the firstWhiteColumn variable is updated with the column index,
  199.             break;                // and both the outer and inner loops are terminated using break
  200.     }
  201.  
  202.     if (firstWhiteColumn != -1)                         //  this function computes the sum of pixel values in that column by iterating over each row in the column
  203.     {
  204.         int sumFC = 0;
  205.         for (int i = 0; i < image.rows; ++i)
  206.         {
  207.             uchar pixel = image.at<uchar>(i, firstWhiteColumn);
  208.             sumFC += pixel;
  209.         }
  210.         return sumFC;
  211.     }
  212.     return 0;                                            // Return 0 if no white pixel is found
  213. }
  214.  
  215. int sumPixelsInLastWhiteColumn(const cv::Mat& image)         // This function is used to calculates sum of pixel values in the last column of the image
  216. {
  217.     int lastColumn = image.cols - 1;                        
  218.     int lastWhiteColumn = -1;                                // lastWhiteColumn as -1 to indicate that no white pixel column has been found yet
  219.  
  220.     for (int j = lastColumn; j >= 0; --j)                    // Find the last column with white pixels
  221.     {
  222.         bool hasWhitePixel = false;
  223.         for (int i = 0; i < image.rows; ++i)
  224.         {
  225.             uchar pixel = image.at<uchar>(i, j);
  226.             if (pixel == 255)                                // This checks if any pixel has a value of 255(indicating a white pixel)
  227.             {
  228.                 hasWhitePixel = true;                        // If a white pixel is found, hasWhitePixel is set to true
  229.                 lastWhiteColumn = j;                         // and lastWhiteColumn is updated with the current column index
  230.                 break;
  231.             }
  232.         }
  233.         if (hasWhitePixel)
  234.             break;
  235.     }
  236.  
  237.     if (lastWhiteColumn != -1)                                // this function calculates the sum of pixel values in that column by iterating over each row
  238.     {                                                         // and accumulating the pixel values in the sumLC variable
  239.         int sumLC = 0;
  240.         for (int i = 0; i < image.rows; ++i)
  241.         {
  242.             uchar pixel = image.at<uchar>(i, lastWhiteColumn);
  243.             sumLC += pixel;
  244.         }
  245.         return sumLC;
  246.     }
  247.     return 0;                                                 // Return 0 if no white pixel is found
  248. }
  249.  
  250. int sumPixelsInFirstWhiteRow(const cv::Mat& image) // This function is used to computes the sum of pixel values in the first row of the image that contains white pixels
  251. {
  252.     int firstWhiteRow = -1;
  253.  
  254.     for (int i = 0; i < image.rows; ++i)                      // Find the first row with white pixels
  255.     {
  256.         bool hasWhitePixel = false;
  257.         const uchar* rowPtr = image.ptr<uchar>(i); // it retrieves the pixel value at position (i, j), and obtain a pointer to the current row, and then accessing the pixel value using rowPtr[j]
  258.         for (int j = 0; j < image.cols; ++j)
  259.         {
  260.             uchar pixel = rowPtr[j];
  261.             if (pixel == 255)                                 // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true
  262.             {
  263.                 hasWhitePixel = true;
  264.                 firstWhiteRow = i;
  265.                 break;
  266.             }
  267.         }
  268.         if (hasWhitePixel)
  269.             break;
  270.     }
  271.  
  272.     if (firstWhiteRow != -1)                                  // This function checks if a white pixel row was found
  273.     {                                                         // it calculates the sum of pixel values in that row
  274.         int sumFR = 0;
  275.         const uchar* rowPtr = image.ptr<uchar>(firstWhiteRow);
  276.         for (int j = 0; j < image.cols; ++j)
  277.         {
  278.             uchar pixel = rowPtr[j];
  279.             sumFR += pixel;
  280.         }
  281.         return sumFR;                                          // function returns sumFR as the sum of pixel values in the first row with white pixels
  282.     }
  283.  
  284.     return 0;                                                  // Return 0 if no white pixel is found
  285. }
  286.  
  287. int sumPixelsInLastWhiteRow(const cv::Mat& image)  //This function is used to calculates the sum of pixel values in the last row of the image that contains white pixels
  288. {
  289.     int lastWhiteRow = -1;                               // -1 indicate that no white pixel row has been found yet
  290.  
  291.     for (int i = image.rows - 1; i >= 0; --i)            // it iterates over each row of the image starting from the last row and going backwards
  292.     {
  293.         bool hasWhitePixel = false;                  
  294.         const uchar* rowPtr = image.ptr<uchar>(i);       // image.ptr<uchar>(i) function is used to obtain a pointer to the specified row (i) in a image
  295.         for (int j = 0; j < image.cols; ++j)
  296.         {
  297.             uchar pixel = rowPtr[j];                     // retrieve the pixel value at the specified column index j in the row
  298.             if (pixel == 255)
  299.             {
  300.                 hasWhitePixel = true;
  301.                 lastWhiteRow = i;
  302.                 break;
  303.             }
  304.         }
  305.         if (hasWhitePixel)
  306.             break;
  307.     }
  308.  
  309.     if (lastWhiteRow != -1)
  310.     {
  311.         int sumLR = 0;
  312.         const uchar* rowPtr = image.ptr<uchar>(lastWhiteRow);
  313.         for (int j = 0; j < image.cols; ++j)
  314.         {
  315.             uchar pixel = rowPtr[j];
  316.             sumLR += pixel;                              // it calculates the sum of pixel values in that row by iterating over each column
  317.         }
  318.         return sumLR;                                    // the function returns sumLR as the sum of pixel values in the last row with white pixels
  319.     }
  320.  
  321.     return 0;                // Return 0 if no white pixel is found
  322. }
Advertisement
Add Comment
Please, Sign In to add comment