Question

I want to composite a number of images into a single window in openCV. I had found i could create a ROI in one image and copy another colour image into this area without any problems.

Switching the source image to an image i had carried out some processing on didn't work though.

Eventually i found out that i'd converted the src image to greyscale and that when using the copyTo method that it didn't copy anything over.

I've answered this question with my basic solution that only caters for greyscale to colour. If you use other Mat image types then you'd have to carry out the additional tests and conversions.

Was it helpful?

Solution

I realised my problem was i was trying to copy a greyscale image into a colour image. As such i had to convert it into the appropriate type first.

drawIntoArea(Mat &src, Mat &dst, int x, int y, int width, int height)
{
    Mat scaledSrc;
    // Destination image for the converted src image.
    Mat convertedSrc(src.rows,src.cols,CV_8UC3, Scalar(0,0,255));

    // Convert the src image into the correct destination image type
    // Could also use MixChannels here.
    // Expand to support range of image source types.
    if (src.type() != dst.type())
    {
        cvtColor(src, convertedSrc, CV_GRAY2RGB);
    }else{
        src.copyTo(convertedSrc);
    }

    // Resize the converted source image to the desired target width.
    resize(convertedSrc, scaledSrc,Size(width,height),1,1,INTER_AREA);

    // create a region of interest in the destination image to copy the newly sized and converted source image into.
    Mat ROI = dst(Rect(x, y, scaledSrc.cols, scaledSrc.rows));
    scaledSrc.copyTo(ROI);
}

Took me a while to realise the image source types were different, i'd forgotten i'd converted the images to grey scale for some other processing steps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top