Skip to content

base64ToMat failing with JPEG image on iOS #97

@HugoBde

Description

@HugoBde

I am trying to feed a base64 image into a model, and I'm trying to use OpenCV to convert the base64 image data to a Float32Array as follow (example taken from another issue):

const matImage = OpenCV.base64ToMat(base64String);
cv.OpenCV.invoke('convertTo', matImage, matImage, DataTypes.CV_32F, 1 / 255);
const { buffer, channels, cols, rows } = cv.OpenCV.matToBuffer(matImage, 'float32');

However, on iOS, I end up with an empty buffer, channels is 1, cols and rows are both 0.

I tried some different inputs on both platforms and I noticed that this issue only happens when the base64 string represents a JPEG image, and on iOS.

Minimal reproducible example:

// Both base64 strings can be checked for validity here: https://base64.guru/converter/decode/image

// Should be a 8x8 pixels grayscale image
const jpegImageString = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAIAAgBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAACP/EAB4QAAEBCQAAAAAAAAAAAAAAABETEhUhN0FhZYSS/9oACAEBAAA/AH7JbMvnXSS7aKtgKmH/2Q=="

// Should be a 3 channels 8x8 image
const pngImageString = "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII="

const jpegImage = OpenCV.base64ToMat(jpegImageString);
// buffer is empty
const { buffer, channels, cols, rows } = cv.OpenCV.matToBuffer(matImage, 'uint8'); 

const pngImage = OpenCV.base64ToMat(pngImageString);
// buffer is filled, channels == 3, cols and rows == 8
const { buffer, channels, cols, rows } = cv.OpenCV.matToBuffer(matImage, 'uint8'); 

As stated above, this issue only seems to happen on iOS, the same code on my Android device will produce the expected result for the JPEG image.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions