Advanced Imaging

AdvancedImagingPro.com

   

Industry News

Updated: July 8th, 2008 05:26 PM CDT

Ch and OpenCV: An Open Relationship With Vision

Advertisement

The prototypes of functions are as follow:
void cvErode( const CvArr* sourceImg, CvArr* DestImg, IplConvKernel*, int iterations);
void cvDilate( const CvArr* sourceImg, CvArr* DestImg, IplConvKernel* B, int iterations);

These two functions perform erosion and dilation on a source image, respectively. Parameter B is a structuring element to determine the shape of a pixel neighborhood over which the minimum is taken. If it is NULL, a 3_3 rectangular structuring element is used. Parameter iterations is the number of times erosion is applied.

Four callback functions, Opening(), Closing(), Erosion(), Dilation(), were declared in this example. For Opening(), it will carry out erosion first, then dilation. For Closing(), it goes the opposite way. Erosion() and Dilation() perform erosion and dilation, respectively. The source code of program morphology.c is as follows:

#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
//declare image object
IplImage* src = 0;
IplImage* image = 0;
IplImage* dest = 0;
//the address of variable which receives trackbar
position update int pos = 0;
//callback function for slider, implements opening
void Opening(int id)
{
id;
cvErode(src,image,NULL,pos);
cvDilate(image,dest,NULL,pos);
cvShowImage("Opening&Closing window",dest);
}
//callback function for slider, implements closing
void Closing(int id)
{
id;
cvDilate(src,image,NULL,pos);
cvErode(image,dest,NULL,pos);
cvShowImage("Opening&Closing window",dest);
}
//callback function for slider, implements erosion
void Erosion(int id)
{
id;
cvErode(src,dest,NULL,pos);
cvShowImage("Erosion&Dilation window",dest);
}
//callback function for slider, implements dilation
void Dilation(int id)
{
id;
cvDilate(src,dest,NULL,pos);
cvShowImage("Erosion&Dilation window",dest);
}
int main( int argc, char** argv )
{
char* filename = argc == 2 ? argv : (char*)"baboon.jpg";
if( (src = cvLoadImage(filename,1)) == 0 )
return -1;
//makes a full copy of the image including header, ROI and data
image = cvCloneImage(src);
dest = cvCloneImage(src);
//create windows for output images
cvNamedWindow("Opening&
Closing window",1);
cvNamedWindow("Erosion&
Dilation window",1);
cvShowImage("Opening&Closing
window",src);
cvShowImage("Erosion&Dilation
window",src);
cvCreateTrackbar("Open"nbsp"Opening&
Closing window",&pos,10,Opening);
cvCreateTrackbar("Close","Opening&
Closing window",&pos,10,Closing);
cvCreateTrackbar("Dilate","Erosion&
Dilation window",&pos,10,Dilation);
cvCreateTrackbar("Erode","Erosion&
Dilation window",&pos,10,Erosion);
cvWaitKey(0);
//releases header and image data
cvReleaseImage(&src);
cvReleaseImage(&image);
cvReleaseImage(&dest);
//destroys windows
cvDestroyWindow("Opening&Closing
window");
cvDestroyWindow("Erosion& Dilation
window");
return 0;
}

Results are shown in Figures 2 and 3. Figure 2 shows the original image. Figure 3 shows the image after dilation. The dilation iterations was set to 8.

Example 2
This example demonstrates the use of 2D/3D plotting and numerical features in Ch. Use OpenCV functions to load a color image shown in Figure 2 first, then convert it to the gray scale image shown in Figure 4. Finally, calculate and display the histogram of the gray image using function histogram() in Ch. The output of the program below is shown in Figures 4 and 5. Note that array y for pixel values is a variable length array, introduced in C99. The histogram of the gray image can be displayed, saved in a variety of different file formats and made ready for Web-based applications for display in Web browsers.

/********************************************
* This program uses function
histogram() in Ch
********************************************/
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <numeric.h>
int main(int argc, char** argv) {
char filename[] = "baboon.jpg";
char windowname[] = "Gray image";
IplImage *image1 = NULL,
*image2 = NULL;
// Load the source image.
image1 = cvLoadImage(argc == 2 ?
argv : filename, 1);
if(!image1) {
printf("Image was not loaded.\n");
return -1;
}
image2 = cvCreateImage(cvSize
(image1->width,image1->height),
IPL_DEPTH_8U, 1);
cvCvtColor(image1,image2,
CV_BGR2GRAY);
cvNamedWindow(windowname, 0);
cvShowImage(windowname, image2);
unsigned char x[128], y[image2->
imageSize];
linspace(x, 0, 255);
memcpy(y, image2->imageData,
image2->imageSize);
histogram(y, x);
cvWaitKey(0);
cvReleaseImage(&image1);
cvReleaseImage(&image2);
cvDestroyWindow(windowname);
return 0;
}

Together, Ch and OpenCV form a powerful, rapid prototyping environment for open architecture computer vision. A wide range of optimized computer vision code is encompassed in OpenCV, and a wide range of visualization and mathematical support code is encompassed by Ch. You can use these tools to iteratively experiment with computer vision but wind up with optimized production-ready code when you are done... with no need to re-code.


Subscribe to our RSS Feeds