How Matlab coder convert the image to unsinged char

5 ビュー (過去 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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by