Thursday, February 7, 2013

Create Binary image using OpenCV

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

[sourcecode language="cpp"]

#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;
}

[/sourcecode]

 

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...