trouble passing image from c to matlab..

...
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

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.
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)

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

Thanx for your reply, but this too didn't work... :( ..
the images i have uploaded seem to have a pattern in error : proper image/3 sections for images with "even" dimensions and 2/4 sections for "odd" dimensions.
Amazingly, if i create an opencv structure and copy the data from "mat1" to it, opencv displays it properly ..
unsigned char *dest,*src;
IplImage*ImgTest = cvCreateImage(cvSize(ImgTemp->height,ImgTemp->width),IPL_DEPTH_8U,1); /*dimensions of temp interchanged for transposing*/
bytescount = (ImgTempTp->width)*(ImgTempTp->height)*sizeof(char)
src = (unsigned char *)mxGetPr(mat1);
dest = (uchar *)(ImgTest->imageData);
for(j=0;j< bytescount;j++)
{
dest[j] = src[j];
}
cvShowImage("EBGM Debug",ImgTempTp);
cvWaitKey(2000);
ignore the transpose...
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 !! :(
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 !!! :)

Asked:

on 11 May 2011

Community Treasure Hunt

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

Start Hunting!