Hi hiện tại tôi đang làm việc trên ứng dụng đọc OCR, nơi tôi có thể chụp ảnh thẻ thành công bằng cách sử dụng khung công tác AVFoundation.Phát hiện các cạnh của thẻ có các góc bo tròn
Bước tiếp theo, tôi cần phải tìm ra các cạnh của thẻ, để tôi có thể cắt hình ảnh thẻ từ hình ảnh chính được chụp & sau đó tôi có thể gửi nó đến công cụ OCR để xử lý.
Vấn đề chính bây giờ là tìm các cạnh của thẻ & tôi đang sử dụng mã bên dưới (được lấy từ một dự án mã nguồn mở khác) sử dụng OpenCV cho mục đích này.Đó hoạt động tốt nếu thẻ là Thẻ hình chữ nhật thuần hoặc Giấy . Nhưng khi tôi sử dụng thẻ có góc tròn (ví dụ: Giấy phép lái xe), nó không thể phát hiện được. Ngoài ra tôi không có nhiều chuyên môn trong OpenCV, bất kỳ ai có thể giúp tôi trong việc giải quyết vấn đề này?
- (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();
}
bạn có thể cung cấp cho tôi liên kết để cắt thẻ không? – faiziii
@raaz i có yêu cầu tương tự, bạn có thể đề xuất cho tôi rằng dự án nguồn mở mà bạn đã sử dụng. Nó sẽ giúp ích rất nhiều. –
@John Bạn sẽ chia sẻ một hoặc hai hình ảnh mẫu? – karlphillip