2017-05-06 8 views
1

CIDetectorが最大の矩形を検出できませんでした。左画像は原画像であり、右画像は矩形検出画像である。完全な長方形を検出しない。どうすれば解決できますか?なぜCIDetectorが最大の長方形を検出できなかったのですか?

Original Image-Detected Image

- (CIDetector *)highAccuracyRectangleDetector 
{ 
    static CIDetector *detector = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken,^
    { 
     detector = [CIDetector detectorOfType:CIDetectorTypeRectangle 
context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}]; 
    }); 
    return detector; 
} 

- (CIRectangleFeature *)_biggestRectangleInRectangles:(NSArray 
*)rectangles 
{ 
    if (![rectangles count]) return nil; 

    float halfPerimiterValue = 0; 

    CIRectangleFeature *biggestRectangle = [rectangles firstObject]; 

    for (CIRectangleFeature *rect in rectangles) 
    { 
     CGPoint p1 = rect.topLeft; 
     CGPoint p2 = rect.topRight; 
     CGFloat width = hypotf(p2.x - p1.x, p2.y - p1.y); 

     CGPoint p3 = rect.topLeft; 
     CGPoint p4 = rect.bottomLeft; 
     CGFloat height = hypotf(p4.x - p3.x, p4.y - p3.y); 
     CGFloat currentHalfPerimiterValue = (height)+(width); 
     _RectHeight = height; 
     _RectWidth = width; 
     if (halfPerimiterValue < currentHalfPerimiterValue) 
     { 

      halfPerimiterValue = currentHalfPerimiterValue; 
      biggestRectangle = rect; 
      NSLog(@"height %@", @(height)); 
      NSLog(@"width %@", @(width)); 
     } 
    } 

    return biggestRectangle; 
} 

答えて

1

最後に、私はCIDetectorAspectRatioを追加することによってこの問題を解決:1.667 @、CIDetectorMaxFeatureCount:5

- (CIDetector *)highAccuracyRectangleDetector 
{ 
    static CIDetector *detector = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken,^
    { 
      detector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh, CIDetectorAspectRatio: @1.667, CIDetectorMaxFeatureCount: @5}]; 
    }); 
    return detector; 
} 
@