我想在openCV中将多个图像合成到单个窗口中。我发现我可以在一张图像中创建ROI,然后将另一张彩色图像复制到该区域而没有任何问题。

但是,将源图像切换为我已经对其进行了一些处理的图像是行不通的。

最终,我发现我已经将src图像转换为灰度图像,并且在使用copyTo方法时,它没有复制任何内容。

我已经用基本的解决方案回答了这个问题,该解决方案只适合灰度到彩色。如果使用其他Mat图像类型,则必须进行其他测试和转换。

最佳答案

我意识到我的问题是我试图将灰度图像复制到彩色图像中。因此,我必须先将其转换为适当的类型。

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);
}

花了我一段时间才意识到图像源类型是不同的,我忘记了将图像转换为灰度用于其他一些处理步骤。

07-27 19:38