Thursday, September 15, 2011

Thought to revive this blog after a long brake from OpenCV usage.

Friday, June 13, 2008

Getting the Histogram of an Image Using OPENCV

This code which I have written in the C++ language displays the Histogram of an Image (A selected Segment of it) and Saves the results.

Note - The coding might be lacking some functionalities..But it is been tested and working flawlessly..


/*This Program calculates and Displays the Histogram of a Segment of any image...
Author - Akila Mike Subasinghe
Web - http://akilamike.googlepages.com/
E Mail - akilamike@gmail.com */

#include
#include
#include
#include

using namespace std;

int main( int argc, char** argv )
{
const char *imageName = "lena.jpg";
IplImage *im = cvLoadImage(imageName,-1);
if(!im)
{//drop out if image not found
cout<return 1;
}

IplImage *grayImage = cvCreateImage(cvSize(im->width,im->height), IPL_DEPTH_8U,1);

cvCvtColor(im,grayImage, CV_BGR2GRAY);

cvNamedWindow("First Window", CV_WINDOW_AUTOSIZE);
cvMoveWindow("First Window", 60, 100);
cvShowImage("First Window", im);

cvNamedWindow("Second Window", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Second Window", 600, 100);
cvShowImage("Second Window", grayImage);

cvWaitKey(0);

//Creating a Rectangular Area to Evaluate
CvRect rect = cvRect(100,100,400,400);
//Applying to the Image to set the Region of Interest.
cvSetImageROI(grayImage,rect);

cvNamedWindow("Third Window", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Third Window", 100, 300);
cvShowImage("Third Window", grayImage);

cvWaitKey(0);

IplImage *histImage = cvCreateImage(cvSize(320,200),8,1); //A Image to Hold the Histogram

int bins = 256;
int hsize[] = {bins};

//max and min value & the Indeces of the histogram
float max_value = 0, min_value = 0;

//ranges - grayscale 0 to 256
float xranges[] = {0, 256};
float* ranges[] = {xranges};

CvHistogram *hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges,1);
cvCalcHist(&grayImage, hist, 0, NULL);
cvGetMinMaxHistValue(hist,&min_value, &max_value);

cout<
//Scalling the BIN vals to fit inside the image representation
cvScale(hist->bins, hist->bins,((double)histImage->height)/max_value,0);
//setting all Hist values to 255
cvSet(histImage,cvScalarAll(255),0);

//Factor for scalling on x axis
int bin_x = cvRound((double)histImage->width/bins);
//DRAWING THE HISTO. using Rectangles

for (int i=0; i{
cvRectangle(histImage,cvPoint(i*bin_x,histImage->height),
cvPoint((i+1)*bin_x, histImage->height- cvRound(cvGetReal1D(hist->bins,i))),cvScalarAll(0),1,8,0);
}

cvNamedWindow("Hist Window", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Hist Window", 200, 400);
cvShowImage("Hist Window", histImage);

cvWaitKey(0);

cvSaveImage("ConsideredPart.jpg",grayImage); //Creates a New file and saves
cvSaveImage("Histogram.jpg", histImage);

cvDestroyWindow("First Window");
cvDestroyWindow("Second Window");
cvDestroyWindow("Third Window");
cvDestroyWindow("Hist Window");

return 0;
}

Thursday, May 22, 2008

Features of OpenCV Library

Lets start publishing information relating to the functions, features and Implementation of the Library OpenCV for all beginners (Including me).