Posts tagged ‘opencv’

Detecting the Dominant points on an image using OpenCV

To detect the dominant points within an image first we must find the edges. In this example the edges are found using cvFindContours. The resulting contours are then processed to find the dominant points along the contour. This is done using the cvFindDominantPoints function, this function implements the IPAN99 algorithm to find the points. A small circle is then drawn at each dominant point.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"

int _tmain(int argc, _TCHAR* argv[])
{
	// open and display input image  
    IplImage* input = cvLoadImage("test.jpg", CV_LOAD_IMAGE_GRAYSCALE);  
    cvNamedWindow("Input", CV_WINDOW_AUTOSIZE);  
    cvShowImage("Input", input); 

	// create gray scale image for edge detection
	IplImage* edge = cvCreateImage(cvGetSize(input), 8,1);

	// create output image
	IplImage* output = cvCreateImage(cvGetSize(input), 8,1);

	// threshold the input image
	cvThreshold(input, edge, 230,255, CV_THRESH_BINARY);
	cvNamedWindow("Threshold", CV_WINDOW_AUTOSIZE);
	cvShowImage("Threshold", edge);

	// generate the contours
	CvMemStorage* storage = cvCreateMemStorage();
	CvSeq* contours = NULL;
	int Nc = cvFindContours(edge, storage, &contours, sizeof(CvContour), CV_RETR_LIST);

	// diplay the contours
	printf("Total contours found = %d\n", Nc);
	cvDrawContours(output, contours, cvScalarAll(255),cvScalarAll(255),10); 

	// generate the dominant points
	CvMemStorage* dominantstorage = cvCreateMemStorage();
	CvSeq* dominant = cvFindDominantPoints(contours, dominantstorage, CV_DOMINANT_IPAN,5,15,5,170);

	printf("dominant total=%d\n", dominant->total);

	// display the dominant points
	int i, idx;
    CvPoint p;
	for ( i = 0; i < dominant->total; i++)
    {
        idx = *(int *) cvGetSeqElem(dominant, i);
        p = *(CvPoint *) cvGetSeqElem(contours, idx);
        cvDrawCircle( output, p , 1, CV_RGB(255,0,0) );
        printf("%d %d %d\n", idx, p.x, p.y);
    }
	
	// show output
	cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output", output);

	// wait for user
	cvWaitKey(0); 

	// garbage collection	
	cvReleaseImage(&input);
	cvDestroyWindow("Input");
	cvReleaseImage(&edge);
	cvDestroyWindow("Threshold");
	cvReleaseImage(&output);
	cvDestroyWindow("Output");
	return 0;
}

Input Image

threshold

After threshold

Output

Splitting multichannel images into RGB using OpenCV

To separate a multi channel image into the three component RGB channels, we can use the cvSplit function. The example below opens a RGB image and then using the cvSplit function creates three output images.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
	// open and display input image  
    IplImage* input = cvLoadImage("test.jpg");  
    cvNamedWindow("Input", CV_WINDOW_AUTOSIZE);  
    cvShowImage("Input", input); 

	// create the output images
	IplImage* output_r = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);
	IplImage* output_g = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);
	IplImage* output_b = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);

	// split the image
	cvSplit(input, output_r, output_g, output_b,NULL);
	
	// display image
	cvNamedWindow("Output R", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output R", output_r);

	cvNamedWindow("Output G", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output G", output_g);

	cvNamedWindow("Output B", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output B", output_b);

	// wait for user
	cvWaitKey(0);

	// garbage collection	
	cvReleaseImage(&input);
	cvDestroyWindow("Input");
	cvReleaseImage(&output_r);
	cvDestroyWindow("Output R");
	cvReleaseImage(&output_g);
	cvDestroyWindow("Output G");
	cvReleaseImage(&output_b);
	cvDestroyWindow("Output B");
	return 0;
}

Input Image

Blue Channel

Green Channel

Red Channel

OpenCV Hello World

Here is the Hello World example code for OpenCV. This simple example creates a image called output, then the text “Hello World” is added to the image.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
	// create image
	IplImage* output = cvCreateImage(cvSize(400, 200), 8, 3);
	
	// create font and add text to the image
	CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1,1,0,1,8);
	cvPutText(output, "Hello World", cvPoint(100,100), &font, cvScalar(255,255,0));
	
	// display image
	cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output", output);

	// wait for user
	cvWaitKey(0);

	// garbage collection	
	cvReleaseImage(&output);
	cvDestroyWindow("Output");
	
	return 0;
}

Hello World output image