3

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

将源图像切换到我已经进行了一些处理的图像虽然不起作用。

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

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

4

1 回答 1

4

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

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

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

于 2012-12-15T15:25:07.263 回答