我使用以下代码移动图像:
Mat img = Cv2.ImRead(@"C:\building.jpg", ImreadModes.GrayScale);
Mat imgout = new Mat();
Point2f[] src = new Point2f[3];
src[1] = new Point2f(0, 0);
src[1] = new Point2f(img.Width-1, 0);
src[2] = new Point2f(0, img.Height-1);
Point2f[] dst = new Point2f[3];
dst[0] = new Point2f(-100, 0);
dst[1] = new Point2f((img.Width - 1)-100, 0);
dst[2] = new Point2f(-100, img.Height - 1);
var m = Cv2.GetAffineTransform(src, dst);
Cv2.WarpAffine(img, imgout, m, img.Size());
我知道你可以让它变得更容易。怎么样?
答案 0 :(得分:1)
仿射变换操作相当复杂。可以通过将子矩阵(roi)复制到与原始尺寸相同的新矩阵来完成简单移位。
Shift Width和Height可以设置为函数的参数。所有操作都可以在三行中完成(读取图像,创建新垫子,将submat复制到新垫子),但将rois定义为单独的操作可使代码更清晰。
int shiftWidth = -100;
int shiftHeight = -100;
Mat img = Cv2.ImRead(@"C:\building.jpg", ImreadModes.GrayScale);
Mat imgout = new Mat(img.Size(), img.Type());
OpenCvSharp.Rect sourceRoi = new OpenCvSharp.Rect(Math.Max(-shiftWidth, 0), Math.Max(-shiftHeight, 0), img.Width - Math.Abs(shiftWidth), img.Height - Math.Abs(shiftHeight));
OpenCvSharp.Rect destRoi = new OpenCvSharp.Rect(Math.Max(shiftWidth, 0), Math.Max(shiftHeight, 0), img.Width - Math.Abs(shiftWidth), img.Height - Math.Abs(shiftHeight));
img.SubMat(sourceRoi).CopyTo(imgout.SubMat(destRoi));