|
Ch OpenCV Package.
Example 1
The program
morphology.c
near the end of this page
is one of sample programs distributed
in OpenCV.
This program can be found in the directory
OPENCVHOME/samples/c/
or in CHHOME/package/chopencv/demos/
after
installation of
Ch OpenCV,
where OPENCVHOME is the home directory
in which OpenCV is installed
and CHHOME is the home directory for Ch such as C:\Ch.
After
Ch OpenCV
is installed,
the program is readily to run without compilation.
It can be executed in a Ch command shell by just typing the file name
at a command prompt
as shown below.
> morphology.c
It can also be executed from
a
ChSciTE IDE as shown in the figure below.
When the program is executed, it performs
morphology operations on an image through a graphical user interface (GUI)
as shown in the image below.
Click
here
for the animated dilation operation.
/* File name: morphology.c */
#include
#include
#include
#include
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[1] : "baboon.jpg";
if( (src = cvLoadImage(filename,1)) == 0 )
return -1;
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","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 an dimage data
cvReleaseImage(&src);
cvReleaseImage(&image);
cvReleaseImage(&dest);
//destroys windows
cvDestroyWindow("Opening&Closing window");
cvDestroyWindow("Erosion&Dilation window");
return 0;
}
Example 2
The program
demhist.ch
calculates the histogram of the image
baboon.jpg
using the numerical function
histogram() in Ch.
The output of the program is displayed below along with the source code.
/*****************************************************************
* This program uses function histogram() in Ch
*****************************************************************/
#include
#include
#include
#include
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[1] : 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;
}
|