Thursday, June 16, 2022

How to Convert RGB Image to Gray

How to Convert RGB Image to Gray

There are many many ways to convert RGB image to gray color. In this blog, I am wring a C++ code to do this. 

Prerequisites

  • Install OpenCV Libraries (https://sltechgeekx.blogspot.com/2022/06/how-to-installing-opencv-from-source.html)
  • Eclipse C++ development application  

Create New Project

Open Eclipse C/C++ and create a new C++ Project call “ConvertImageToGrayscale

File -> New -> C++ Project

Include OpenCV Path

Right Click on the Project -> Properties -> C/C++ Build -> Settings -> Includes



Add opencv installed path


Include OpenCV Library Path

Include opencv libs to your project



Add New Source File

  • Add new folder call “src” to project
    • Right Click on the Project -> New -> Folder -> Enter the name
  • Add Source file call “ConvertImageToGrayscale.cpp” to that “src” folder.
    • Right Client on src folder -> New -> Source File -> Enter the Name


Source Code

/*
 *  ConvertImageToGrayscale.cpp
 *
 *  Created on: Jun 15, 2022
 *  Author: viduranga
 */

#include <opencv2/opencv.hpp>
#include <unistd.h>

#ifndef __has_include
  static_assert(false, "__has_include not supported");
#else
#  if __cplusplus >= 201703L && __has_include(<filesystem>)
#    include <filesystem>
     namespace fs = std::filesystem;
#  elif __has_include(<experimental/filesystem>)
#    include <experimental/filesystem>
     namespace fs = std::experimental::filesystem;
#  elif __has_include(<boost/filesystem.hpp>)
#    include <boost/filesystem.hpp>
     namespace fs = boost::filesystem;
#  endif
#endif

using namespace cv;
using fs::current_path;

int main(int argc, char** argv)
{
char* ImageFile = argv[1];
Mat original_image;  /* mat object for storing original_image */
original_image = imread( ImageFile, IMREAD_COLOR ); /* read ImageFile */

if( argc != 2 || !original_image.data )
{
   printf( " No image data \n " );
   return -1;
}

char *cwd = get_current_dir_name();  /* Get Current working Directory */
std::string CurrentWorkingDirectory(cwd);
std::string InputFilePath(ImageFile);
std::string input_filename = InputFilePath.substr(InputFilePath.find_last_of("/\\") + 1); /* Get Path of input file */
std::string input_folder_path = InputFilePath.substr(0,InputFilePath.find_last_of("\\/"));  /* Get Folder Path of input file */
std::string OutputFile;

if (input_filename != InputFilePath)
{
OutputFile = input_folder_path.append("/greyImage-");
}
else {
OutputFile = CurrentWorkingDirectory.append("/greyImage-");
}

OutputFile = OutputFile.append(input_filename);

Mat gray_image; /* mat object for storing gray_image */
cvtColor( original_image, gray_image, COLOR_BGR2GRAY ); /* convert image from color to gray */

imwrite( OutputFile, gray_image );

namedWindow( "Original Image", WINDOW_AUTOSIZE );  /* set window name for Original Image */
namedWindow( "Gray Image", WINDOW_AUTOSIZE );   /* set window name Gray Image */

imshow( "Original Image", original_image );   /* show window containing original_image */
imshow( "Gray Image", gray_image );   /* show window containing gray_image */

waitKey(0);      /* to exit */

return 0;
}

Build the Project and Run

Right Click on the Project -> Build Project


Run the executable file. You have put image file as command line argument.

No comments:

Post a Comment