首页 > 解决方案 > 为什么 OpenCV QRcodeDetector 可以在一个图像上工作而不是另一个使用 Objective C++ 绑定与 swift 的原因?

问题描述

我正在尝试使用 OpenCV 检测静止图像中的二维码,然后确定其在图像中的位置。我已经成功链接了 OpenCV 库和图像样本,在大多数情况下,OpenCV 代码不会检测到二维码,但是在 swift 中检测二维码的代码每次都有效,所以我知道这不是问题与图像。我在 OpenCV 网站上搜索了文档,但无济于事。我想使用 openCV 来检测二维码和位置的原因是为了对图像进行进一步的操作,并且在 Mat 图像上拥有位置将是有益的。

请注意,我是第一次尝试很多事情的学生。

SWIFT代码

    func detectQRCode(_ image: UIImage?) -> [CIFeature]? {
    if let image = image, let ciImage = CIImage.init(image: image){
        var options: [String: Any]
        let context = CIContext()
        options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
        let qrDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: options)
        if ciImage.properties.keys.contains((kCGImagePropertyOrientation as String)){
            options = [CIDetectorImageOrientation: ciImage.properties[(kCGImagePropertyOrientation as String)] ?? 1]
        } else {
            options = [CIDetectorImageOrientation: 1]
        }
        let features = qrDetector?.features(in: ciImage, options: options)
        return features

    }
    return nil
}

   //qr code
            if let features = detectQRCode(displayPhoto.image), !features.isEmpty{
                for case let row as CIQRCodeFeature in features{
                    print(row.messageString ?? "nope")
                    print(row.bounds)
                    print(row.topLeft)
                    print(row.bottomLeft)
                    print(row.topRight)
                    print(row.bottomRight)
                   // print(row.)
                }
            }

调用 OpenCV 包装器:

print("\(OpenCVWrapper.decodeQRCode(displayPhoto.image!))")

调用纯 C++ 代码

+(NSString *) decodeQRCode: (UIImage *)inputImage {
Mat mat = [inputImage CVMat];

string qrcode = detectAndDecode(mat);
    
return [NSString stringWithFormat: @"Data stored in the QR code is %s", qrcode.c_str()];

}

最后是c++代码

QRCodeDetector qrDecoder = QRCodeDetector();

字符串检测和解码(垫子){

vector<Point> points;

String data = qrDecoder.detectAndDecode(mat, points);

return data;

}

任何线索、提示或提示将不胜感激。如果有人知道任何项目,它允许我检测 QR 码并在图像的其余部分中找到与 QR 相关的某些区域的像素值,这将非常有帮助。

编辑:在使用 detectAndDecode 函数之前,我将图像的大小调整为 (1200X800) 并且我得到了更加一致的结果,是否有我应该瞄准的最佳尺寸?

标签: c++swiftopencvqr-codeobjective-c++

解决方案


推荐阅读