Page 41 -
P. 41
Chapter 1 ■ Practical Aspects of a Vision System 15
The field nr is the number of rows in the image, and nc is the number
of columns. These are equivalent to IplImage.height and IplImage.width,
respectively. The oi and oj fields specify the origin of the image, and are used
only for a very few cases (e.g., restoration). There are no corresponding fields
in OpenCV.
The way to convert an AIPCV image into an OpenCV image is now clear,
and is needed so that images can be displayed in windows and saved in
JPEG and other formats.
IplImage *toOpenCV (IMAGE x)
{
IplImage *img;
int i=0, j=0;
CvScalar s;
img=cvCreateImage(cvSize(x->info->nc,x->info->nr),8, 1);
for (i=0; i<x->info->nr; i++)
{
for (j=0; j<x->info->nc; j++)
{
s.val[0] = x->data[i][j];
cvSet2D (img, i,j,s);
}
}
return img;
}
This function copies the pixel values into a new IplImage. It is also possible
to use the original data array in the IplImage directly. There is some danger in
this, in that OpenCV may decide to free the storage, for instance, making both
versions inaccessible.
Converting from IplImage to AIPCV is more complicated, because OpenCV
images might be in color. If so, how is it converted into grey? We’ll not
dwell on this except to say that one color image can be converted into three
monochrome images (one each for red, green, and blue), or a color map could
be constructed using a one-byte index that could be used as the pixel value.
The solution presented here is to convert a 3-channel color image into grey by
averaging the RGB values, leaving the other solutions for future consideration.
IMAGE fromOpenCV (IplImage *x)
{
IMAGE img;
int color=0, i=0;
int k=0, j=0;
CvScalar s;
if ((x->depth==IPL_DEPTH_8U) &&(x->nChannels==1)) // Grey image