Posts tagged ‘threshold’

Image Contour detection and display using OpenCV

In this example we threshold the image based on the position of the track bar. Then find contours on the image an display the contours as white lines.

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

// global variables
IplImage* input = NULL;
IplImage* gray = NULL;
int threshold = 100;
CvMemStorage* storage = NULL;

/** trackbar event
 * @param trackbar position
 */
void on_trackbar(int)
{
	
	if (storage == NULL)
	{
		// create storage
		gray = cvCreateImage(cvGetSize(input), 8,1);
		storage = cvCreateMemStorage(0);
	}
	else
	{
		// clear storage
		cvClearMemStorage(storage);
	}

	// convert to gray scale and then threshold
	cvCvtColor(input, gray, CV_BGR2GRAY);
	cvThreshold(gray,gray,threshold,255,CV_THRESH_BINARY);

	// find the edges
	CvSeq* edges = 0;
	cvFindContours(gray, storage, &edges);
	cvZero(gray);
	if (edges)
	{
		// display the edges as whiet lines
		cvDrawContours(gray, edges, cvScalarAll(255),cvScalarAll(255),100);
	}
	cvShowImage("Input", gray);
}

/** main function
 * @param argc arguments
 * @param argv argument values
 * @return exit code
 */
int _tmain(int argc, _TCHAR* argv[])
{
	// open and display input image  
    input = cvLoadImage("test.jpg");  
    cvNamedWindow("Input", CV_WINDOW_AUTOSIZE);  
    cvShowImage("Input", input); 

	// create trackbar callback
	cvCreateTrackbar("Threshold", "Input", &threshold, 255, on_trackbar);
	on_trackbar(0);

	// wait for user
	cvWaitKey(0);

	// garbage collection	
	cvReleaseImage(&input);
	cvDestroyWindow("Input");
	return 0;
}

The input image

Threshold value 100

Threshold value 150

Threshold value 200

Threshold an Image using OpenCV

The cvThreshold function allows us to reject pixels above or below a set value while keeping the others. In this example the input image is separated into the RGB channels. Then we preform a threshold on the red channel, with a maximum value of 100. The result of this is that all the light areas of the image are removed.

#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* channel_r = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);
	IplImage* channel_g = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);
	IplImage* channel_b = cvCreateImage(cvSize(input->width, input->height), input->depth, 1);

	// split the image
	cvSplit(input, channel_r, channel_g, channel_b, NULL);
	
	// display image
	cvNamedWindow("Channel R", CV_WINDOW_AUTOSIZE);
	cvShowImage("Channel R", channel_r);
	cvSaveImage("channel_r.png", channel_r);

	// threshold red channel
	IplImage* output = cvCreateImage(cvSize(channel_r->width, channel_r->height), channel_r->depth, 1);
	cvThreshold(channel_r, output, 100,100, CV_THRESH_TRUNC);

	// display the output image
	cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
	cvShowImage("Output", output);
	cvSaveImage("output.png", output);

	// wait for user
	cvWaitKey(0);

	// garbage collection	
	cvReleaseImage(&input);
	cvDestroyWindow("Input");
	cvReleaseImage(&channel_r);
	cvDestroyWindow("Channel R");
	cvReleaseImage(&output);
	cvDestroyWindow("Output");
	return 0;
}

Input Image


The red channel, before threshold

The red channel, after threshold