【发布时间】:2018-02-27 12:26:28
【问题描述】:
我正在使用在线 OCR 服务,它会为我提供每个已识别的单词及其上/左点、宽度和高度(以像素为单位)。
我很难将它们转换为点并在识别的单词周围绘制一个矩形,因为(我认为)除了从像素到点本身的转换之外,包含图像的 imageView(以及我想要的绘制矩形)具有“缩放方面适合”内容模式。
这就是我获取图像并将其发送到网络服务的方式:
__weak typeof(self) weakSelf = self;
[self.cameraView captureImageWithCompletionHander:^(id image)
{
self.cameraView.enableTorch = NO;
self.captureImageView = [[UIImageView alloc] initWithImage:image];
self.captureImageView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
self.captureImageView.frame = CGRectOffset(weakSelf.view.bounds, 0, -weakSelf.view.bounds.size.height);
self.captureImageView.alpha = 1.0;
self.captureImageView.contentMode = UIViewContentModeScaleAspectFit;
self.captureImageView.userInteractionEnabled = YES;
[self.cameraView addSubview:self.captureImageView];
NSData *imageData = UIImagePNGRepresentation(image);
NSURL *urlSave = [[self getDocumentsPathURL] URLByAppendingPathComponent:k_IMAGE_NAME];
[imageData writeToFile:urlSave.path atomically:NO];
OCRController *ocrController = [OCRController sharedInstance];
[ocrController callOCRSpace:(UIImage*)image
completion:^(NSArray *responseObject) {
[ProgressHUD dismiss];
NSLog(@"Results: %@", responseObject);
dispatch_async(dispatch_get_main_queue(), ^{
[self drawRectangleOnImage:self.scannedImage rect:responseObject];
});
} error:^(NSError *error) {
[ProgressHUD dismiss];
NSLog(@"Error OCR: %@", error.localizedDescription);
}];
这是绘制矩形的方法(我现在只尝试第一个):
- (void)drawRectangles:(NSArray*)objects {
Words *firstObj = objects[0];
NSLog(@"Word: %@", firstObj.wordText);
UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake([firstObj.left floatValue] / 4, [firstObj.top floatValue] / 4, [firstObj.width floatValue] / 4, [firstObj.height floatValue] / 4)];
rectView.layer.borderColor = [UIColor redColor].CGColor;
rectView.layer.borderWidth = 2.0;
NSLog(@"Rect.:\nTop: %.2f - Left: %.2f\nWidth: %.2f - Height: %.2f", [firstObj.top floatValue] / 4, [firstObj.left floatValue] / 4, [firstObj.width floatValue] / 4, [firstObj.height floatValue] / 4);
[self.captureImageView addSubview:rectView];
}
这是图像左上角第一个检测到的单词“Gracias”的结果:
在 Prashant Tukadiya 的回答之后编辑:
- (void)drawRectangles:(NSArray*)objects {
Words *firstObj = objects[0];
CGPoint imagePoints = CGPointMake([firstObj.left doubleValue], [firstObj.top doubleValue]);
float percentX = imagePoints.x / self.scannedImage.size.width;
float percentY = imagePoints.y / self.scannedImage.size.height;
CGPoint imageViewPoints = CGPointMake(self.captureImageView.frame.size.width * percentX, self.captureImageView.frame.size.height * percentY);
UIView *rectView = [[UIView alloc] initWithFrame:CGRectMake(imageViewPoints.x, imageViewPoints.y, [firstObj.width floatValue], [firstObj.height floatValue])];
rectView.layer.borderColor = [UIColor redColor].CGColor;
rectView.layer.borderWidth = 2.0;
[self.captureImageView addSubview:rectView];
}
我还从链接的答案中添加了 UIImageView+GeometryConversion 类别,但我真的不知道如何使用它。
我对所有这些坐标的东西都很陌生,我真的迷路了。
这是目前的结果,看起来我的 X 是对的,至少:
第二次编辑:我很接近了
所以,我找到了另一个示例并创建了一个新方法:
-(void)drawRectangleOnImage:(UIImage *)img rect:(NSArray *)objects {
CGSize imgSize = img.size;
CGFloat scale = 0;
UIGraphicsBeginImageContextWithOptions(imgSize, NO, scale);
[img drawAtPoint:CGPointZero];
for (Words *item in objects) {
CGRect rect = [self.captureImageView convertRect:CGRectMake([item.left doubleValue], [item.top doubleValue], [item.width floatValue], [item.height floatValue]) toView:self.captureImageView];
[[UIColor greenColor] setFill];
UIRectFill(rect);
NSLog(@"Palabra: %@\nRect: %@\n\n", item.wordText, NSStringFromCGRect(rect));
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.captureImageView.image = newImage;
}
这次我已经能够在每个识别的单词上画一个矩形。现在的问题是:
画一个空的矩形(我还没能做到,是的,我觉得自己很蠢)
让用户可以缩放图像,触摸图像上的单词,然后在该单词周围绘制矩形
这是我的最后一个结果:
【问题讨论】:
-
根据图片你得到了哪些分数?
-
顶部:163.75 - 左侧:134.50 宽度:86.25 - 高度:26.25
-
尝试根据uiimageView转换那个点。你做错了
[firstObj.left floatValue] / 4,你需要扭转他在这里所做的事情stackoverflow.com/questions/17200191/… -
我不确定,但
CGRect rect = [self.captureImageView convertRect:CGRectMake([item.left doubleValue], [item.top doubleValue], [item.width floatValue], [item.height floatValue]) toView:self.captureImageView];它没有用意味着它将返回相同的坐标,无论通过哪个。并且您可以使用 StrokeColor 来提供边框而不是填充 -
对于让用户可以缩放图像,触摸图像上的一个单词,然后在该单词周围绘制矩形,您可以使用您的 self.zoomScale scrollview 的 zoomScale 属性。您需要根据用户操作将您的点与缩放比例分开或将您的点与缩放比例相乘。希望对你有帮助
标签: ios objective-c drawing pixels