嗨,目前我正在开发一个 OCR 阅读应用程序,我已经成功地使用 AVFoundation 框架捕获了卡片图像。
下一步,我需要找出卡片的边缘,以便我可以从主要捕获的图像中裁剪卡片图像,然后我可以将其发送到 OCR 引擎进行处理。
现在的主要问题是找到卡片的边缘,我正在使用下面的代码(取自另一个开源项目),它使用 OpenCV 来实现此目的。如果卡片是纯矩形卡片或纸,它工作正常。但是当我使用圆角卡(例如驾驶执照)时,它无法检测到。另外我在 OpenCV 方面没有太多专业知识,有人可以帮我解决这个问题吗?
- (void)detectEdges
{
cv::Mat original = [MAOpenCV cvMatFromUIImage:_adjustedImage];
CGSize targetSize = _sourceImageView.contentSize;
cv::resize(original, original, cvSize(targetSize.width, targetSize.height));
cv::vector<cv::vector<cv::Point>>squares;
cv::vector<cv::Point> largest_square;
find_squares(original, squares);
find_largest_square(squares, largest_square);
if (largest_square.size() == 4)
{
// Manually sorting points, needs major improvement. Sorry.
NSMutableArray *points = [NSMutableArray array];
NSMutableDictionary *sortedPoints = [NSMutableDictionary dictionary];
for (int i = 0; i < 4; i++)
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithCGPoint:CGPointMake(largest_square[i].x, largest_square[i].y)], @"point" , [NSNumber numberWithInt:(largest_square[i].x + largest_square[i].y)], @"value", nil];
[points addObject:dict];
}
int min = [[points valueForKeyPath:@"@min.value"] intValue];
int max = [[points valueForKeyPath:@"@max.value"] intValue];
int minIndex;
int maxIndex;
int missingIndexOne;
int missingIndexTwo;
for (int i = 0; i < 4; i++)
{
NSDictionary *dict = [points objectAtIndex:i];
if ([[dict objectForKey:@"value"] intValue] == min)
{
[sortedPoints setObject:[dict objectForKey:@"point"] forKey:@"0"];
minIndex = i;
continue;
}
if ([[dict objectForKey:@"value"] intValue] == max)
{
[sortedPoints setObject:[dict objectForKey:@"point"] forKey:@"2"];
maxIndex = i;
continue;
}
NSLog(@"MSSSING %i", i);
missingIndexOne = i;
}
for (int i = 0; i < 4; i++)
{
if (missingIndexOne != i && minIndex != i && maxIndex != i)
{
missingIndexTwo = i;
}
}
if (largest_square[missingIndexOne].x < largest_square[missingIndexTwo].x)
{
//2nd Point Found
[sortedPoints setObject:[[points objectAtIndex:missingIndexOne] objectForKey:@"point"] forKey:@"3"];
[sortedPoints setObject:[[points objectAtIndex:missingIndexTwo] objectForKey:@"point"] forKey:@"1"];
}
else
{
//4rd Point Found
[sortedPoints setObject:[[points objectAtIndex:missingIndexOne] objectForKey:@"point"] forKey:@"1"];
[sortedPoints setObject:[[points objectAtIndex:missingIndexTwo] objectForKey:@"point"] forKey:@"3"];
}
[_adjustRect topLeftCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"0"] CGPointValue]];
[_adjustRect topRightCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"1"] CGPointValue]];
[_adjustRect bottomRightCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"2"] CGPointValue]];
[_adjustRect bottomLeftCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"3"] CGPointValue]];
}
original.release();
}