1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include "main.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
cvDestroyAllWindows();
// Chargement d'une nouvelle image
Mat image = imread("my_image.png", 1);
if( image.empty() )
{
cout << "Couldn't load image " << endl;
return -1;
}
IplImage *img = &image.operator IplImage();
IplImage* grayImg = NULL; // tmp image object
IplImage* edgeImg = NULL; // output image object
int lowerThreshold = 50; // lower canny edge threshold initial setting
int upperThreshold = 200; // upper canny edge threshold initial setting
int windowSize = 3; // canny edge window size
char* windowName = "Canny Edge Detection"; // window name
bool keepProcessing = true; // loop control flag
char key; // user input
// create window object
cvNamedWindow(windowName, 0 );
// add adjustable trackbars for ech of the canny edge parameters
cvCreateTrackbar("Lower", windowName, &lowerThreshold, 255, NULL);
cvCreateTrackbar("Upper", windowName, &upperThreshold, 255, NULL);
cvCreateTrackbar("Window", windowName, &windowSize, 7, NULL);
// create other images (and make sure they have the same origin)
grayImg = cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
grayImg->origin = img->origin;
edgeImg = cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
edgeImg->origin = img->origin;
// grab the next frame to maintain sync
// start main grab/process/display loop
while(keepProcessing)
{
// check that the window size is always odd and > 1
if ((fmod((double) windowSize, 2) == 0) || (windowSize <= 2))
{
windowSize++;
}
// convert colour of captured image to greyscale and perform
// edge detection using canny edge detection algorithm
cvCvtColor(img, grayImg, CV_BGR2GRAY);
cvCanny(grayImg, edgeImg, lowerThreshold, upperThreshold, max(3, windowSize));
Mat edgeMat(edgeImg);
bitwise_not (edgeMat, edgeMat);
// display image in window
imshow(windowName, edgeMat);
// start event processing loop
// here we use a 40 msec delay for ~25 fps (100/25 = 40)
key = cvWaitKey(40);
if (key == 'x')
{
// if user presses "x" then exit
printf("Keyboard exit requested : exiting now - bye!\n");
keepProcessing = false;
}
}
// destroy window object
// (triggered by event loop *only* window is closed)
cvDestroyWindow( windowName );
// release capture device
// destroy image objects
cvReleaseImage( &grayImg );
cvReleaseImage( &edgeImg );
// Note that the image captured by the device is allocated/released
// by the capture function.
// There is no need to release it explicitly.
// all OK : main returns 0
return 0;
} |
Partager