How Matlab coder convert the image to unsinged char

5 views (last 30 days)
good day
I am working with matlab Coder which convert a image function to c++ code. the function looks something like this array=findsomething(I); where I is an image has been read with the function Imread. the result code in c++ code which has emxArray_uint8_T which looks like this struct emxArray_uint8_T { unsigned char *data; int *size; int allocatedSize; int numDimensions; boolean_T canFreeData; }; the data from the image should be a stream in unsigned char *data,i can read an image from OpenCV but i need to convert the image type from Mat to unsighed char, i have already read http://www.mathworks.com/help/matlab/matlab_external/matlab-data.html#f22019 but i dont know which comes first red blue or green,and if red should i start with coloms or rows? can someone help

Accepted Answer

Ah
Ah on 2 Dec 2016
Edited: Ah on 2 Dec 2016
so i found out how to fill this emxArray_uint8_T with unsinged char, in c++ there is the code
emxArray_uint8_T *I;
emxInit_uint8_T(&I, 3);
string imageName("F:/********amr.jpg");
Mat image;
image = imread(imageName.c_str(), IMREAD_COLOR); // Read the file
Vec3b intensity;
int sizeimage, counter;
int rows = image.rows;
int cols = image.cols;
sizeimage = rows*cols;
I->size[0] = rows;
I->size[1] = cols;
I->size[2] = 3;
emxEnsureCapacity((emxArray__common *)I, 0, (int)sizeof(unsigned char));
counter = 0;
for (int i = 0;i < cols;i++)
{
for (int j = 0;j < rows;j++)
{
intensity = image.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
I->data[counter] = red;
I->data[counter + sizeimage] = green;
I->data[counter + 2* sizeimage] = blue;
counter++;
}
}
now the data is transfered from the image into the emxArray_uint8_T which u can use in your c++ code i had the same results of my function in Matlab and my c++ program.
  1 Comment
Peyman Ghasemi
Peyman Ghasemi on 15 Nov 2017
Hi,
This is very well. I converted my Mat image to the emxArray but I couldn't run it in the main MATLAB generated function. Also I have no idea for the reverse conversion: emxArray to Mat image.
Do you have any library as the interface between the OpenCV and MATLAB Coder?
Regards;
Peyman

Sign in to comment.

More Answers (1)

Saipraveen
Saipraveen on 30 Sep 2019
Answering for other users, I have created a file exchange file that converts image to a c/c++ array file that could be used. https://www.mathworks.com/matlabcentral/fileexchange/72535-image-to-c-c-array-converter.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!