在opencv中如何将灰度图像复制并缩放到另一个彩色图像中 [英] In opencv how do I copy and scale a greyscale image into another colour image

查看:728
本文介绍了在opencv中如何将灰度图像复制并缩放到另一个彩色图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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.

推荐答案

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

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.

这篇关于在opencv中如何将灰度图像复制并缩放到另一个彩色图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆