jahu
jahu

Reputation: 5657

Copy image to ROI

I'm trying to copy one image to another and I use CopyTo to achieve it, except instead of the source image being put in the ROI of the target, my target image gets replaced entirely.

Here is what my code looks like:

var output = new Mat(size, size, MatType.CV_8UC3, background);
var temp1 = image.Resize(new OpenCvSharp.Size(targetWidth, targetHeight), 0, 0, interpolation);
xOffset = Convert.ToInt32((size - targetWidth) / 2);
yOffset = Convert.ToInt32((size - targetHeight) / 2);
output.AdjustROI(yOffset, (size - 1) - yOffset, xOffset, (size - 1) - xOffset);
temp1.CopyTo(output);
output.AdjustROI(0, size - 1, 0, size - 1);

I found that in OpenCV this is now achieved by using () operator with ROI, however I cannot find this operator in OpenCVSharp and I have no clue what it's equivalent might be named like.

Upvotes: 0

Views: 4035

Answers (1)

jahu
jahu

Reputation: 5657

I realized that this might be achieved using Mat's constructor in OpenCVSharp and the following code seems to do the trick:

var output = new Mat(size, size, MatType.CV_8UC3, background);
var temp1 = image.Resize(new OpenCvSharp.Size(targetWidth, targetHeight), 0, 0, interpolation);
xOffset = Convert.ToInt32((size - targetWidth) / 2);
yOffset = Convert.ToInt32((size - targetHeight) / 2);
var roi = new Mat(output, new Rect(xOffset, yOffset, targetWidth, targetHeight));
temp1.CopyTo(roi);

Upvotes: 1

Related Questions