2017-07-15 2 views

Antwort

0

Wenn Sie nach einer Objekterkennung suchen, z. B. nach einer Visitenkarte, sehen Sie sich das integrierte "Vision" -Framework in iOS11 an. Ich kann mich nicht erinnern, ob ihre WWDC-Sitzung diese Verwendung gezeigt hat, aber ich werde Sie auf diese github link hinweisen, die eine Rechteckerkennung (wie eine Visitenkarte) verwendet, die mit CoreML gemischt ist (was wahrscheinlich nicht von Nutzen ist). Ich warne dich, dass dies nicht mein Code ist, also kann ich nicht sagen, wie fehlerfrei es ist, aber es wird dich hoffentlich in die richtige Richtung weisen. Wenn das nicht gelingt, ist die Apple Documention immer ein guter Anfang.

2

Ja! Mit CIDetector von CoreImage können Sie Rechteck erkennen. Hier ist der Code (Swift 3) höchstwahrscheinlich Sie suchen!

func performRectangleDetection(image: CIImage) -> CIImage? { 
     var resultImage: CIImage? 
     resultImage = image 
     let detector = CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorAspectRatio: 1.6, CIDetectorMaxFeatureCount: 10])! 

     // Get the detections 
     var halfPerimiterValue = 0.0 as Float; 
     let features = detector.features(in: image) 
     print("feature \(features.count)") 
     for feature in features as! [CIRectangleFeature] { 

      let p1 = feature.topLeft 
      let p2 = feature.topRight 
      let width = hypotf(Float(p1.x - p2.x), Float(p1.y - p2.y)); 
      //NSLog(@"xaxis %@", @(p1.x)); 
      //NSLog(@"yaxis %@", @(p1.y)); 
      let p3 = feature.topLeft 
      let p4 = feature.bottomLeft 
      let height = hypotf(Float(p3.x - p4.x), Float(p3.y - p4.y)); 
      let currentHalfPerimiterValue = height+width; 
      if (halfPerimiterValue < currentHalfPerimiterValue) 
      { 
       halfPerimiterValue = currentHalfPerimiterValue 
       resultImage = cropBusinessCardForPoints(image: image, topLeft: feature.topLeft, topRight: feature.topRight, 
                 bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight) 
       print("perimmeter \(halfPerimiterValue)") 
      } 

     } 

     return resultImage 
    } 

    func cropBusinessCardForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage { 

     var businessCard: CIImage 
     businessCard = image.applyingFilter(
      "CIPerspectiveTransformWithExtent", 
      withInputParameters: [ 
       "inputExtent": CIVector(cgRect: image.extent), 
       "inputTopLeft": CIVector(cgPoint: topLeft), 
       "inputTopRight": CIVector(cgPoint: topRight), 
       "inputBottomLeft": CIVector(cgPoint: bottomLeft), 
       "inputBottomRight": CIVector(cgPoint: bottomRight)]) 
     businessCard = image.cropping(to: businessCard.extent) 

     return businessCard 
    } 

Rufen Sie die Funktion performRectangleDetection, hier cropBusinessCardForPoints ist Assistentin.

UIImage/CIImage Conversion .

Best of Luck!

Verwandte Themen