Is there any reason why OpenCV QRcodeDetector will work on one image but not another using Objective C++ bindings with swift?

  c++, objective-c, opencv, qr-code, swift

I am trying to use OpenCV to detect a QR code in a still image, and then determine its position in the image. I have successfully linked the OpenCV library and over a sample of images, OpenCV code will not detect a QR code in the majority of instances, however the code for detecting the QR code in swift works every time, so I know it is not a problem with the image. I’ve searched the docs on the OpenCV website to no avail. The reason I want to use the openCV to detect the QR code and the position is to carry out further manipulation on the image and it would be beneficial to have the positions on the Mat image.

Please note, I am a student trying a lot of things for the first time.

Swift code

    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.)
                }
            }

Call to OpenCV wrapper:

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

Call to pure C++ code

+(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()];

}

Finally c++ code

QRCodeDetector qrDecoder = QRCodeDetector();

String detectAndDecode (Mat mat){

vector<Point> points;

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

return data;

}

Any clues, hints or tips would be greatly appreciated. And if anyone knows of any projects, which allow me to detect a QR code and the find the pixel values of certain areas within the rest of the image in relation to the QR it would be very helpful.

Edit: I resized the image to (1200X800) before using the detectAndDecode function and I am getting much more consistent results, is there an optimal size which I should aim for?

Source: Windows Questions C++

LEAVE A COMMENT