Monday, January 26, 2015

Digital Image processing with c++ ( Chapter 2 ) - Convert RGB Image to Gray Scale

Today I'm going to explain about how to convert color (RGB) image to gray scale image
Gray scale is shades of grey , darkest or weakest shade is black and the lightest or strongest shade is white.It has no color components like RGB image.It has one component gray and different intensity levels in between .Gray scale intensity is stored as an 8-bit integer giving 256 possible different shades of gray from black to white.

I'm going to explain this conversion by using 2 methods

  1. By using inbuilt open CV function
  2. By using custom Algorithm

**** Note that I'm explaining these tutorials using visual studio

By using inbuilt open CV function


#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using namespace cv;

int main()
{
 Mat RGBImage;

 RGBImage =imread("c:/example_image.png");

 Mat grayScaleImage(RGBImage.size(),CV_8UC1);

 cvtColor(RGBImage,grayScaleImage,CV_RGB2GRAY);

 namedWindow("RGBImage",1);
 namedWindow("GrayScaleImage",1);

 imshow("RGBImage",RGBImage);
 imshow("GrayScaleImage",grayScaleImage);

 waitKey();
 return 0;
}

this is very easy method because inbuilt Open CV function save your day

 Mat RGBImage;
 RGBImage =imread("c:/example_image.png");

From this line you can read image and assign it to RGBImage Mat object

Mat grayScaleImage(RGBImage.size(),CV_8UC1);

From this line we can create Mat object named "grayScaleImage"
As first paramether we have to pass the size of that object and we make the same size as RGBImage by using RGBImage.size()
And also that object bit depth ,data type and number of channels need to mention as next parameter.
CV_8UC1 mean our object has 8 bit depth unsigned char data type (U) with one channel (C1)

cvtColor(RGBImage,grayScaleImage,CV_RGB2GRAY);

From this we convert our RBG image to Gray Scale image by using cvtColor function.
As first paramether we pass sourse image next we pass output image and final parameter we mentioned conversion type .
In this case we convert RGB to Gray scale for that we pass CV_RGB2GRAY as our 3rd parameter.

namedWindow("RGBImage",1);
namedWindow("GrayScaleImage",1);

imshow("RGBImage",RGBImage);
imshow("GrayScaleImage",grayScaleImage

From this line we can get our results.

By using custom Algorithm


#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using namespace cv;

int main()
{
 Mat RGBImage;

 RGBImage =imread("c:/example_image.png");

 Mat grayScaleImage(RGBImage.size(),CV_8UC1);

 int rows=RGBImage.rows;
 int cols=RGBImage.cols;
 

 for(int i=0;i<rows;i++){
  for(int j=0;j<cols;j++){

   Vec3b intensity=RGBImage.at<Vec3b>(i,j);

   int blue=intensity.val[0];
   int green =intensity.val[1];
   int red=intensity.val[2];

   grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126;

  }

 }

 namedWindow("RGBImage",1);
 namedWindow("GrayScaleImage",1);

 imshow("RGBImage",RGBImage);
 imshow("GrayScaleImage",grayScaleImage);

 waitKey();
 return 0;
}

By using these 2 for loops we can access to each and every location of Mat object (pixel of image)

 
for(int i=0;i<rows;i++){
  for(int j=0;j<cols;j++){

   Vec3b intensity=RGBImage.at<Vec3b>(i,j);

   int blue=intensity.val[0];
   int green =intensity.val[1];
   int red=intensity.val[2];

   grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126;

  }
 } 

RGB has 3 color component we can consider every pixel is 3 dimensional vector .
Therefore by using this line we can assign it to 3d vector

   Vec3b intensity=RGBImage.at<Vec3b>(i,j);
.
Next we can get each and every 3 color component value from each pixel

     int blue=intensity.val[0];
     int green =intensity.val[1];
     int red=intensity.val[2];

Then we can calculate relevant gray scale value respect to these 3 color component values by using this function

grayScaleImage.at<uchar>(i,j)=blue*0.0722+green*0.7152+red*0.2126;

Then we assign that values to grayScaleImage Mat object pixel values






If you have Any question ,complain or suggestion please leave a comment .I always like to help you. :) See you soon .Thank you.

Monday, January 19, 2015

Digital Image processing with c++ ( Chapter 1 ) - Configure eclipse with open CV


This is my first blog post so there can be many mistakes please help to to correct my mistakes and make this blog more impressive.Today I'm going to explain you how to Configure working environment for Digital image processing with C++.For that first we need to download few softwares and libraries.

Prerequisites


Java JRE
Windows 7 or later

Development tools



  • Eclipse IDE for C/C++     Download
  • MinGW C++ compiler     Download
  • cmake     Download   (make sure to download binary distributions)
  • OpenCV     Download  


  • Here I use Cmake to compile my own binaries because lots of questions going on that precompiled binaries are NOT working for well.

    Configuration


    1. Install MinGW C++ compiler. During installation , select “C++ Compiler” and “MSYS Basic System” for installation
    2. Add system PATH to "c:/mingw/bin" (Right click on my computer-> Properties->Advance system settings->Environment variable -> select path and click edit) example
    3. Install Cmake
    4. Install/unzip Opencv to C:\OpenCV\
    5. Run cmake GUI tool
    6. Choose C:\OpenCV\ as source
    7. Choose the destination, C:\OpenCV\x86 ,This is where to build the binaries
    8. Press Configure button, choose MinGW Makefiles as the generator.(then in red highlighted area We can choose many options but it's better to use default options if you are beginner)
    9. Press the Configure button again then press the Generate button
    10. Exit the cmake program when the generating is done
    11. Run the command line mode (cmd.exe) and go to the destination directory C:\OpenCV\x86 (type :- cd /OpenCV\x86)
    12. Type "mingw32-make". You can see a progress of building binaries. If the command is not found error occur problem is in system path (step 2)
    13. Add system PATH to C:\OpenCV\x86\bin (similar as step 2)
    14. Restart Computer
    15. Extract the downloaded Eclipse and run it
    16. Creating a new C++ Project from the File and New menu
    17. Select “Hello World C++ Project” under Executable for the Project Type, and MinGW GCC for the Toolchain of your New C++ Project. Type a Project Name and click the Finish button.
    18. Add “-static-libgcc -static-libstdc++” as Linker flags for your new project. This text should be added to the Linker flags field, which can be found by right-clicking on the new Project in the Project Explorer and clicking on Properties. Under the Project Properties, expand the C/C++ Build menu and click on Settings. Under the Tool Settings tab, expand the MinGW C++ Linker menu and click on Miscellaneous. Add the text to the Linker flags field, then click the Apply button. (More details ) about step 16 to 18
    19. Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add the source OpenCV folder "C:\OpenCV\build\include"
    20. Add the built OpenCV library folder, "C:\OpenCV\x86\lib" to Library search path (-L)
    21. Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) ONE BY ONE (this could vary from project to project, you can add all of them if you like or some of them just the ones that you need for your project,to run our simple project "opencv_core2410 " and "opencv_highgui2410 "):
        opencv_calib3d2410
        opencv_contrib2410
        opencv_core2410
        opencv_features2d2410
        opencv_flann2410
        opencv_gpu2410
        opencv_highgui2410
        opencv_imgproc2410
        opencv_legacy2410
        opencv_ml2410
        opencv_nonfree2410
        opencv_objdetect2410
        opencv_photo2410
        opencv_stitching2410
        opencv_video2410
        opencv_videostab2410

      *2410 mean opencv version 2.4.10 therefore this will need to change if you use other version

    22. Copy this code to your work place

      #include <opencv2/highgui/highgui.hpp>
      #include <opencv2/core/core.hpp>
      
      using namespace std;
      using namespace cv;
      
      int main()
      {
      
          Mat img=imread("c:/example_image.png");
          namedWindow("cat",WINDOW_AUTOSIZE);
          imshow("cat",img);
          waitKey(0);
          return 1;
      
      }
      

      (download this sample image and save it in c:/example_image.png)
    23. Click Build Project under the Project menu, then click Run under the Run menu.
    24. example_image.png will pop up !!!!!!!!!!!!!!

    Note :- if error occurs like this
    'cv::Exception' what(): C:\opencv\sources\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
    Problem is incorrect path to image (c:/example_image.png)