图像处理 – 在opencv中如何将灰度图像复制并缩放到另一个彩色图像中

我想在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,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,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);
}

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

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效