如何在 Emgu 中将 Mat 裁剪成 ROI(OpenCV for C#)
How to crop a Mat to ROI in Emgu (OpenCV for C#)
我找到的一些答案推荐使用:
frame = ...
Rectangle ROI = ...
Mat crop = new Mat(frame, ROI)
但这在 Emgu 中似乎不起作用 3.x
艾哈迈德,什么不起作用?你没有得到带有数据的垫子吗?什么是 "frame" 是 "frame" 垫子?您的投资回报率是 RotatedRect 吗?因为如果您使用的是不起作用的 RotatedRect,那么您是对的。如果 frame 是一个 Mat 那么你正在做的应该工作得很好。需要一些关于什么不起作用的详细信息。
我明白了。要裁剪 Mat,您必须使用 Image 对象作为缓冲区并使用其 .ROI 属性:
Mat crop_color_frame(Mat input, Rectangle crop_region)
{
/*
* TODO(Ahmed): Figure out why I had to copy this into this class.
* */
Image<Bgr, Byte> buffer_im = input.ToImage<Bgr, Byte>();
buffer_im.ROI = crop_region;
Image<Bgr, Byte> cropped_im = buffer_im.Copy();
return cropped_im.Mat;
}
Ahmed,您真的不必为了创建 Mat 而创建 Image<>。请尝试以下操作:
Mat myNewMat = new Mat(input, crop_region);
由于您没有 RotatedRect,因此您不必将旋转的矩形转换为直立的矩形。因此,您只需从现有的 Mat 和矩形创建一个新的 Mat。
这应该有效,并且可以简化您的代码。
道格
我找到的一些答案推荐使用:
frame = ...
Rectangle ROI = ...
Mat crop = new Mat(frame, ROI)
但这在 Emgu 中似乎不起作用 3.x
艾哈迈德,什么不起作用?你没有得到带有数据的垫子吗?什么是 "frame" 是 "frame" 垫子?您的投资回报率是 RotatedRect 吗?因为如果您使用的是不起作用的 RotatedRect,那么您是对的。如果 frame 是一个 Mat 那么你正在做的应该工作得很好。需要一些关于什么不起作用的详细信息。
我明白了。要裁剪 Mat,您必须使用 Image 对象作为缓冲区并使用其 .ROI 属性:
Mat crop_color_frame(Mat input, Rectangle crop_region)
{
/*
* TODO(Ahmed): Figure out why I had to copy this into this class.
* */
Image<Bgr, Byte> buffer_im = input.ToImage<Bgr, Byte>();
buffer_im.ROI = crop_region;
Image<Bgr, Byte> cropped_im = buffer_im.Copy();
return cropped_im.Mat;
}
Ahmed,您真的不必为了创建 Mat 而创建 Image<>。请尝试以下操作:
Mat myNewMat = new Mat(input, crop_region);
由于您没有 RotatedRect,因此您不必将旋转的矩形转换为直立的矩形。因此,您只需从现有的 Mat 和矩形创建一个新的 Mat。
这应该有效,并且可以简化您的代码。
道格