Home > Backend Development > C++ > How to Efficiently Convert OpenCV Mat to an Array or Vector?

How to Efficiently Convert OpenCV Mat to an Array or Vector?

Susan Sarandon
Release: 2024-10-27 03:32:30
Original
351 people have browsed it

How to Efficiently Convert OpenCV Mat to an Array or Vector?

Convert Mat to Array or Vector in OpenCV

In OpenCV matrix operations, occasionally there's a need to convert Mat to an Array or a Vector. This article provides a comprehensive guide to achieve such conversions, utilizing OpenCV's robust functionality.

Direct Conversion to Array

If the Mat object mat possesses continuous memory, its data can be directly retrieved as a 1D array:

<code class="cpp">std::vector<uchar> array(mat.rows * mat.cols * mat.channels());
if (mat.isContinuous())
    array = mat.data;</code>
Copy after login

Conversion to Row-Based Array

In cases where the memory is not continuous, the data can be obtained row by row, resulting in a 2D array:

<code class="cpp">uchar **array = new uchar*[mat.rows];
for (int i = 0; i < mat.rows; ++i)
    array[i] = new uchar[mat.cols * mat.channels()];

for (int i = 0; i < mat.rows; ++i)
    array[i] = mat.ptr<uchar>(i);</code>
Copy after login

Alternative Vector Conversion

Using std::vector offers an alternative approach for converting Mat to an array:

<code class="cpp">std::vector<uchar> array;
if (mat.isContinuous()) {
    // array.assign(mat.datastart, mat.dataend); // May cause issues for sub-matrices
    array.assign(mat.data, mat.data + mat.total() * mat.channels());
} else {
    for (int i = 0; i < mat.rows; ++i) {
        array.insert(array.end(), mat.ptr<uchar>(i), mat.ptr<uchar>(i) + mat.cols * mat.channels());
    }
}</code>
Copy after login

Converting to Vectors of Other Types

For OpenCV Mats with other data types, such as CV_32F, the conversion process is similar:

<code class="cpp">std::vector<float> array;
if (mat.isContinuous()) {
    // array.assign((float*)mat.datastart, (float*)mat.dataend); // May cause issues for sub-matrices
    array.assign((float*)mat.data, (float*)mat.data + mat.total() * mat.channels());
} else {
    for (int i = 0; i < mat.rows; ++i) {
        array.insert(array.end(), mat.ptr<float>(i), mat.ptr<float>(i) + mat.cols * mat.channels());
    }
}</code>
Copy after login

Understanding Mat Data Continuity

It's crucial to comprehend Mat data continuity to effectively perform conversions. Key points to note are:

  • Mats created through imread(), clone(), or a constructor are inherently continuous.
  • Data discontinuities only occur when a Mat borrows data from an existing Mat (except for certain scenarios involving full original width).

The above is the detailed content of How to Efficiently Convert OpenCV Mat to an Array or Vector?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template