How Matlab coder convert the image to unsinged char

조회 수: 9 (최근 30일)
Ah
Ah 2016년 11월 30일
답변: Saipraveen 2019년 9월 30일
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

채택된 답변

Ah
Ah 2016년 12월 2일
편집: Ah 2016년 12월 2일
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
Peyman Ghasemi
Peyman Ghasemi 2017년 11월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Saipraveen
Saipraveen 2019년 9월 30일
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.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by