Re: Need help on corners, edges and contours detection

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Need help on corners, edges and contours detection

umeshnarayanan
--- In [hidden email], "clement.tellier" <clement.tellier@...>
wrote:

>
> Hi,
>
> For a professionnal project, i have to :
> - detect contours of unknown objects.
> - detect outstanding points.
>
> I'm a computer images engineer, having studied image analysis for 2
> years and i'm very happy to discover that OpenCV implements a lot of
> stuff around this topic. I have spend around 2 weeks to get familiar
> with OpenCV, reading docs and manuals and performing some tests, but
> unfortunetly, due to my poor english skill or memory leak, i'm still
> lost and enable to correctly use OpenCV. It seems that some tips have
> to be known, as using some functions before others and so on.
>
> Does anyone know where i can find tutorials/exemples of use on
> corners, edges and contours detection ? And if someone would be ok to
> spend time with me, perhaps we can speak together on private channel.
>
> In a second time, since these detections will be performed on stereo
> images, i will have to pair the detected points. OpenCV seems to
> offer good stuff to do that, like motion detection...
>
> For a starting point, i would like to have well defined contours of
> the objects.
>
> I really need help !
>
> Thanks in advance.
>



hai

this is sample programm for detecting and drawing the counter of an
image ,first converting to gray scale image , the detecting and
drawing the counter




#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main()
{
  IplImage* newImg = NULL;
  IplImage* grayImg = NULL;
  IplImage* contourImg = NULL;
 
  //parameters for the contour detection
 
  CvMemStorage * storage = cvCreateMemStorage(0);
  CvSeq * contour = 0;
 
  int mode = CV_RETR_EXTERNAL;
 
  mode = CV_RETR_CCOMP; //detect both outside and inside contour
 
cvNamedWindow("src", 1);
cvNamedWindow("gray", 1);
cvNamedWindow("contour",1);
 
 
  //load original image
  newImg = cvLoadImage("apple.jpg",1);
 
  //create a single channel 1 byte image (i.e. gray-level image)
  grayImg = cvCreateImage( cvSize(newImg->width, newImg->height),
IPL_DEPTH_8U, 1 );
 
  //convert original color image (3 channel rgb color image) to
gray-level image
  cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
  cvShowImage( "src", newImg );
 
  //make a copy of the original image to draw the detected contour
  contourImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 3);
  contourImg = cvCloneImage( newImg );
 
  //find the contour
  cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
 
  //draw the contour
  cvDrawContours(contourImg, contour, CV_RGB(0, 255, 0), CV_RGB(255,
0, 0), 2, 2, 8);
  cvShowImage( "gray", grayImg );
  cvMoveWindow("counter",250,300);
  cvShowImage( "contour", contourImg );
   
  cvWaitKey(0);
 
  cvDestroyWindow( "src" );
  cvDestroyWindow( "contour" );
  cvReleaseImage( &newImg );  
  cvReleaseImage( &grayImg );  
  cvReleaseImage( &contourImg );
  cvReleaseMemStorage(&storage);
 
  return 0;
}




this one for edge detection



//umeshnarayanan 12/01/2008 programm for edge detection
//this programm will take a image and convert it to gray scale image and
//then its edge is detected by Canny algorithm for edge detection

#include "cv.h" //main OpenCV header
#include "highgui.h" //GUI header

int main()
 
 {
       
   IplImage* newImg; // original image
   IplImage* grayImg; // gray image
   IplImage* cannyImg; // gray image for the canny edge detection
   
   
   newImg = cvLoadImage("micky.jpg",1); //load original image
   
    //create a single channel 1 byte image (i.e. gray-level image)
    grayImg = cvCreateImage( cvSize(newImg->width, newImg->height),
IPL_DEPTH_8U, 1 );
   
    //convert original color image (3 channel rgb color image) to
gray-level image
    cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
    cannyImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 1);


 
   
    // canny edge detection
    cvCanny(grayImg, cannyImg, 50, 150, 3);
   
    cvNamedWindow("src", 1);
    cvNamedWindow("gray", 1);
    cvMoveWindow("src", 50, 150);
    cvNamedWindow("canny",1);
    cvMoveWindow("canny", 650, 150);
    cvShowImage( "src", newImg );
    cvShowImage( "canny", cannyImg );
    cvShowImage( "gray", grayImg );
   
    cvWaitKey(0);
    cvDestroyWindow( "src" );
    cvDestroyWindow( "canny" );
    cvDestroyWindow( "grayImg" );
    cvReleaseImage( &newImg );
    cvReleaseImage( &grayImg );
    cvReleaseImage( &cannyImg );
    return 0;
   
 }












Reply | Threaded
Open this post in threaded view
|

Re: Need help on corners, edges and contours detection

umeshnarayanan
--- In [hidden email], "clement.tellier" <clement.tellier@...>
wrote:

>
> Hi,
>
> For a professionnal project, i have to :
> - detect contours of unknown objects.
> - detect outstanding points.
>
> I'm a computer images engineer, having studied image analysis for 2
> years and i'm very happy to discover that OpenCV implements a lot of
> stuff around this topic. I have spend around 2 weeks to get familiar
> with OpenCV, reading docs and manuals and performing some tests, but
> unfortunetly, due to my poor english skill or memory leak, i'm still
> lost and enable to correctly use OpenCV. It seems that some tips have
> to be known, as using some functions before others and so on.
>
> Does anyone know where i can find tutorials/exemples of use on
> corners, edges and contours detection ? And if someone would be ok to
> spend time with me, perhaps we can speak together on private channel.
>
> In a second time, since these detections will be performed on stereo
> images, i will have to pair the detected points. OpenCV seems to
> offer good stuff to do that, like motion detection...
>
> For a starting point, i would like to have well defined contours of
> the objects.
>
> I really need help !
>
> Thanks in advance.
>




its another interesting programm to find the edge of the objects in
the video using canny algorithm



#include "cv.h" // includes OpenCV definitions
#include "highgui.h" // includes highGUI definitions
#include <stdio.h>// includes C standard input/output definitions
#include <stdlib.h>
#include <iostream>


   
int main()   //(int argc, char **argv)
   
   {
      CvCapture *capture = 0;
      IplImage *frame = 0;
      IplImage *sourse = 0;
      IplImage *grayImg = 0; // gray image
      IplImage *cannyImg = 0; // gray image for the canny edge detection
      IplImage *cannyImgclone = 0;
      int key = 0;
     
      CvFont font; //initialize the font
      cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA);
      CvPoint pt1 = cvPoint(20,190);
      CvPoint pt2 = cvPoint(90,220);
      CvPoint pt3 = cvPoint(40,190);
      CvPoint pt4 = cvPoint(50,220);
      capture = cvCaptureFromFile("one.avi"); //capture video from file
     
     IplImage* contourImg = NULL;
 
  //parameters for the contour detection
 
  CvMemStorage * storage = cvCreateMemStorage(0);
  CvSeq * contour = 0;
 
  int mode = CV_RETR_EXTERNAL;
 
  mode = CV_RETR_CCOMP; //detect both outside and inside contour
     
     
     
     
     
      /*==================== writing the video=========================*/
CvVideoWriter *writer;
//CvVideoWriter *cannyout;
int isColor = 1;
int fps     = 20;  // or 30
int frameW  = 500; // 744 for firewire cameras
int frameH  = 500; // 480 for firewire cameras

writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);

//cannyout=cvCreateVideoWriter("canny.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);

 
  /*==================== writing the video=========================*/  
   
           
   
      /* always check */
      if (!capture)
        {
      fprintf (stderr, "Cannot open Avi file!\n");
      return(1);
    }
 
      cvNamedWindow("result", CV_WINDOW_AUTOSIZE); // create a window
for the result
      cvMoveWindow("result", 350, 0);
      cvNamedWindow("src", CV_WINDOW_AUTOSIZE); // create a window for
sourse
      cvNamedWindow("canny",1);
      cvMoveWindow("canny", 750, 0);
      cvNamedWindow("counter",1);
       cvMoveWindow("counter", 750, 300);      
             
    for (;;)
   
    //for( int fr = 1;frame; frame = cvQueryFrame(capture), fr++ )
    {
   
        frame = cvQueryFrame(capture);// to retrive the frames
             sourse = cvQueryFrame(capture);
       
       
       if (!cvGrabFrame(capture))
      {
      break;
                }
               
    //create a single channel 1 byte image (i.e. gray-level image)
    grayImg = cvCreateImage( cvSize(sourse->width, sourse->height),
IPL_DEPTH_8U, 1 );
   
    //convert original color image (3 channel rgb color image) to
gray-level image
    cvCvtColor( sourse, grayImg, CV_BGR2GRAY );
    cannyImg = cvCreateImage(cvGetSize(sourse), IPL_DEPTH_8U, 1);
   
    // canny edge detection
    cvCanny(grayImg, cannyImg, 50, 150, 3);
   
     
      cvShowImage("src", sourse);
      char string[] = "Captured frame";
      char* t(&string[0]);
     
      cvPutText(frame,t, pt1, &font,CV_RGB(255, 255, 255));
      cvPutText(frame,"frame # ", pt2, &font,CV_RGB(255, 255, 255));
      cvShowImage("result", frame); /* display current frame */
     
       
      cvPutText(cannyImg,"Canny edge ", pt3, &font,CV_RGB(255, 255, 255));
      //cvPutText(cannyImg," detection ", pt4, &font,CV_RGB(255, 255,
255));
      cvShowImage( "canny", cannyImg );
      cannyImgclone = cvCloneImage(cannyImg);
// ==========================

//convert original color image (3 channel rgb color image) to
gray-level image
  cvCvtColor( sourse, grayImg, CV_BGR2GRAY );
  cvShowImage( "counter", sourse );
 //make a copy of the original image to draw the detected contour
  contourImg = cvCreateImage(cvGetSize(sourse), IPL_DEPTH_8U, 3);
  contourImg = cvCloneImage( sourse );
 
  //find the contour
  cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
 
  //draw the contour
  cvDrawContours(contourImg, contour, CV_RGB(0, 255, 0), CV_RGB(255,
0, 0), 2, 2, 8);
 

  cvShowImage( "counter", contourImg );
 
 
 
 
 
 
 
// ===============================    
 
       key=cvWaitKey(150);
     // key=cvWaitKey(30);
     
     //for saving the frames
         char can[] =  "cannyedge.jpg";
             char* c( &can[0] );
         
             cvSaveImage(c ,cannyImgclone);
             
             char result[] =  "resultant.jpg";
             char* r( &result[0] );
         
             cvSaveImage(r ,frame);
             
             
             cvWriteFrame(writer,frame);
           //cvWriteToAVI(cannyout,cannyImgclone);
             
             
/* always check */
             
 if (!frame)
     {
      break;
      }
     
/* exit if user press a key */    
 
if ((key = cvWaitKey(1)) > 0)
      {
      goto done;
      }
     
       } // end of for loop
     
      done:
   
      cvWaitKey(0);
      cvDestroyWindow("result");
      cvDestroyWindow("src");
     
       cvDestroyWindow( "canny" );
   
   
      cvReleaseImage( &grayImg );
      cvReleaseImage( &cannyImg );
      cvReleaseImage( &frame );
      cvReleaseImage( &sourse );
    cvReleaseImage( &contourImg );
     
      cvReleaseCapture(&capture);
      //cvReleaseVideoWriter(&writer);
     // cvReleaseVideoWriter(&cannyout);
      return(0);
 
      }




the below program will detect the back ground and forground fro m a
video (avi) and show in different window



#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
#include <stdio.h>

//this is a sample for foreground detection functions
int main(int argc, char** argv)
{
IplImage* tmp_frame = NULL;
IplImage* sourse = NULL;
CvCapture* capt = NULL;

      CvFont font; //initialize the font
      cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA);
      CvPoint pt1 = cvPoint(20,190);
     




//if( argc < 2 )
//{
// printf("please specify video file name \n");
// exit(0);
// }

//cap = cvCaptureFromFile(argv[1]);


capt = cvCaptureFromFile("yellow.avi"); // capture frme form file
tmp_frame = cvQueryFrame(capt);
sourse = cvQueryFrame(capt);

if(!tmp_frame)
{
printf("bad video \n");
exit(0);
}
cvNamedWindow("Sourse", 1);
cvNamedWindow("BackGround", 1);
cvMoveWindow("BackGround",350, 0);
cvNamedWindow("ForGround", 1);
cvMoveWindow("ForGround", 750, 0);


// this portion is bit complcated ,need more understanding


//create BG model
CvBGStatModel* bg_model;

bg_model =cvCreateGaussianBGModel( tmp_frame );

for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(capt), fr++ )
{

double t = (double)cvGetTickCount();

cvUpdateBGStatModel( tmp_frame, bg_model );

t = (double)cvGetTickCount() - t;


printf( "%.1f\n", t/(cvGetTickFrequency()*1000.) );




cvPutText(sourse,"sourse Video", pt1, &font,CV_RGB(255, 255, 255));
cvShowImage("Sourse",sourse );
cvPutText(bg_model->background,"Background", pt1, &font,CV_RGB(255,
255, 255));
cvShowImage("BackGround", bg_model->background);

cvPutText(bg_model->foreground,"ForeGround", pt1, &font,CV_RGB(255,
255, 255));  

cvShowImage("ForGround", bg_model->foreground);

//int k = cvWaitKey(20);

cvWaitKey(20);
//if( k == 27 ) break;
//printf("frame# %d \r", fr);
}


cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&capt);

return 0;
}