求:openCV 中 cvMinAreaRect2 函数的 具体实现代码 哪怕是伪代码也好
1个回答
展开全部
参考资料:http://fossies.org/dox/OpenCV-2.4.4a/rotcalipers_8cpp_source.html
以下截取自347行开始
347 CV_IMPL CvBox2D
348 cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
349 {
350 cv::Ptr<CvMemStorage> temp_storage;
351 CvBox2D box;
352 cv::AutoBuffer<CvPoint2D32f> _points;
353 CvPoint2D32f* points;
354
355 memset(&box, 0, sizeof(box));
356
357 int i, n;
358 CvSeqReader reader;
359 CvContour contour_header;
360 CvSeqBlock block;
361 CvSeq* ptseq = (CvSeq*)array;
362 CvPoint2D32f out[3];
363
364 if( CV_IS_SEQ(ptseq) )
365 {
366 if( !CV_IS_SEQ_POINT_SET(ptseq) &&
367 (CV_SEQ_KIND(ptseq) != CV_SEQ_KIND_CURVE ||
368 CV_SEQ_ELTYPE(ptseq) != CV_SEQ_ELTYPE_PPOINT ))
369 CV_Error( CV_StsUnsupportedFormat,
370 "Input sequence must consist of 2d points or pointers to 2d points" );
371 if( !storage )
372 storage = ptseq->storage;
373 }
374 else
375 {
376 ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
377 }
378
379 if( storage )
380 {
381 temp_storage = cvCreateChildMemStorage( storage );
382 }
383 else
384 {
385 temp_storage = cvCreateMemStorage(1 << 10);
386 }
387
388 ptseq = cvConvexHull2( ptseq, temp_storage, CV_CLOCKWISE, 1 );
389 n = ptseq->total;
390
391 _points.allocate(n);
392 points = _points;
393 cvStartReadSeq( ptseq, &reader );
394
395 if( CV_SEQ_ELTYPE( ptseq ) == CV_32SC2 )
396 {
397 for( i = 0; i < n; i++ )
398 {
399 CvPoint pt;
400 CV_READ_SEQ_ELEM( pt, reader );
401 points[i].x = (float)pt.x;
402 points[i].y = (float)pt.y;
403 }
404 }
405 else
406 {
407 for( i = 0; i < n; i++ )
408 {
409 CV_READ_SEQ_ELEM( points[i], reader );
410 }
411 }
412
413 if( n > 2 )
414 {
415 icvRotatingCalipers( points, n, CV_CALIPERS_MINAREARECT, (float*)out );
416 box.center.x = out[0].x + (out[1].x + out[2].x)*0.5f;
417 box.center.y = out[0].y + (out[1].y + out[2].y)*0.5f;
418 box.size.width = (float)sqrt((double)out[1].x*out[1].x + (double)out[1].y*out[1].y);
419 box.size.height = (float)sqrt((double)out[2].x*out[2].x + (double)out[2].y*out[2].y);
420 box.angle = (float)atan2( (double)out[1].y, (double)out[1].x );
421 }
422 else if( n == 2 )
423 {
424 box.center.x = (points[0].x + points[1].x)*0.5f;
425 box.center.y = (points[0].y + points[1].y)*0.5f;
426 double dx = points[1].x - points[0].x;
427 double dy = points[1].y - points[0].y;
428 box.size.width = (float)sqrt(dx*dx + dy*dy);
429 box.size.height = 0;
430 box.angle = (float)atan2( dy, dx );
431 }
432 else
433 {
434 if( n == 1 )
435 box.center = points[0];
436 }
437
438 box.angle = (float)(box.angle*180/CV_PI);
439 return box;
440 }
441
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询