trouble passing image from c to matlab..

1 view (last 30 days)
Vikash Anand
Vikash Anand on 11 May 2011
...
mxArray *mat1;
UINT8_T *dynamicData;
mwSize index;
unsigned long int elements;
elements = (ImgTempTp->width)*(ImgTempTp->height);
dynamicData = mxCalloc(elements, sizeof(UINT8_T));
for ( index = 0; index < elements; index++ ) {
dynamicData[index] = ImgTempTp->imageData[index];
}
mat1 = mxCreateNumericMatrix(0, 0, mxUINT8_CLASS, mxREAL);
mxSetData(mat1, dynamicData);
mxSetM(mat1, ImgTempTp->height);
mxSetN(mat1, ImgTempTp->width);
mlfShowimage(mat1);
.
.
ImgTempTp is an image created by opencv. the data type is unsigned char.
function [] = showimage(img)
figure,imshow(img);
input('Press Enter to continue...');
end
Some times the image appears properly but many times it gets mixed up in 2-3 manners i.e. it has 2-3 sections which contains parts of original image in tilted fashion
I have tried memcpy also. But i get the same error.. PLs help...
  2 Comments
Vikash Anand
Vikash Anand on 11 May 2011
If i try loading an image using cvLoadImage in opencv and then pass it to matlab, i get different images at each run.
Also, the distorted image has max. no. of sections as 4.
Vikash Anand
Vikash Anand on 11 May 2011
result :
https://sites.google.com/site/scrapfromvks/matlab
these images have been captured by opencv haar detect and passed on to matlab.. Opencv displays all of these correctly.

Sign in to comment.

Answers (2)

Kaustubha Govind
Kaustubha Govind on 11 May 2011
Is there a reason your are using a rather roundabout way to achieve this? Why not use something like:
...
mxArray *mat1;
UINT8_T *dynamicData;
mwSize index;
unsigned long int elements;
elements = (ImgTempTp->width)*(ImgTempTp->height);
mat1 = mxCreateNumericMatrix(ImgTempTp->height, ImgTempTp->width, mxUINT8_CLASS, mxREAL);
dynamicData = (UINT8_T *)mxGetData(mat1);
for ( index = 0; index < elements; index++ ) {
dynamicData[index] = ImgTempTp->imageData[index];
}
mlfShowimage(mat1);
.
.
Do you know if ImgTempTp->imageData contains the image in the right indexing order? It seems like the image is being read along the diagonal. Have you tried examining the contents of ImgTempTp->imageData and comparing them with the value 'img' that's passed into showimage?
  3 Comments
Vikash Anand
Vikash Anand on 11 May 2011
The first round about manner i found in "arrayFillSetData.c" in matlab examples.. so just followed it.
Before that, i also tried:
mxArray *mat1;
mwSize dims[2];
dims[0] = (mwSize)(ImgTempTp->width);
dims[1] = (mwSize)(ImgTempTp->height);
mat1 = mxCreateNumericArray(2,dims,mxUINT8_CLASS,mxREAL);
memcpy(mxGetPr(mat1), (uchar *)(ImgTempTp->imageData), (ImgTempTp->width)*(ImgTempTp->height)*sizeof(char));
mlfShowimage(mat1);
but none of these worked !! :(
Kaustubha Govind
Kaustubha Govind on 11 May 2011
Could it be than opencv is somehow not storing the pixel values in the expected linear order?

Sign in to comment.


Vikash Anand
Vikash Anand on 13 May 2011
Hey govinda.. i found the solution .. It is something related to 4 bytes alignment. I resized the image in opencv before transferring to matlab such that the dimensions were multiple of 4 and it worked perfectly !!! :)

Community Treasure Hunt

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

Start Hunting!