要在图片上标识出所有的Aruco标记,直接上代码。需要导入opencv库,我是用了别人编译好的C++库直接使用的。下载地址
- (void)detectARMarker:(UIImage *)input_img {
cv::Mat mat;
UIImageToMat(input_img, mat);
cvtColor(mat, mat, COLOR_RGBA2RGB);
std::vector<int> ids;
std::vector<std::vector<cv::Point2f> > corners;
// 获取 ArUco 模块中预定义的 AR 标记字典
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
// 使用 detectMarkers 检测 AR 标记
// 将 AR 标记的 ID 存储在 ids 中,并将 AR 标记的坐标存储在角点中
cv::aruco::detectMarkers(mat, dictionary, corners, ids);
// 在原始图像上绘制检测到的 AR 标记
if (ids.size() > 0)
cv::aruco::drawDetectedMarkers(mat, corners, ids); // draw bounding boxes.
// 将 Mat 类型的图像转换为 Swift 可以处理的 UIImage 类型
UIImage *output_img = MatToUIImage(mat);
_image = output_img;
// 将 id 和角转换为 {id <NSNumber>:corner <NSArray [CGPoint]>} 形式的字典并存储在 mutableDict
NSMutableDictionary *mutableDict = @{}.mutableCopy;
for (int i = 0; i < ids.size(); i++)
{
auto id = ids[i];
NSNumber *markerId = [NSNumber numberWithInt:id];
NSMutableArray *corner = [[NSMutableArray alloc] initWithCapacity:corners[i].size()];
for (auto point: corners[i]) {
[corner addObject:[NSValue valueWithCGPoint:CGPointMake(point.x, point.y)]];
}
mutableDict[markerId] = corner;
}
_markerDict = mutableDict;
}
以上是静态图片的识别,利用摄像头实时标记aruco已经完成,后期会贴出代码。
参考文章 GitHub地址 https://github.com/dparksports/aruco-arkit-opencv
OpenCV官网下载地址 https://opencv.org/releases/
网友评论