Ask Your Question
0

How to convert CV_IMAGE_ELEM from IplImage* to Mat ?

asked 2015-05-29 03:14:16 -0600

sidPhoenix gravatar image

One of the lines of the code that i'm editing from IplImage* to Mat is

CV_IMAGE_ELEM(output, float, pit->y, pit->x) = CV_IMAGE_ELEM(SWTImage, float, pit->y, pit->x);

Please tell me how to convert it into Mat format for C++.

edit retag flag offensive close merge delete

Comments

For helping to pass from IplImage to Mat, tell us the code. If you are trying to read an image, then use cv::imread...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-29 07:05:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-29 07:06:32 -0600

theodore gravatar image

updated 2015-05-29 07:24:58 -0600

the CV_IMAGE_ELEM was a macro in the old C opencv api in order to obtain the pixel value of an image. Assume a gray-scale image, access to the row i, column j pixel can be:

CV_IMAGE_ELEM (image, type, i, j);

or in case of a colour image:

CV_IMAGE_ELEM (image, type, i, Nc * j + c);

, where

  • Nc: The total number of channels
  • c: The channel you're interested in (varies from 0 to 3)

For example:

CV_IMAGE_ELEM (image, uchar, i, 3 * j)
CV_IMAGE_ELEM (image, uchar, i, 3 * j +1)
CV_IMAGE_ELEM (image, uchar, i, 3 * j +2)

Now if you want to transform this into the new C++ api you can do it different ways:

You use the at method to access the value at a particular position (i, j):

type elem_a= matrix.at<type>[c](i,j); //access element aij, with i from 0 to rows-1 and j from 0 to cols-1

Instead of specifying the position with i and j, you can use a Point object:

Point p=Point(x,y);
type elem_a= matrix.at<type>[c](p); //Warning: y ranges from 0 to rows-1 and x from 0 to cols-1

or you can use the ptr method to obtain a pointer to a particular row. Then you use the [] to access a particular pixel in a particular channel:

type elem = matrix.ptr<type>(i)[Nc * j + c]

, where

  • type: The datatype (float, int, uchar, etc)
  • i: The row you're interested in
  • Nc: The total number of channels
  • j: The column you're interested in
  • c: The channel you're interested in (varies from 0 to 3, in case of single channel this can be omitted)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-05-29 03:14:16 -0600

Seen: 1,864 times

Last updated: May 29 '15