cvSetImageROI using greyscale and color images
Domanda
I'm trying to set an image region of interest and add one image into another. One image is a mask (greyscale) and the other one is a color image. Right now, I'm doing
IplImage * _newImg = newImage.getCvImage();
IplImage * _oldBG = tempBG.getCvImage();
CvRect rect = cvRect(100, 100, _newImg->width, _newImg->height);
cvSetImageROI(_oldBG, rect);
cvAdd(_newImg, _oldBG, _newImg, NULL);
cvResetImageROI(_oldBG);
This causes the following error:
OpenCV Error: Assertion failed (src1.size() == src2.size()) in binaryMaskOp,
I've stepped through the code, and both images are the same size. I'm assuming the problem is that one image is color and the other is greyscale. Is there a way to perform the procedure above using images with different channels?
Thanks.
Soluzione
That assertion in OpenCV trunk is CV_Assert( src1.size == dst.size && src1.channels() == dst.channels() );
, located here.
You are best to verify that the image sizes and number of channels are the same, if they are then cvAdd
should be able add them normally. I would recommend having a separate result IplImage for the result parameter to cvAdd
.
To split an image into its separate channels using cvSplit
.
For RGB
// Allocate image planes
IplImage* r = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* g = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* b = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
// Split image onto the color planes
cvSplit( src, r, g, b, NULL );
For RGBA
// Allocate image planes
IplImage* r = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* g = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* b = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* a = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
// Split image onto the color planes
cvSplit( src, r, g, b,a NULL );
cvMerge
does the opposite of cvSplit
.
You could then call cvAdd
on the resulting split images one by one and merge the result back at the end.