Borrar filtros
Borrar filtros

Converting coder::array<unsigned char, 3U> to unsigned char

26 visualizaciones (últimos 30 días)
Abhijith Sreekumar
Abhijith Sreekumar el 28 de Jun. de 2022
Editada: cui,xingxing el 23 de Feb. de 2023
I am using Matlab coder to generate c++ codes for a script i have made. The output datatype of MATLAB coder is coder::array<unsigned char, 3U>. Would you please help me figure out how to convert this into unsigned char so that i can use it in opencv.
Thank you
  1 comentario
Alexander Bottema
Alexander Bottema el 24 de Ag. de 2022
To directly access the underying data of a coder::array you can use the ".data()" method. The total number of elements can be computed via the ".numel()" method. Size of each dimension is available via the ".size(dimension)" method.
I attached a full example (see example.zip).
example.m:
function y = example(n)
y = zeros(n,n,n,'uint8');
for i = 1:numel(y)
y(i) = 64 + i;
end
main.cpp:
#include <iostream>
#include "example.h"
#include "coder_array.h"
int main(int argc, char **argv[])
{
(void)argc;
(void)argv;
coder::array<unsigned char, 3U> out;
example(2, out);
int n = out.numel();
unsigned char *data = out.data();
for (int i = 0; i < n; i++) {
std::cout << data[i];
}
std::cout << std::endl;
return 0;
}
doit.m:
cfg = coder.config('exe');
cfg.TargetLang = 'C++';
codegen -args 0 -config cfg example main.cpp
And running the executable gives:
>> !example (!./example on Linux)
ABCDEFGH

Iniciar sesión para comentar.

Respuestas (1)

Anirban Ghosh
Anirban Ghosh el 24 de Ag. de 2022
Hi Abhijith,
Please refer to this documentation page for information on using the coder:array template: Use Dynamically Allocated C++ Arrays in Generated Function Interfaces. In particular, note the "Using the coder::array Class Template" section and the two examples that follow.
If your output (say myArray) is of type coder::array<unsigned char, 3U>, it contains elements of type unsigned char and has 3 dimensions.
  • To access the sizes of the three dimensions, call the size method: myArray.size(0), myArray.size(1), and myArray.size(2).
  • To access the (i,j,k)-th element of myArray, call the at method: myArray.at(i,j,k). This call will return the unsigned char value stored at that location.
  • In your code, you can pre-allocate an unsigned char array and populate it with these values.
  1 comentario
cui,xingxing
cui,xingxing el 23 de Feb. de 2023
Editada: cui,xingxing el 23 de Feb. de 2023
How should I set the data elements of coder::array if the other way around?(Instead of copying element by element, the in-place method), I'm following the linked documentation and trying myself, but unfortunately I couldn't make it work!

Iniciar sesión para comentar.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by