In the last episode of this short introduction to Opencv and Processing I want to show an algorithm that allows  to appreciate how with the libraries JavaCvPro you can use the OpenCV to implement an algorithm more sophisticated than the basic ones that I have proposed till now.

In this example I make the image edge detection. Then the contours detection is obtained and hence the contours are approximated by polynomial curve. In particular, the polynomial is a set of points that can be represented for example with the segments.
The code is organized as a function java but it is simple to adapt for different uses.

```// -- opencv linearization variables
int iterazione=0;
int cc=-1;
int [][] E;```
```// ********************************************************
// OpenCV - Processing with JavaCvPro
// Edge detection - contour detection - polynomial approximation
// ********************************************************
Pimage goFilterCV(PImage draft) {

E = new int[ow*oh][3]; // matrix for vertex list - weigth * height

opencv.allocate(ow, oh); //allocate space for image in opencv

//-- copy draft image to opencv IplImage with javacvpro library
opencv_core.IplImage opencvImgSrc=opencv.fromPImage(draft);  // copy draft --> IplImage
opencv_core.CvSize mySize=opencvImgSrc.cvSize();             // take the size of IplImage

opencv_core.IplImage opencvImgDest= opencv_core.cvCreateImage(mySize, opencv_core.IPL_DEPTH_8U, 3); // build an image IplImage , 3 channels

//--- bilateral filter effect
for (int i=0; i<10; i++) {
opencv_imgproc.bilateralFilter(opencvImgSrc, opencvImgDest, 3, 20.0, 50.0, 0 ); // applique un effet Flou gaussien
opencv_core.cvCopy(opencvImgDest, opencvImgSrc);
}
filter1 = createImage(ow,oh,RGB);
filter1=toPImage(opencvImgDest);

//-- define IplImage
opencv_core.IplImage iplImgGray;
opencv_core.IplImage iplImgGray1;
iplImgGray = opencv_core.cvCreateImage(mySize, opencv_core.IPL_DEPTH_8U, 1);   // build an image IplImage , 1 channels - only gray
iplImgGray1= opencv_core.cvCreateImage(mySize, opencv_core.IPL_DEPTH_8U, 1);   // build an image IplImage , 1 channels - only gray

//-- transform colors in gray levels
opencv_imgproc.cvCvtColor(opencvImgSrc, iplImgGray, opencv_imgproc.CV_RGB2GRAY);

//-- edge detection - canny algo
opencv_imgproc.cvCanny(iplImgGray, iplImgGray1, 100.0, 130.0, 3 );
//opencv_imgproc.cvDilate(iplImgGray1, iplImgGray1, null , 1); // in alternative you can use the dilate function to better find the edges

//-- contour detection
opencv_core.CvMemStorage mem = opencv_core.CvMemStorage.create();
opencv_core.CvSeq contour = new opencv_core.CvSeq(null);
int total=opencv_imgproc.cvFindContours(iplImgGray1, mem, contour, Loader.sizeof(opencv_core.CvContour.class), opencv_imgproc.CV_RETR_LIST, opencv_imgproc.CV_CHAIN_APPROX_NONE);
println("total point cvFindContours:"+total);

//-- polynomial approximation
while (contour != null && !contour.isNull ()) {
iterazione++; //number of interactions
opencv_core.CvSeq poly = null;
if (contour.elem_size() > 0) {
poly = opencv_imgproc.cvApproxPoly(contour, Loader.sizeof(opencv_core.CvContour.class), mem, opencv_imgproc.CV_POLY_APPROX_DP, 3, -1);

//transfer to matrix
int n = poly.total();
opencv_core.CvPoint points = new opencv_core.CvPoint(n);
opencv_core.cvCvtSeqToArray(poly, points.position(0), opencv_core.CV_WHOLE_SEQ);
for (int i = 0; i < n; i++) {
cc++;
opencv_core.CvPoint pt = points.position(i);
int x = pt.x();
int y = pt.y();
E[cc][0]=x;
E[cc][1]=y;
E[cc][2]=iterazione;
// println("pts:"+i+" x:"+x+" y:"+y);
}
}
contour = contour.h_next();
} //end polynomial approx

// -- print some values for debugging purposes
// for (int i = 0; i < 100; i++) //contatore to have all the points
// println(E[i][0]+" "+E[i][1]+" "+E[i][2]);

draft=toPImage(iplImgGray1); // return draft - PImage
return (draft);

}

// ********************************************************
// take an IplImage and give a PImage
// ********************************************************
PImage toPImage (opencv_core.IplImage iplImgIn) { // take an IplImage and give a PImage
//--- put the IplImage in a bufferedImage
BufferedImage bufImg=iplImgIn.getBufferedImage();

//---- build a PImage with the same size of IplImage---
PImage imgOut = createImage(iplImgIn.width(), iplImgIn.height(), RGB);

// put the pixel of IplImage to imgOut.pixels of PImage
bufImg.getRGB(0, 0, iplImgIn.width(), iplImgIn.height(), imgOut.pixels, 0, iplImgIn.width());

imgOut.updatePixels(); // PImage update

return(imgOut); // return the PImage
}```