c# - Shift image. OpenCvSharp -
i use following code shift image:
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()); i know can make easier. how?
affine transform rather complicated operation. simple shift can done copy of submatrix (roi) new matrix of same size original.
shift width , height can set parameters of function. operations can done in 3 lines (read image, create new mat, copy of submat new mat), defining rois separate operations makes code clearer.
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));
Comments
Post a Comment