RSS

Tag Archives: opencv

Image Morphology using OpenCV

1. Erosion

Erosion

Erosion

generally decreases the sizes of objects and removes small anomalies by subtracting objects with a radius smaller than the structuring element.

void cvErode( const CvArr* src, CvArr* dst,
IplConvKernel*element=NULL, int iterations=1 );

The function describes:
src
-Source image
dst
-Destination image
element
-Structuring element used for erosion. If it is NULL, a 3×3 rectangular
structuring element is used
iterations
-Number of times erosion is applied

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

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("Image2.png");
IplImage *dest =cvCreateImage(cvSize(img->width,img->height),8,3);
IplImage *temp =cvCreateImage(cvSize(img->width,img->height),8,3);

cvErode(img,dest,NULL,2);

cvNamedWindow("Image:");
cvNamedWindow("Destination:");

cvShowImage("Image:", img);
cvShowImage("Destination:", dest);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvDestroyWindow("Destination:");

cvReleaseImage(&img);
cvReleaseImage(&dest);
return 0;
}

2. Dilation

Dilation

Dilation

generally increases the sizes of objects, filling in holes and broken areas, and connecting areas that are separated by spaces smaller than the size of the structuring element.

void cvDilate( const CvArr* src, CvArr* dst, IplConvKernel*
element=NULL, int iterations=1 );

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

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("Image2.png");
IplImage *dest =cvCreateImage(cvSize(img->width,img->height),8,3);
IplImage *temp =cvCreateImage(cvSize(img->width,img->height),8,3);

cvDilate(img,dest,NULL,2);

cvNamedWindow("Image:");
cvNamedWindow("Destination:");

cvShowImage("Image:", img);
cvShowImage("Destination:", dest);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvDestroyWindow("Destination:");

cvReleaseImage(&img);
cvReleaseImage(&dest);
return 0;
}

3. MorphologyEx

This function performs advanced morphological transformations, which enables composite operations such as Opening, Closing, Morphological Gradient, Top Hat and Black Hat. The one function that lets you do all this is:
void cvMorphologyEx( const CvArr* src, CvArr* dst, CvArr* temp,
IplConvKernel* element, int operation, int iterations=1 );

The function describes:
src
-Source image
dst
-Destination image
temp
-Temporary image, required in some cases
element
-Structuring element
operation
-Type of morphological operation, one of:
CV_MOP_OPEN - opening
CV_MOP_CLOSE - closing
CV_MOP_GRADIENT - morphological gradient
CV_MOP_TOPHAT - "top hat"
CV_MOP_BLACKHAT - "black hat"
iterations
-Number of times erosion and dilation are applied

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

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("Image2.png");
IplImage *dest =cvCreateImage(cvSize(img->width,img->height),8,3);
IplImage *temp =cvCreateImage(cvSize(img->width,img->height),8,3);

cvMorphologyEx(img,dest,temp,structure,MOP_CLOSE,1);
cvNamedWindow("Image:");
cvNamedWindow("Destination:");

cvShowImage("Image:", img);
cvShowImage("Destination:", dest);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvDestroyWindow("Destination:");

cvReleaseImage(&img);
cvReleaseImage(&dest);
return 0;
}
Advertisements
 
Leave a comment

Posted by on April 5, 2013 in C

 

Tags: ,

Edge Detection with OpenCV

Sobel

Edge detection is considered as the most common approach for detecting meaningful discontinuities in the grey- level.

1. Sobel Operator

void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder,
int aperture_size=3 );

Parameters:
src – Source image
dst – Destination image
xorder – First Order derivative in x direction
yorder – First Order derivative in y direction
apertureSize – Size of the extended Sobel kernel, must be 1, 3, 5 or 7

The function is called with (xorder=1, yorder=0, aperture_size=3)
|-1 0 1|
|-2 0 2|
|-1 0 1|

and (xorder=0, yorder=1, aperture_size=3)

|-1 -2 -1|
| 0 0 0|
| 1 2 1|

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("building.jpg");

IplImage *gray;
IplImage *sobelX;
IplImage *sobelY;
IplImage *tempX;
IplImage *tempY;

gray =cvCreateImage(cvSize(img->width,img->height),8,1);

cvCvtColor(img,gray,CV_RGB2GRAY);
if(!img)
{
printf("Error :coudn't open the image file.\n");
return 1;
}

sobelX =cvCreateImage(cvSize(img->width,img->height),8,1);
sobelY =cvCreateImage(cvSize(img->width,img->height),8,1);

// Create Temp Images
tempX =cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_16S,1);
tempY =cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_16S,1);

cvSobel(gray,tempX,1,0,3);
cvSobel(gray,tempY,0,1,3);

// Convert to the Absolute Value
cvConvertScaleAbs(tempX,sobelX,1,0);
cvConvertScaleAbs(tempY,sobelY,1,0);

cvNamedWindow("Image:",1);
cvShowImage("Image:", img);

cvNamedWindow("Image:",1);
cvShowImage("Gray:", gray);

cvNamedWindow("sobelx:",1);
cvNamedWindow("sobely:",1);

cvShowImage("sobelx:", sobelX);
cvShowImage("sobely:", sobelY);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvDestroyWindow("Gray:");
cvDestroyWindow("sobelx:");
cvDestroyWindow("sobely:");

cvReleaseImage(&img);
cvReleaseImage(&gray);
cvReleaseImage(&sobelX);
cvReleaseImage(&sobelY);
return 0;
}

1. LaplacianOperator

The function calculates the Laplacian of the source image by filtering the image with the
following 3X3 aperture:

|-1 -1 -1|
| -1 8 -1|
|-1 -1 -1|

Use the following function to apply the Laplacian operator:

void cvLaplace(const CvArr* src, CvArr* dst, int apertureSize=3)

Parameters:
src – Source image
dst – Destination image
apertureSize – Size of the extended Sobel kernel

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("building.jpg");

IplImage *gray;
IplImage *laplace;
IplImage *temp;


gray =cvCreateImage(cvSize(img->width,img->height),8,1);
cvCvtColor(img,gray,CV_RGB2GRAY);

temp =cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_16S,1);
laplace =cvCreateImage(cvSize(img->width,img->height),8,1);

IplImage *dst1=cvCreateImage(cvSize(img->width,img->height),8,1);

cvLaplace(gray,temp,3);
cvConvertScaleAbs(temp,laplace,1,0);

cvSmooth( laplace, dst1,CV_MEDIAN,3,3);

cvNamedWindow("Image:",1);
cvShowImage("Image:", img);

//cvNamedWindow("temp:",1);
cvNamedWindow("Laplace:",1);
cvNamedWindow("remove:",1);

cvShowImage("Laplace:", laplace);
cvShowImage("remove:", dst1);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvDestroyWindow("Laplace:");
cvDestroyWindow("remove:");


//cvReleaseImage(&img);
cvReleaseImage(&laplace);
cvReleaseImage(&dst1);
return 0;
}

 
1 Comment

Posted by on April 2, 2013 in C

 

Tags: ,

How to use Order Statistic Filters with OpenCV

Order Statistic Filters

Order Statistic Filters

Order Statistic filters are filters whose response is based on ordering/ranking the pixels
containing in the 3×3 window.
1. Median filter

The value of the centre pixel is replaced by the median value of its neighbourhood pixels. The median is taken after arranging the pixel values in ascending order and then taking the middle value. Use the following function,

void cvSmooth( const CvArr* src,
CvArr* dst,
int smoothtype=CV_MEDIAN,
int param1=3, int param2=3);

CV_MEDIAN (median blur) – finding median of param1×param1 neighborhood (the neighborhood is square).
2. Max filter
The max filter uses the maximum value of its neighbourhood pixels to replace the centre pixel value.

void cvDilate(const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1)

The function dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken. Image Understanding and Processing (OpenCV)

3. Minimum filter
The minimum filter uses the minimum value of its neighbourhood pixels to replace the centre pixel value.

void cvErode(const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1)

The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken.

Sample Code

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

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("apple noise.jpg");
IplImage *dst1=cvCreateImage(cvSize(img->width,img->height),8,3);

//cvSmooth(img,dst,CV_MEDIAN,3,3); Median Filter
//cvDilate(img,dst,NULL,1); Max Filter
//cvErode(img,dst,NULL,1); //Max FiltercvNamedWindow

cvNamedWindow("Image:");
cvShowImage("Image:", img);

cvNamedWindow("DST:");
cvShowImage("DST:", dst1);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvReleaseImage(&img);

cvDestroyWindow("DST:");
cvReleaseImage(&dst1);

return 0;
}

 
Leave a comment

Posted by on March 31, 2013 in C

 

Tags: ,

Image Difference with OpenCV

First Image

First Image

Second Image

Second Image

Image Difference

Image Difference

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

int _tmain(int argc, _TCHAR* argv[])
 {

IplImage *first = cvLoadImage("C:\\Users\\first.jpg");
 IplImage *second = cvLoadImage("C:\\Users\\second.jpg");

IplImage *subImage;
 subImage = cvCloneImage(first);

cvAbsDiff(first,second,subImage);
 cvNamedWindow("Original:");
 cvShowImage("Original:", first);

cvNamedWindow("Modified:");
 cvShowImage("Modified:", second);

cvNamedWindow("Diff:");
 cvShowImage("Diff:", subImage);

cvWaitKey(0);
 cvDestroyWindow("Original:");
 cvReleaseImage(&first);
 cvDestroyWindow("Modified:");
 cvReleaseImage(&second);
 cvDestroyWindow("Diff:");
 cvReleaseImage(&subImage);
 return 0;

}

 
Leave a comment

Posted by on February 12, 2013 in C

 

Tags:

OpenCV Histogram Tutorial

Histogram is a graphical representation of the intensity distribution of an image.

Original Image

Original Image

Grayscale Image

Grayscale Image

Histogram

Histogram

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

int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("C:\\Users\\gamma.jpg");

IplImage *gray = cvCreateImage(cvSize(img->width,img->height),8,1);

cvCvtColor(img,gray,CV_RGB2GRAY);
cvNamedWindow("Image:",1);
cvShowImage("Image:", img);

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

//create a rectangular area
CvRect rect = cvRect(0,0,500,600);
//CvRect rect = cvRect(x,y,width,height)

//apply the rectangular to the image and establish a region of interest
cvSetImageROI(gray,rect);
//gray=pointer to the image(gray pointer)
//rect=nameROI

float range[]={0,255};
float *ranges[]={range};
int hist_size = 256;

//create an image to hold the histogram
IplImage*histImage = cvCreateImage(cvSize(320,200),8,1);
//create a histogram to the information from the image
CvHistogram *hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
//1=no of dimentions
//&hist_size=size of dimention
//CV_HIST_ARRY=how to store data in histogram(multi dimentional array)
//ranges=ranges of values for dimention(0,255)
//1=uniform flag(sub intervals should be same)

//calculate the histogram
cvCalcHist(&gray,hist,0,NULL);
//&gray=source
//hist=pointer to the histogram
//0=accumilation flag(if set,hist is not clear at the begining)
//NULL=operational mask

float min_value,max_value=0;
int min_idx,max_idx=0;
//Grab Min/Max Values
cvGetMinMaxHistValue(hist,&min_value,&max_value,&min_idx,&max_idx);
//hist=pointer to the histogram

//Scale the bin values to fit to image representation
//remove higher values
cvScale(hist->bins, hist->bins,((double)histImage->height)/max_value,0);

//hist->bins=source to be scared
//hist->bins=destination
//use of same vales is manupilate source values and put them in the same location

//Set Up Factors For Visualization(set all histogram values to 255)
cvSet(histImage,cvScalarAll(255),0);
//create a factor for scaling along the width
int bin_w=cvRound((double)histImage->width/hist_size);

//draw the values
int i;
for(i=0;i<hist_size;i++)
{
//draw the histogram data on to the histogram image
cvRectangle(histImage,cvPoint(i*bin_w,histImage->height),cvPoint((i+1)*bin_w,histImage->height-cvRound(cvGetReal1D(hist->bins,i))),cvScalarAll(0),-1,8,0);
//cvScalarAll(0)=draw in black color
//-1=thickness of the line(filled rectangal)
//8=line type(connected)
//0=no of fractional points
}

cvNamedWindow("Histogram:",1);
cvShowImage("Histogram:", histImage);

cvWaitKey(0);
cvDestroyWindow("Image:");
cvReleaseImage(&img);

cvDestroyWindow("Gray:");
cvReleaseImage(&gray);

cvDestroyWindow("Histogram:");
cvReleaseImage(&histImage);

return 0;

}
 
Leave a comment

Posted by on February 10, 2013 in C

 

Tags: ,

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


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

 

 
Leave a comment

Posted by on February 7, 2013 in C

 

Tags:

Create Grayscale image using OpenCV


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

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

cvNamedWindow("Original:",1);
cvShowImage("Original:",src);

cvCvtColor(src,dest,CV_RGB2GRAY);//Change from RGB to GrayScale
cvNamedWindow("Gray:",1);
cvShowImage("Gray:",dest);

cvWaitKey(0);

cvDestroyWindow("Original:");
cvReleaseImage(&src);
cvDestroyWindow("Gray:");
cvReleaseImage(&dest);
return 0;
}

 
Leave a comment

Posted by on February 5, 2013 in C

 

Tags: ,