void canny(unsigned char *readImage, unsigned char **writeImage, float gauSigma, float edgeT, float backT, char *composedFilename) { float *radiansImg; short int *gsmoothImage, *deltaX, *deltaY, *magnitude; unsigned char *nms; // FILE *fp; (*writeImage) = (unsigned char *) calloc(HEIGHT*WIDTH, sizeof (unsigned char)); if (VERBOSE) printf("Gaussian smoothing ...\n"); gaussianSmooth(readImage, &gsmoothImage, gauSigma); if (VERBOSE) printf("Computing the X and Y first derivaties.\n"); derrXY(gsmoothImage, &deltaX, &deltaY); radianDir(deltaX, deltaY, &radiansImg, -1, -1); if (VERBOSE) printf("Computing the magnitude of the gradient.\n"); magXY(deltaX, deltaY, &magnitude); if (VERBOSE) printf("Doing the non-maximal suppression.\n"); nms = (unsigned char *) calloc(HEIGHT*WIDTH, sizeof (unsigned char)); nonMaxSup(magnitude, deltaX, deltaY, nms); if (VERBOSE) printf("Doing hysteresis thresholding.\n"); applyHysteresis(magnitude, nms, edgeT, backT, *writeImage); free(gsmoothImage); free(deltaX); free(deltaY); free(magnitude); free(nms); }
void Objectness::predictBBoxSI(CMat &img3u, ValStructVec<float, Vec4i> &valBoxes, vecI &sz, int NUM_WIN_PSZ, bool fast) { const int numSz = _svmSzIdxs.size(); const int imgW = img3u.cols, imgH = img3u.rows; valBoxes.reserve(10000); sz.clear(); sz.reserve(10000); for (int ir = numSz - 1; ir >= 0; ir--) { int r = _svmSzIdxs[ir]; int height = cvRound(pow(_base, r/_numT + _minT)), width = cvRound(pow(_base, r%_numT + _minT)); if (height > imgH * _base || width > imgW * _base) continue; height = min(height, imgH), width = min(width, imgW); Mat im3u, matchCost1f, mag1u; resize(img3u, im3u, Size(cvRound(_W*imgW*1.0/width), cvRound(_W*imgH*1.0/height))); gradientMag(im3u, mag1u); matchCost1f = _tigF.matchTemplate(mag1u); ValStructVec<float, Point> matchCost; nonMaxSup(matchCost1f, matchCost, _NSS, NUM_WIN_PSZ, fast); // Find true locations and match values double ratioX = width/_W, ratioY = height/_W; int iMax = min(matchCost.size(), NUM_WIN_PSZ); for (int i = 0; i < iMax; i++) { float mVal = matchCost(i); Point pnt = matchCost[i]; Vec4i box(cvRound(pnt.x * ratioX), cvRound(pnt.y*ratioY)); box[2] = cvRound(min(box[0] + width, imgW)); box[3] = cvRound(min(box[1] + height, imgH)); box[0] ++; box[1] ++; valBoxes.pushBack(mVal, box); sz.push_back(ir); } } //exit(0); }
ImageRAII canny( IplImage * image, std::pair< int, int > thresh, double sigma ) { const char * WINDOW_NAME = "Basic Canny Edge Detector"; ImageRAII grayscale( cvCreateImage( cvGetSize( image ), image->depth, 1 ) ); ImageRAII destination( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); ImageRAII gaussian( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); ImageRAII gradient_x( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); ImageRAII gradient_y( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); ImageRAII gradient( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); ImageRAII orientation( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) ); // convert image to grayscale cvCvtColor( image, grayscale.image, CV_BGR2GRAY ); // gaussian smoothing cvSmooth( grayscale.image, gaussian.image, CV_GAUSSIAN, GAUSSIAN_X, GAUSSIAN_Y, sigma ); // find edge strength cvSobel( gaussian.image, gradient_x.image, 1, 0, 3 ); cvSobel( gaussian.image, gradient_y.image, 0, 1, 3 ); // find edge orientation CvSize image_size = cvGetSize( gaussian.image ); for( int i = 0; i < image_size.width; i++ ) { for( int j = 0; j < image_size.height; j++ ) { double x = cvGet2D( gradient_x.image, j, i ).val[0]; double y = cvGet2D( gradient_y.image, j, i ).val[0]; float angle; if( x == 0 ) { if( y == 0 ) angle = 0; else angle = 90; } else angle = cvFastArctan( y, x ); CvScalar g; CvScalar a; g.val[0] = cvSqrt( pow( x, 2 ) + pow( y, 2 ) ); a.val[0] = find_angle( angle ); cvSet2D( destination.image, j, i, g ); cvSet2D( orientation.image, j, i, a ); } } ImageRAII suppressed_image = nonMaxSup( destination.image, orientation.image ); ImageRAII hysteresis_image = hysteresis( suppressed_image.image, orientation.image, thresh ); cvNamedWindow( WINDOW_NAME ); cvShowImage( WINDOW_NAME, destination.image ); cvMoveWindow( WINDOW_NAME, image_size.width, 0 ); return hysteresis_image; }