gpt4 book ai didi

ios - CIDetector featuresInImage 返回 0

转载 作者:行者123 更新时间:2023-11-29 00:02:07 25 4
gpt4 key购买 nike

我正在尝试使用核心检测器获取图像上的文本区域。

- (NSArray *)detectWithImage:(UIImage *)img
{
// prepare CIImage
CIImage *image = [CIImage imageWithCGImage:img.CGImage];

// flip vertically
CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];
[filter setValue:image forKey:kCIInputImageKey];
CGAffineTransform t = CGAffineTransformMakeTranslation(0, CGRectGetHeight(image.extent));
t = CGAffineTransformScale(t, 1.0, -1.0);
[filter setValue:[NSValue valueWithCGAffineTransform:t] forKey:kCIInputTransformKey];
image = filter.outputImage;


// prepare CIDetector
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeText
context:nil
options:@{
CIDetectorAccuracy: CIDetectorAccuracyHigh}];
// retrive array of CITextFeature
NSArray *features = [detector featuresInImage:image
options:@{CIDetectorReturnSubFeatures: @YES}];

return features;
}

传递的图像是:

enter image description here

我从这张图片中什么也没得到。我也尝试过使用彩色图像,也没有翻转图像。

有人能指出我正确的方向吗?

谢谢!

最佳答案

您应该检查以确保传递给您的函数的 UIImageimg.CGImage 不是 nil,因为您的其余部分代码似乎没问题,尽管翻转不是必需的。例如:

UIImageView *imageView = [[UIImageView alloc] initWithImage: img];
CIImage *image = [CIImage imageWithCGImage:img.CGImage];

CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeText
context:nil
options:@{
CIDetectorAccuracy: CIDetectorAccuracyHigh}];
// retrive array of CITextFeature
NSArray *features = [detector featuresInImage:image options:@{CIDetectorReturnSubFeatures: @YES}];

for(CITextFeature *feature in features) {
UIView *view = [[UIView alloc] initWithFrame: CGRectMake(feature.bounds.origin.x, image.size.height - fear.bounds.origin.y - feature.bounds.height, fear.bounds.width, feature.bounds.height)];
view.backgroundColor = [[UIColor redColor] colorWithAlphaComponent: 0.25];
[imageView addSubview: view];
}

产生的结果: enter image description here

其中红色突出显示表示从CIDetector返回的边界

关于ios - CIDetector featuresInImage 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com