RSS

Create Binary image using OpenCV

07 Feb

Welcome back, Today Let’s see how to create a binary image using OpenCV library. Before start, we should have a color image(RGB) or grayscale image to make it binary.

For RGB images : RGB  => Grayscale => Binary

For Grayscale images : Grayscale => Binary


#include "stdafx.h"
#include<opencv\cv.h>
#include<opencv\cxcore.h>
#include<opencv\highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{
IplImage *rgb = cvLoadImage("C:\\Users\\opencv3\\img\\opencv.png",1);
IplImage *gray = cvCreateImage(cvSize(rgb->width,rgb->height),8,1);

cvCvtColor(rgb,gray,CV_RGB2GRAY);//Change from RGB to GrayScale
IplImage *binary = cvCloneImage(gray);

cvNamedWindow("RGB:",1);
cvShowImage("RGB:",rgb);

cvNamedWindow("Grayscale:",1);
cvShowImage("Grayscale:",gray);

cvThreshold(gray,binary,80,255,CV_THRESH_BINARY);   //Change from Grayscale to Binary
cvNamedWindow("Binary:",1);
cvShowImage("Binary:",binary);

cvWaitKey(0);

cvDestroyWindow("RGB:");
cvReleaseImage(&rgb);
cvDestroyWindow("Grayscale:");
cvReleaseImage(&gray);
cvDestroyWindow("Binary:");
cvReleaseImage(&binary);
return 0;
}

 

Advertisements
 
Leave a comment

Posted by on February 7, 2013 in C

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: