Ask Your Question
0

Convert CvImagePtr* (CvImage) channel to IplImage

asked 2012-09-05 04:29:12 -0600

cduguet gravatar image

updated 2014-01-28 17:13:34 -0600

ngrennan gravatar image

Hi,

I have extracted an Image message using cv_bridge as this:

cv_bridge::CvImagePtr cv_ptr;
cv_ptr = cv_bridge::toCvCopy(msg, enc::MONO8);

and i could convert it to an IplImage using:

IplImage *image2 = NULL;
allocateit( &image2, frame_size, IPL_DEPTH_8U, 1 );
image2->imageData = (char *) cv_ptr->image.data;

where allocateit is a function to allocate space. My problem comes when I want to read a color image as this:

cv_bridge::CvImagePtr cv_ptr;
cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
IplImage *image2 = NULL;
allocateit( &image2, frame_size, IPL_DEPTH_8U, 1 );

and want to copy one channel of *cv_ptr to the image to the IplImage *image2. I have tried:

image2->imageData = (char *) cv_ptr->image.data;

but it crashes,

cv::cvtColor(cv_ptr->image.data,image2->imageData,CV_RGB2GRAY);

but does not compile,

int from_to[] = {0,0};
cv::mixChannels(&(cv_ptr->image.data),1,image2,1, from_to,1);

does not compile either.

What would you recommend me to do? Thanks!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2012-09-11 05:13:04 -0600

cduguet gravatar image

updated 2012-09-11 05:13:43 -0600

I know I should use cv::Mat, in fact I want to use it. But specifically in this problem I cannot use it. (see: http://tech.dir.groups.yahoo.com/group/OpenCV/message/81826).

Anyway I found a (not elegant) way to convert it :

                for(int ko=0; ko< frame_size.width*frame_size.height ; ko++)
{
        image2->imageData[ko] = (char) cv_ptr->image.data[ko*3];
}
edit flag offensive delete link more

Comments

what is frame_size?

karthik gravatar imagekarthik ( 2013-11-05 02:58:01 -0600 )edit
3

answered 2012-09-05 20:12:47 -0600

Mani gravatar image

I strongly recommend using new OpenCV 2.x API, specially the Mat data structure instead of IplImage. It's easy to use split function to access different image channels and the assignment operator or clone() function to copy reference/data.

edit flag offensive delete link more
1

answered 2012-09-05 08:39:31 -0600

jbohren gravatar image

You might find more help on the OpenCV support forum.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-09-05 04:29:12 -0600

Seen: 2,316 times

Last updated: Sep 11 '12