• Open Source Computer Vision Library

Canny边缘检测2

Wikipedia,自由的百科全书

运行结果:

Image:Example-edge.png

代码:

#ifdef _CH_
#pragma package <opencv>
#endif
 
#ifndef _EiC
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#endif
 
char wndname[] = "Edge";
char tbarname[] = "Threshold";
 
 
IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;
 
// define a trackbar callback
void on_trackbar(int h)
{
    cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
    cvNot( gray, edge );
 
    // Run the edge detector on grayscale
    cvCanny(gray, edge, (float)h, (float)h*3, 3);
 
    cvZero( cedge );
    // copy edge points
    cvCopy( image, cedge, edge );
 
    cvShowImage(wndname, cedge);
}
 
int main( int argc, char** argv )
{
    char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
    int edge_thresh = 1;   
    if( (image = cvLoadImage( filename, 1)) == 0 )
        return -1;
 
    // Create the output image
    cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);
 
    // Convert to grayscale
    gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
    edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
    cvCvtColor(image, gray, CV_BGR2GRAY);
 
    // Create a window
    cvNamedWindow(wndname, 1);
 
    // create a toolbar 
    cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);
 
    // Show the image
    on_trackbar(0);
 
    // Wait for a key stroke; the same function arranges events processing
    cvWaitKey(0);
    cvReleaseImage(&image);
    cvReleaseImage(&gray);
    cvReleaseImage(&edge);
    cvDestroyWindow(wndname);
 
    return 0;
}
 
#ifdef _EiC
main(1,"edge.c");
#endif

Python版本

#! /usr/bin/env python
 
print "OpenCV Python version of edge"
 
import sys
 
# import the necessary things for OpenCV
from opencv import cv
from opencv import highgui
 
# some definitions
win_name = "Edge"
trackbar_name = "Threshold"
 
# the callback on the trackbar
def on_trackbar (position):
 
    cv.cvSmooth (gray, edge, cv.CV_BLUR, 3, 3, 0)
    cv.cvNot (gray, edge)
 
    # run the edge dector on gray scale
    cv.cvCanny (gray, edge, position, position * 3, 3)
 
    # reset
    cv.cvSetZero (col_edge)
 
    # copy edge points
    cv.cvCopy (image, col_edge, edge)
 
    # show the image
    highgui.cvShowImage (win_name, col_edge)
 
if __name__ == '__main__':
    filename = "../c/fruits.jpg"
 
    if len(sys.argv)>1:
        filename = sys.argv[1]
 
    # load the image gived on the command line
    image = highgui.cvLoadImage (filename)
 
    if not image:
        print "Error loading image '%s'" % filename
        sys.exit(-1)
 
    # create the output image
    col_edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 3)
 
    # convert to grayscale
    gray = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
    edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
    cv.cvCvtColor (image, gray, cv.CV_BGR2GRAY)
 
    # create the window
    highgui.cvNamedWindow (win_name, highgui.CV_WINDOW_AUTOSIZE)
 
    # create the trackbar
    highgui.cvCreateTrackbar (trackbar_name, win_name, 1, 100, on_trackbar)
 
    # show the image
    on_trackbar (0)
 
    # wait a key pressed to end
    highgui.cvWaitKey (0)
Views
Personal tools