我一直在 Objective C 中使用一些代码,对在 AVCaptureStillImageOutput
中捕获的 CIImage
执行 CIDetector
。我的目标是将其转换为 swift 3。我已将其全部转换为 swift 3,但我在我的 CIRectangleFeature
方法中收到了 this Could not cast 错误。所以我来这里寻求帮助,因为我已经为此工作好几天了,但无法做好。我确信这是我忽略的简单事情。
这是 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;
}
这个函数被另一个函数调用,这里是 Objective C 中的
- (CIRectangleFeature *)biggestRectangleInRectangles:(NSArray *)rectangles
{
CIRectangleFeature *rectangleFeature = [self _biggestRectangleInRectangles:rectangles];
现在,如果 if
像这样返回 true
if (self.isBorderDetectionEnabled)
{
if (_borderDetectFrame)
{
_borderDetectLastRectangleFeature = [self biggestRectangleInRectangles:[[self highAccuracyRectangleDetector] featuresInImage:image]];
_borderDetectFrame = NO;
}
它也在另一种方法中被调用,该方法捕获图像并以基本相同的方式保存它们
现在我把它翻译成这样的 swift 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 中调用它基本上与 Objective 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
您在数组中使用双重包装:
let rectangleFeature: CIRectangleFeature? = self.biggestRectangle(rectangles: [self.highAccuracyRectangleDetector().features(in: enhancedImage!)])
关于ios - 可以将类型 '__NSArray0' 的值转换为 'CIRectangleFeature',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568334/