我一直在目标C中使用一些代码,这些代码对CIDetector中捕获的CIImage执行AVCaptureStillImageOutput。我的目标是将其转换为swift3。将其全部转换为swift 3,但是我无法在CIRectangleFeature方法中强制转换。所以我在这里一直在寻求帮助,因为我已经为此工作了好几天,但无法正确解决。我敢肯定,这是我所忽略的简单事情。

这是Objective C中的代码

- (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(p1.x - p2.x, p1.y - p2.y);

    CGPoint p3 = rect.topLeft;
    CGPoint p4 = rect.bottomLeft;
    CGFloat height = hypotf(p3.x - p4.x, p3.y - p4.y);

    CGFloat currentHalfPerimiterValue = height + width;

    if (halfPerimiterValue < currentHalfPerimiterValue)
    {
        halfPerimiterValue = currentHalfPerimiterValue;
        biggestRectangle = rect;
    }
}

return biggestRectangle;


}

该函数是从另一个函数中调用的,它在目标C中

- (CIRectangleFeature *)biggestRectangleInRectangles:(NSArray *)rectangles
{
    CIRectangleFeature *rectangleFeature = [self _biggestRectangleInRectangles:rectangles];


现在,如果-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection返回if,则从true内部调用此函数,如下所示

if (self.isBorderDetectionEnabled)
{
    if (_borderDetectFrame)
    {
        _borderDetectLastRectangleFeature = [self biggestRectangleInRectangles:[[self highAccuracyRectangleDetector] featuresInImage:image]];
        _borderDetectFrame = NO;
    }


在另一种捕获图像并以基本相同的方式保存图像的方法中也调用它

现在我将它翻译成这样的迅捷3

func bigRectangle(rectangles: [Any]) -> CIRectangleFeature {


    var halfPerimiterValue: Float = 0
    var biggestRectangles: CIRectangleFeature? = rectangles.first as! CIRectangleFeature? // This is the line causing the casting error
    for rect: CIRectangleFeature in rectangles as! [CIRectangleFeature]{
        let p1: CGPoint = rect.topLeft
        let p2: CGPoint = rect.topRight
        let width: CGFloat = CGFloat(hypotf(Float(p1.x) - Float(p2.x), Float(p1.y) - Float(p2.y)))
        let p3: CGPoint = rect.topLeft
        let p4: CGPoint = rect.bottomLeft
        let height: CGFloat = CGFloat(hypotf(Float(p3.x) - Float(p4.x), Float(p3.y) - Float(p4.y)))
        let currentHalfPerimiterValue: CGFloat = height + width
        if halfPerimiterValue < Float(currentHalfPerimiterValue) {
            halfPerimiterValue = Float(currentHalfPerimiterValue)
            biggestRectangles = rect
        }
    }
    return biggestRectangles!
}


我在Swift 3中称呼它基本上与目标C一样,就像这样

func biggestRectangle(rectangles: [Any]) -> CIRectangleFeature {
    let rectangleFeature: CIRectangleFeature? = self.bigRectangle(rectangles: rectangles)


像在Objective C中一样调用该函数。

if self.isEnableBorderDetection == true{
        if self.borderDetectFrames == true {
            self.borderDetectLastRectangleFeature = self.biggestRectangle(rectangles: [self.highAccuracyRectangleDetector().features(in: image)])
            self.borderDetectFrames = false
        }


从功能func captureOutput(_ captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBuffer?, from connection: AVCaptureConnection) {}

现在,我尝试在两个函数中将数组类型从[Any]更改为[CIRectangleFeature],但是我遇到了CIFeature是不相关类型的问题。

希望有人可以看看这个并指出正确的方向。在此先感谢您的帮助。

最佳答案

UPD
您在数组中使用双重包装:

08-26 03:29