Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
Announcements
Important Update: Community Platform Migration​. Learn more​>

OpenCV to IPP

bibi88
Beginner
1,915 Views
Hello,

I've some problem, trying to convert the linear structure of an IPL (OpenCV) image into an IPP image structure.
When I display the IPP matrix of the picture, it appears that it's not the expected one.
Here is my code:

typedef struct IppImage_8u IppImage_8u;
struct IppImage_8u{
Ipp8u* data[4]; // 4 layers (RGB for example+gamma)
int width[4];
int height[4];
int stepWidth[4];
};

IplImage* pcv_Img contains an image.
IppImage_8u* pipp_Img is allocated (sizeof(IppImage_8u))
pipp_Img->data[0] = NULL;
pipp_Img->data[1] = NULL;
pipp_Img->data[2] = NULL;
pipp_Img->data[0] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[0]));
pipp_Img->data[1] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[1]));
pipp_Img->data[2] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[2]));
pipp_Img->data[0] = (Ipp8u *) &pcv_Img->imageData[0];
pipp_Img->data[1] = (Ipp8u *) &pcv_Img->imageData[pcv_Img->width*pcv_Img->height];
pipp_Img->data[2] = (Ipp8u *) &pcv_Img->imageData[2*pcv_Img->width*pcv_Img->height];
pipp_Img->width[0] = pcv_Img->width;
pipp_Img->width[1] = pcv_Img->width;
pipp_Img->width[2] = pcv_Img->width;
pipp_Img->height[0] = pcv_Img->height;
pipp_Img->height[1] = pcv_Img->height;
pipp_Img->height[2] = pcv_Img->height;


Here is the displaying method that I use:

int i,j,k;
for (k=0;k<3;k++)
{
for (i=0;iheight;i++)
{
for (j=0;jwidth;j++)
{
printf("%d ",pipp_Img->data[i*pipp_Img->widthStep+j]);
}
printf("\\n");
}
printf("\\n \\n \\n");
}

The displayed number doesn't correspond to what, for example, Matlab says. Could you please explain me why?

Thanks in advance for your help!
0 Kudos
4 Replies
Vladimir_Dudnik
Employee
1,915 Views
Hello,

unfortunately I did not get the idea you show with code snippet. Why do you overwrite pipp_Img->data pointers?

pipp_Img->data[0] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[0]));
pipp_Img->data[1] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[1]));
pipp_Img->data[2] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[2]));

// the assigments below will overwrite pipp_Img->data pointers, resulting in loose of just allocated memory
pipp_Img->data[0] = (Ipp8u *) &pcv_Img->imageData[0];
pipp_Img->data[1] = (Ipp8u *) &pcv_Img->imageData[pcv_Img->width*pcv_Img->height];
pipp_Img->data[2] = (Ipp8u *) &pcv_Img->imageData[2*pcv_Img->width*pcv_Img->height];

If you try to represent IplImage datawith your IppImage_8u data structure you also need to ensure you use IplImage step (aka pitch) to navigate correctlythrough IplImage pixels.

Vladimir
0 Kudos
bibi88
Beginner
1,915 Views
Hello,

Sorry for the misunderstanding! My problem is quite easy in fact. I've a 3 component picture in a format char* old_version_data, with height "old_version_height", width "old_version_width" and a pitch "old_version_pitch".
I would like to get this picture in an ippi format, so I've defined a Ipp8u* ipp_data[3], IppSize ipp_size and int ipp_pitch.
How can I do the translation from "old_version" to ipp?

Thanks in advance!
0 Kudos
Ying_H_Intel
Moderator
1,915 Views
Hello bibi88,

No sure if i understand correct,

but if youare using thestructIplImage* pcv_Img in OpenCV( i.e OpenCV 2.1.0), then the two struct of IPL image and IPP image is almost aligned witheach other, you don't need to any change.

For example
// 3 channel image
IplImage* pSrcImg = cvLoadImage( "testimg.bmp", 1 );
int SRC_WIDTH= pSrcImg->width;
int SRC_HEIGHT=pSrcImg->height;
CvSize cvsrcSize={ SRC_WIDTH,SRC_HEIGHT};
IppiSize t_size={SRC_WIDTH, SRC_HEIGHT};

when call ipp function (ipp image)
the width is same, height is same, step is same. Just please notes theimageData pointer.

// copy the OpenCV IPL image to IPP image
Ipp8u *ipp_template = ippiMalloc_8u_C3( SRC_WIDTH, SRC_HEIGHT, &stepByte );
ippiCopy_8u_C3R((Ipp8u*)pSrcImg->imageData, (Ipp8u) pSrcImg->widthStep, ipp_template, stepByte, t_size);
more dicussion are in Intel Ipp - Open Source Computer Vision Library (OpenCV) FAQ


So reviewyour code, 3 commponent picture or4 layers (RGB for example+gamma).
the keyproblem is the storage format.
is it
RGBRGBRGBRGB...
or
RRRRRRR
GGGGGGG
BBBBBBB ?

If it is former, then the stucture is alignment naturally as above sample. The convertion is not needed.

If it is latter,
first the memory allocation is not needed
pipp_Img->data[0] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[0]));
pipp_Img->data[1] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[1]));
pipp_Img->data[2] = ippiMalloc_8u_C1(pcv_Img->width, pcv_Img->height, &(pipp_Img->stepWidth[2]));

It is ok to use the memory from pcv_Image.
But second question, are you sure the R frame and G frame and B frame are stored in IPL like the below? (could you show how*version_version_datadefined based onpcv_Img?)
pipp_Img->data[0] = (Ipp8u *) &pcv_Img->imageData[0];
pipp_Img->data[1] = (Ipp8u *) &pcv_Img->imageData[pcv_Img->width*pcv_Img->height];
pipp_Img->data[2] = (Ipp8u *) &pcv_Img->imageData[2*pcv_Img->width*pcv_Img->height];

According to your code
int i,j,k;
for (k=0;k<3;k++)
{
for (i=0;iheight;i++)
{
for (j=0;jwidth;j++)
{
printf("%d ",pipp_Img->data[i*pipp_Img->widthStep+j]);
}
printf("\n");
}
printf("\n \n \n");
}

It seems
at least should be

pipp_Img->data[0] = (Ipp8u *) &pcv_Img->imageData[0];
pipp_Img->data[1] = (Ipp8u *) &pcv_Img->imageData[pcv_Img->widthStep*pcv_Img->height];
pipp_Img->data[2] = (Ipp8u *) &pcv_Img->imageData[2*pcv_Img->widthStep*pcv_Img->height];

Please notes the width is not widthStep
(awhole rowin bytes, may be 4 bytes align of 32 bytes align).

Regards,
Ying
0 Kudos
bibi88
Beginner
1,915 Views
Thank you very much, IplImage was following the interleaved format (RGB RGB RGB and not a planar RRR GGG BBB), that's why I had so many problems! Sorry for my dummy question!
0 Kudos
Reply