kubci98
kubci98

Reputation: 398

OpenCV: cv::Mat fails to create a matrix out of seemingly valid data

I am using OpenCV for android in my project to process a YUV420 (https://wiki.videolan.org/YUV/#NV12) image. As the rest of the project is in C#, I am using GCHandles to copy an array of bytes to the C++ code. I have made sure that the data are OK in C++ by making a sum of all the bytes both in C# and C++ and comparing them. However, when I try this:

int foo(unsigned char * inputPtr, int width, int height) {
    cv::InputArray input_image = cv::Mat(height + height /2, width, CV_8UC1, inputPtr);

    int res = 0;
    for (int y = 0; y < input_image.rows(); y++) {
            for (int x = 0; x < input_image.cols(); x++) {
                res += (int)input_image.getMat().ptr(x + y * input_image.cols());
        }
    }
    return res;
}

, zero is returned every time (the C# count still returns correct numbers). The sum of all bytes that returns correct values looks like this:

int foo(unsigned char * inputPtr, int width, int height) {
    int res = 0;

    for (int i = 0; i < width * height * 1.5f; i++) {
        res += (int)inputPtr[i];
    }
    return res;
}

The cv::Mat() arguments should be correct based on this question: Converting from YUV colour space to RGB using OpenCV

What reason could OpenCV have to decide not to create a matrix out of seemingly valid data?

EDIT: Forgot to add that the inputPtr pointer points to a C# byte[] with the size of (int)(camBytes.Width * camBytes.Height * 1.5f).

Upvotes: 0

Views: 1062

Answers (1)

Nuzhny
Nuzhny

Reputation: 1927

Ptr return pointer but no value. Use at():

res += (int)input_image.getMat().at<uchar>(y, x);

Upvotes: 1

Related Questions