mark-p4 commented on code in PR #2057: URL: https://github.com/apache/systemds/pull/2057#discussion_r1697028003
########## src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixIMGTransform.java: ########## @@ -0,0 +1,232 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysds.runtime.matrix.data; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.sysds.runtime.functionobjects.Builtin; +import org.apache.sysds.runtime.functionobjects.CTable; +import org.apache.sysds.runtime.functionobjects.SwapIndex; +import org.apache.sysds.runtime.instructions.InstructionUtils; +import org.apache.sysds.runtime.matrix.operators.*; +import org.apache.sysds.runtime.util.DataConverter; + +import static org.apache.commons.math3.util.FastMath.floor; + +/**Separate class for generating the transformation matrix based on an affine transformation matrix and a matrix + * containing the dimensions of the input and the target dimensions of the output + */ +public class LibMatrixIMGTransform { + + protected static final Log LOG = LogFactory.getLog(LibMatrixIMGTransform.class.getName()); + + +// * affine transformation matrix for calculated for a picture of original size and target dimensions +// * see: https://en.wikipedia.org/wiki/Affine_transformation +// * +// * #orig_w = as.scalar(dimMat[1,2]) +// * #orig_h = as.scalar(dimMat[1,1]) +// * #out_w = as.scalar(dimMat[2,2]) +// * #out_h = as.scalar(dimMat[2,1]) +// * T_inv = inv(transMat) +// * +// * ## coordinates of output pixel-centers linearized in row-major order +// * coords = matrix(1, rows=3, cols=out_w*out_h) +// * coords[1,] = t((seq(0, out_w*out_h-1) %% out_w) + 0.5) +// * coords[2,] = t((seq(0, out_w*out_h-1) %/% out_w) + 0.5) +// * # compute sampling pixel indices +// * coords = floor(T_inv %*% coords) + 1 +// * inx = t(coords[1,]) +// * iny = t(coords[2,]) +// * # any out-of-range pixels, if present, correspond to an extra pixel with fill_value at the end of the input +// * index_vector = (orig_w *(iny-1) + inx) * ((0<inx) & (inx<=orig_w) & (0<iny) & (iny<=orig_h)) +// * index_vector = t(index_vector) +// * xs = ((index_vector == 0)*(orig_w*orig_h +1)) + index_vector +// * #if(min(index_vector) == 0){ +// * # ys=cbind(img_in, matrix(fill_value,nrow(img_in), 1)) +// * #}else{ +// * # ys = img_in +// * #} +// * ind= matrix(seq(1,ncol(xs),1),1,ncol(xs)) +// * z = table(xs, ind) +// * zMat = transMat +// * isFillable = as.double(min(index_vector) == 0) + /** This method produces a transformation matrix for affine transformation (see Wikipedia: "Affine transformation"). + * It takes a transformation 3x3 matrix (last row is always 0,0,1 for images), called affine matrix, and a dimension matrix, where the + * original image dimensions (width x height) are stored alongside the output image dimensions (width x height) + * @param transMat affine 3x3 matrix for image transformations + * @param dimMat 2x2 matrix with original and output image dimensions + * @param threads number of threads for use in different methods + * + * @return array of two matrix blocks, 1st is the transformation matrix, 2nd a 1x1 matrix with 1 or 0 + */ + public static MatrixBlock[] transformationMatrix(MatrixBlock transMat, MatrixBlock dimMat, int threads) { + //check the correctness of the input dimension matrix + isValidDimensionMatrix(dimMat); + + int orig_w = (int) dimMat.get(0,0); + int orig_h = (int) dimMat.get(0,1); + int out_w = (int) dimMat.get(1,0); + int out_h = (int) dimMat.get(1,1); + + //calculate the inverse of the transformation matrix + MatrixBlock t_Inv = LibCommonsMath.unaryOperations(transMat, "inverse"); + + //create the coords matrix: coords = matrix(1, rows=3, cols=out_w*out_h) + MatrixBlock coords = new MatrixBlock(3, out_w*out_h, false); + //change values to coords[1, ] = t((seq(0, out_w * out_h - 1) %% out_w) + 0.5) + //coords[2, ] = t((seq(0, out_w * out_h - 1) %/% out_w) + 0.5) + double [] coords1 = new double[out_w*out_h]; + double [] coords2 = new double[out_w*out_h]; + double [] coords3 = new double[out_w*out_h]; Review Comment: i gave it some thought and did not come up with a better way to implement this. I could initialize different Matrix Blocks, but ultimately i am not certain if this would lead to improved performance, as the two first double[] are needed anyway. Also the this line in coords is needed in the following matrix mult with the affine matrix, as: 3x3 * 3xN. I would gladly take advice -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@systemds.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org