Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- In this Task of reading images we focused on every pixel value
- Here, Initially we read and store image from Directory in Grayscale format, and convert it in binary form,
- 0 pixel value for Black & 255 pixel value for White
- 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
- 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
- ##### For HORIZONTAL ARROW #####
- 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
- Then the Arrow points towards LEFT DIRECTION Else the Arrow points towards RIGHT DIRECTION
- ##### SAME ALGORITHM IS ALSO USED FOR VERTICAL ARROW #####
- */
- #include <iostream>
- #include <opencv2/opencv.hpp>
- #include <dirent.h>
- #include <fstream>
- #include <string>
- #include <algorithm>
- using namespace cv;
- using namespace std;
- // Function declaration
- void convertToBinary(cv::Mat& image);
- int sumPixelsInLastWhiteColumn(const cv::Mat& image);
- int sumPixelsInFirstWhiteColumn(const cv::Mat& image);
- int sumPixelsInFirstWhiteRow(const Mat& image);
- int sumPixelsInLastWhiteRow(const Mat& image);
- int main()
- {
- string directory = "./assets/"; // directory where images are stored
- ofstream MyFile(directory + "Output.txt"); // Output will save in Output.txt file
- string folderPath = "/home/purva/CHECK2/assets/"; // Path to the directory of images
- MyFile << "Direction of Arrows: \n";
- DIR* dir = opendir(folderPath.c_str());
- if (dir)
- {
- vector<string> imageFiles; // Vector to store image filenames
- struct dirent* entry;
- while ((entry = readdir(dir)) != nullptr)
- {
- string filename = entry->d_name;
- if (filename.find(".png") != string::npos)
- {
- imageFiles.push_back(filename);
- }
- }
- closedir(dir);
- sort(imageFiles.begin(), imageFiles.end()); // Sort the image filenames in ascending order
- for (const string& filename : imageFiles)
- {
- string imagePath = folderPath + filename; // Construct the full image path
- Mat image = imread(imagePath, IMREAD_GRAYSCALE); // Read the image and store it in mat image variable
- if (image.empty()) // Check if the image was successfully loaded
- {
- cout << "Failed to open the image file: " << imagePath << endl;
- continue;
- }
- convertToBinary(image); // Function call
- int countRowsWithWhitePixels(const Mat& image);
- int countColumnsWithWhitePixels(const Mat& image);
- int rowCount = countRowsWithWhitePixels(image);
- int colCount = countColumnsWithWhitePixels(image);
- if(rowCount > colCount){
- // cout << "Vertical Arrow" << endl;
- int sumFR = sumPixelsInFirstWhiteRow(image); // Addition of Pixels value of First Row having First_white_pixel of image
- int sumLR = sumPixelsInLastWhiteRow(image); // Addition of Pixels value of Last Row having Last_white_pixel of image
- if(sumLR > sumFR){
- MyFile << "TOP\n";
- }
- else
- MyFile << "BOTTOM\n";
- }
- else{
- // cout << "Horizontal Arrow" << endl;
- int sumLC = sumPixelsInLastWhiteColumn(image); // Addition of Pixels value of Last Column having First_white_pixel of image
- int sumFC = sumPixelsInFirstWhiteColumn(image); // Addition of Pixels value of First Column having First_white_pixel of image
- if(sumLC > sumFC){
- MyFile << "LEFT\n";
- }
- else
- MyFile << "RIGHT\n";
- }
- }
- }
- else
- {
- cout << "Failed to open the directory: " << folderPath << endl;
- return -1;
- }
- MyFile.close();
- cout << "Check your Folder for Output file" << endl;
- return 0;
- }
- void convertToBinary(Mat& image) // Function to convert a grayscale image to binary
- {
- int threshold = 127; // Threshold value to determine the binary threshold
- for (int i = 0; i < image.rows; i++) // Iterate over the pixels of the image
- {
- for (int j = 0; j < image.cols; j++)
- { // 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
- int pixel = image.at<uchar>(i, j); // Get the pixel value at (i, j)
- if (pixel > threshold) // Apply thresholding
- image.at<uchar>(i, j) = 255; // Set pixel to white
- else
- image.at<uchar>(i, j) = 0; // Set pixel to black
- }
- }
- }
- int countRowsWithWhitePixels(const Mat& image) // This function will count all Rows having white pixels, it iterates over each row of the image
- {
- int rowCount = 0; // Initially Row count will zero
- for (int i = 0; i < image.rows; i++) // image.rows is used to retrieve the number of rows in the image
- {
- const uchar* rowPtr = image.ptr<uchar>(i);
- // A lambda function (uchar pixel) { return pixel == 255; } is used to define the condition for a white pixel
- // If a row contains at least one white pixel, rowCount is incremented
- // std::any_of func is used to check if the row contains any white pixel
- // 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
- bool hasWhitePixel = any_of(rowPtr, rowPtr + image.cols, [](uchar pixel) { return pixel == 255; });
- if (hasWhitePixel)
- rowCount++;
- }
- return rowCount;
- }
- int countColumnsWithWhitePixels(const Mat& image) // This function will count all Columns having white pixels
- {
- int colCount = 0;
- // The function iterates over each column of the image using the outer for loop
- for (int j = 0; j < image.cols; j++) // image.cols is used to retrieve the number of columns in the image
- {
- bool hasWhitePixel = false;
- 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)
- {
- uchar pixel = image.at<uchar>(i, j);
- if (pixel == 255)
- {
- hasWhitePixel = true; // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true,
- break; // and the inner loop is terminated using break
- }
- }
- if (hasWhitePixel)
- colCount++;
- }
- return colCount;
- }
- 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
- {
- int firstWhiteColumn = -1;
- for (int j = 0; j < image.cols; ++j) // Find the first column with white pixels
- {
- bool hasWhitePixel = false;
- for (int i = 0; i < image.rows; ++i)
- {
- uchar pixel = image.at<uchar>(i, j);
- if (pixel == 255)
- {
- hasWhitePixel = true; // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true,
- firstWhiteColumn = j; // and the inner loop is terminated using break
- break;
- }
- }
- 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,
- break; // and both the outer and inner loops are terminated using break
- }
- if (firstWhiteColumn != -1) // this function computes the sum of pixel values in that column by iterating over each row in the column
- {
- int sumFC = 0;
- for (int i = 0; i < image.rows; ++i)
- {
- uchar pixel = image.at<uchar>(i, firstWhiteColumn);
- sumFC += pixel;
- }
- return sumFC;
- }
- return 0; // Return 0 if no white pixel is found
- }
- int sumPixelsInLastWhiteColumn(const cv::Mat& image) // This function is used to calculates sum of pixel values in the last column of the image
- {
- int lastColumn = image.cols - 1;
- int lastWhiteColumn = -1; // lastWhiteColumn as -1 to indicate that no white pixel column has been found yet
- for (int j = lastColumn; j >= 0; --j) // Find the last column with white pixels
- {
- bool hasWhitePixel = false;
- for (int i = 0; i < image.rows; ++i)
- {
- uchar pixel = image.at<uchar>(i, j);
- if (pixel == 255) // This checks if any pixel has a value of 255(indicating a white pixel)
- {
- hasWhitePixel = true; // If a white pixel is found, hasWhitePixel is set to true
- lastWhiteColumn = j; // and lastWhiteColumn is updated with the current column index
- break;
- }
- }
- if (hasWhitePixel)
- break;
- }
- if (lastWhiteColumn != -1) // this function calculates the sum of pixel values in that column by iterating over each row
- { // and accumulating the pixel values in the sumLC variable
- int sumLC = 0;
- for (int i = 0; i < image.rows; ++i)
- {
- uchar pixel = image.at<uchar>(i, lastWhiteColumn);
- sumLC += pixel;
- }
- return sumLC;
- }
- return 0; // Return 0 if no white pixel is found
- }
- 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
- {
- int firstWhiteRow = -1;
- for (int i = 0; i < image.rows; ++i) // Find the first row with white pixels
- {
- bool hasWhitePixel = false;
- 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]
- for (int j = 0; j < image.cols; ++j)
- {
- uchar pixel = rowPtr[j];
- if (pixel == 255) // If the pixel value is equal to 255 (indicating a white pixel), the hasWhitePixel flag is set to true
- {
- hasWhitePixel = true;
- firstWhiteRow = i;
- break;
- }
- }
- if (hasWhitePixel)
- break;
- }
- if (firstWhiteRow != -1) // This function checks if a white pixel row was found
- { // it calculates the sum of pixel values in that row
- int sumFR = 0;
- const uchar* rowPtr = image.ptr<uchar>(firstWhiteRow);
- for (int j = 0; j < image.cols; ++j)
- {
- uchar pixel = rowPtr[j];
- sumFR += pixel;
- }
- return sumFR; // function returns sumFR as the sum of pixel values in the first row with white pixels
- }
- return 0; // Return 0 if no white pixel is found
- }
- 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
- {
- int lastWhiteRow = -1; // -1 indicate that no white pixel row has been found yet
- 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
- {
- bool hasWhitePixel = false;
- 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
- for (int j = 0; j < image.cols; ++j)
- {
- uchar pixel = rowPtr[j]; // retrieve the pixel value at the specified column index j in the row
- if (pixel == 255)
- {
- hasWhitePixel = true;
- lastWhiteRow = i;
- break;
- }
- }
- if (hasWhitePixel)
- break;
- }
- if (lastWhiteRow != -1)
- {
- int sumLR = 0;
- const uchar* rowPtr = image.ptr<uchar>(lastWhiteRow);
- for (int j = 0; j < image.cols; ++j)
- {
- uchar pixel = rowPtr[j];
- sumLR += pixel; // it calculates the sum of pixel values in that row by iterating over each column
- }
- return sumLR; // the function returns sumLR as the sum of pixel values in the last row with white pixels
- }
- return 0; // Return 0 if no white pixel is found
- }
Advertisement
Add Comment
Please, Sign In to add comment