In opencv how do I copy and scale a greyscale image into another colour image

前端 未结 1 1384
甜味超标
甜味超标 2020-12-19 18:39

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

相关标签:
1条回答
  • 2020-12-19 18:45

    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.

    0 讨论(0)
提交回复
热议问题