Thursday, April 29, 2010

Getting Histogram for a given grayscale image

What is Histogram?

Graphical representation of frequency density of image pixel values. Its helpful in identifying the pixel distribution in an image.

Program:

  /*
 * File:   main.cpp
 * Author: Karthick
 * Created on April 2, 2010, 11:52 AM
 */


#include < cv.h >
#include < highgui.h >

using namespace std;

IplImage* image = 0;
IplImage* imgHistogram = 0;
IplImage* gray = 0;
CvHistogram* hist;


int main(int argc, char** argv) {


    if (argc != 2 || !(image = cvLoadImage(argv[1])))
        return -1;

    //size of the histogram -1D histogram
    int bins = 256;
    int hsize[] = {bins};
    //max and min value of the histogram
    float max_value = 0, min_value = 0;
    //value and normalized value
    float value;
    int normalized;
    //ranges - grayscale 0 to 256
    float xranges[] = {0, 256};
    float* ranges[] = {xranges};


    //create an 8 bit single channel image to hold a
    //grayscale version of the original picture
    gray = cvCreateImage(cvGetSize(image), 8, 1);
    cvCvtColor(image, gray, CV_BGR2GRAY);


    //Create 3 windows to show the results
    cvNamedWindow("original", 1);
    cvNamedWindow("gray", 1);
    cvNamedWindow("histogram", 1);


    //planes to obtain the histogram, in this case just one
    IplImage * planes[] = {gray};
    //get the histogram and some info about it
    hist = cvCreateHist(1, hsize, CV_HIST_ARRAY, ranges, 1);
    cvCalcHist(planes, hist, 0, NULL);

    cvGetMinMaxHistValue(hist, &min_value, &max_value);
    printf("min: %f, max: %f\n", min_value, max_value);


    //create an 8 bits single channel image to hold the histogram
    //paint it white
    imgHistogram = cvCreateImage(cvSize(bins, 200), 8, 1);
    cvRectangle(imgHistogram, cvPoint(0, 0), cvPoint(256, 200), CV_RGB(255, 255, 255), -1);
    //draw the histogram
    for (int i = 0; i < bins; i++) {
        value = cvQueryHistValue_1D(hist, i);
        normalized = cvRound(value * 200 / max_value);
        cvLine(imgHistogram, cvPoint(i, 200), cvPoint(i, 200 - normalized), CV_RGB(0, 0, 0));
        printf("%d\n", normalized);
    }


    //show the image results
    cvShowImage("original", image);
    cvShowImage("gray", gray);
    cvShowImage("histogram", imgHistogram);
    cvWaitKey();
    return (EXIT_SUCCESS);
}

Output:


My first Program

Like all the Programming Language books start with the Hello-World Program, I start my blog with a program in OpenCV to display an image..

Program:


#include < cv.h >
#include
< highgui.h >

using namespace std;

int main(int argc, char** argv)
{

    if(argc < 1 || argv[1] ==NULL)
    {
        return -1;
    }

    //Function which loads the image into an IplImage object
    IplImage* img = cvLoadImage(argv[1]);

    //Creates a window object to hold the image
    cvNamedWindow("My Picture");

    //show the image
    cvShowImage("My Picture",img);

    //Wait for some key to be pressed indefinitely
    while(1)
    {
     char c = cvWaitKey();
     //if escape character is pressed, break out of the loop
     if( c == 27 ) break;
    }

     //Release IplImage object
    cvReleaseImage( &img );

    cvDestroyWindow("My Picture");

    return(EXIT_SUCCESS);
}

Output:


Wednesday, April 14, 2010

Setting up your local machine

People who have already set up information can ignore this part.

Tools that has to be installed in your local machine:


     Please install all the above tools and you should be good to go.


Installation Steps:

When first started setting up my local machine, I followed the instructions mentioned in the below pdf link. Hopefully, the tutorials mentioned in the file, help you guys.

Thanks to the Steve Hodkin you created this file.

http://opencvtutorial.googlegroups.com/web/openCV2.0_Netbeans_6.8.pdf?gsc=FH5caS4AAAAqU9sAza7VmEhO7zTFfYOvj8-3iAnICcE50bdN256kHDoWSZIADbRm0dqobv8BkAA 


From here on wards in my blog, I will image processing concepts, and OpenCV code for that. If you have any questions about the OpenCV functions and the parameters need for the funtion, please refer to the documentation.


http://www-nh.scphys.kyoto-u.ac.jp/~hayata/opencv_doc/ref/opencvref_cxcore.htm