การทำ edge detection

Posted by Undermine on 05:26

ตัวอย่างนี้แสดงถึงวิธีการใช้คำสั่งของ OpenCV เพื่อทำ edge detection โดยใช้วิธีของ Canny

#include 
using namespace std;

#include 
#include 
#include 

#define CAMERA_ID 0
#define ORG_IMAGE_DISPLAY  "original image"
#define PROCESSED_IMAGE_DISPLAY "processed image"

#define STRONG_EDGE_TH 100
#define WEAK_EDGE_TH 10

int main( int argc, char **argv )
{
    // create camera capture object.
    CvCapture *pCapture = NULL;
    pCapture = cvCreateCameraCapture( CAMERA_ID );
    if ( pCapture == NULL )
        {
            cout << "ERROR: Failed to open camera" << endl;
            return EXIT_FAILURE;
        }

    // create windows for displaying the captured image
    // and the captured image.
    cvNamedWindow( ORG_IMAGE_DISPLAY );
    cvNamedWindow( PROCESSED_IMAGE_DISPLAY );

    // capture, process, and display
    // until user hit any key of the keyboard
    IplImage *pOrgImage = NULL;
    IplImage *pProcessedImage = NULL;
    do
    {
        // release old image
        // before creating a new one
        if ( pProcessedImage != NULL )
            {
             cvReleaseImage( &pProcessedImage );
             pProcessedImage = NULL;
            }

        // capture new image
        pOrgImage = cvQueryFrame( pCapture );

        // create image which has the same width, height, depth, and number of channels
        // as the original image for put the result of processing.
        pProcessedImage = cvCreateImage( cvSize( pOrgImage->width, pOrgImage->height ),
                    pOrgImage->depth, 1 );

        // do canny edge dectection
        cvCvtColor( pOrgImage, pProcessedImage, CV_BGR2GRAY );
        cvCanny( pProcessedImage, pProcessedImage, STRONG_EDGE_TH, WEAK_EDGE_TH );

        // display images
        cvShowImage( ORG_IMAGE_DISPLAY, pOrgImage );
        cvShowImage( PROCESSED_IMAGE_DISPLAY, pProcessedImage );
    } while ( cvWaitKey( 1 ) == -1 );

    // release old image
    if ( pProcessedImage != NULL )
        {
            cvReleaseImage( &pProcessedImage );
            pProcessedImage = NULL;
        }

    // before exiting the program,
    // destroy the display windows and
    // release the cameara capture object.
    cvDestroyWindow( ORG_IMAGE_DISPLAY );
    cvDestroyWindow( PROCESSED_IMAGE_DISPLAY );
    cvReleaseCapture( &pCapture );
    return 0;
}

1 comments:

Comment by Saad on 26 กุมภาพันธ์ 2556 เวลา 00:38

Thanks, a very good and WORKING CODE

I added following only in CodeBlock , I have already configured CodeBlock to work with OpenCV

#include
#include
#include

#include
using namespace std;

What I found were 2 windows : 1 . Real Time Web Cam image 2. It only showed edges of the image captured by Web Cam.

 

แสดงความคิดเห็น