1

I have ROI of the image and I need to create new image that would be subpart of image. How can I do that? (I want to make pieces an array of images, not rectangles.)

Image<Bgr, byte> img = frame.Copy();
pieces = new List<System.Drawing.Rectangle>();

int input_cell_width = img.Width / Cols;
int input_cell_height = img.Height / Rows;

System.Drawing.Rectangle old_roi = img.ROI;

for (int row = 0; row < Rows; ++row)
{
    for (int col = 0; col < Cols; ++col)
    {
        // Get template
        img.ROI = gridOuput.GetCellItemRect(row, col, new System.Drawing.Point(0, 0));
        pieces.Add(img.ROI);
    } 
}

Thanks.

berak
  • 39,159
  • 9
  • 91
  • 89
user2558053
  • 435
  • 2
  • 6
  • 12

1 Answers1

5

First set the image's ROI (as you already do) and then use the Copy() member method of Image. As long as an image's ROI is set, Copy() will only copy that subpart of the image. See the example below.

var rois = new List<Rectangle>(); // List of rois
var imageparts = new List<Image<Gray,byte>>(); // List of extracted image parts

using (var image = new Image<Gray, byte>(...))
{
    foreach (var roi in rois)
    {
        image.ROI = roi;
        imageparts.Add(image.Copy());   
    }
}
Anders
  • 580
  • 5
  • 19