Displaying image in grayscale

OpenCV provides easy way to load image as IplImage in 3 band format, grayscale format or load image as it is(in its original format). Image loading function loads image from file as IplImage by taking 2 arguments.

  1. Image file name e.g. 'img1.png' in my case.
  2. Color code as integer.
Here is my img1.png file in original format.

We can provide 3 types of color code integers
  • 'CV_LOAD_IMAGE_COLOR'
    This will load image in 3channel color image i.e. RGB image.
    we will use following code and see what will be the result

    #include <iostream>
    #include <cv.h>
    #include <highgui.h>
    using namespace std;
    char key;
    int main()
    {
    IplImage* frame=cvLoadImage("img1.png",CV_LOAD_IMAGE_COLOR); //Load image in IplImage frame.
    cvNamedWindow("window1", CV_WINDOW_AUTOSIZE); //Create window of name 'Window1'
    if(frame == NULL){ //if image file not found then print this message
    printf("the file doesnot exist");
    exit(0);
    }
    cvShowImage("window1",frame); //Display image in Window1
    cvWaitKey(); //Wait for keyboard input
    cvDestroyWindow( "window1" ); //destroy window
    return 0;
    }

    The result will be as following image.
    The image is not actually changed as we have given RGB Image and output is also displaying RGB image.

  • 'CV_LOAD_IMAGE_GRYASCALE'
    This will load image in gray scale format.
    we will use following code and see what will be the result

    #include <iostream>
    #include <cv.h>
    #include <highgui.h>
    using namespace std;
    char key;
    int main()
    {
    IplImage* frame=cvLoadImage("img1.png",CV_LOAD_IMAGE_GRAYSCALE); //Load image in IplImage frame.
    cvNamedWindow("window1", CV_WINDOW_AUTOSIZE); //Create window of name 'Window1'
    if(frame == NULL){ //if image file not found then print this message
    printf("the file doesnot exist");
    exit(0);
    }
    cvShowImage("window1",frame); //Display image in Window1
    cvWaitKey(); //Wait for keyboard input
    cvDestroyWindow( "window1" ); //destroy window
    return 0;
    }

    The result will be as following image.
    The out put image is gray scale version of input image.

  • 'CV_LOAD_IMAGE_UNCHANGED'
    This will load image as it is.
    We can use this format if we want to load image in its original format.

No comments:

Post a Comment