在不缩放的情况下调整图像<gray,byte =“”>的大小。 Emgu CV </gray,>

时间:2011-12-19 04:43:21

标签: c# opencv emgucv

我使用Image.InRange从图像创建蒙版。为了保持最佳性能,我使用Image.ROI裁剪图像,然后再使用InRange方法。为了实际使用图像,我需要它与原始尺寸相同,但对我来说很明显的是如何缩放图像,而不是更改保留图像的尺寸。

以下是相关代码:

public Image<Gray, byte> Process(Image<Bgr, byte> frameIn, Rectangle roi)
    {
        Image<Bgr, byte> rectFrame = null;
        Image<Gray, byte> mask = null;
        if (roi != Rectangle.Empty)
        {
            rectFrame = frameIn.Copy(roi);
        }
        else
        {
            rectFrame = frameIn;
        }

        if (Equalize)
        {
            rectFrame._EqualizeHist();
        }


        mask = rectFrame.InRange(minColor, maxColor);

        mask ._Erode(Iterations);
        mask ._Dilate(Iterations);

        if (roi != Rectangle.Empty)
        {
            //How do I give the image its original dimensions?
        }

        return mask;
    }

谢谢你, 克里斯

1 个答案:

答案 0 :(得分:1)

我假设您希望返回与fram相同大小的掩码。最简单的方法是将掩码复制到与framIn大小相同的新图像。如果您的应用程序不是时间敏感的,那么可以使掩码与framIn设置其ROI相同,然后进行操作。这需要更长的时间来处理,而不是最佳实践。

无论如何,如果没有让我知道,那么希望你的代码是你的,我会相应地纠正它。

if (roi != Rectangle.Empty)
{
    //Create a blank image with the correct size
    Image<Gray, byte> mask_return = new Image<Gray, byte>(frameIn.Size);
    //Set its ROI to the same as Mask and in the centre of the image (you may wish to change this)
    mask_return.ROI = new Rectangle((mask_return.Width - mask.Width) / 2, (mask_return.Height - mask.Height) / 2, mask.Width, mask.Height);
    //Copy the mask to the return image
    CvInvoke.cvCopy(mask, mask_return, IntPtr.Zero);
    //Reset the return image ROI so it has the same dimensions
    mask_return.ROI = new Rectangle(0, 0, frameIn.Width, frameIn.Height);
    //Return the mask_return image instead of the mask
    return mask_return;
}

return mask;

希望这有帮助,

干杯,

克里斯

相关问题