Skip to main content

TensorMath

Namespace

Namespace in the Lens Scripting API.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Enumerations

EnumerationDescription
BorderType-
ThresholdMethod-

Functions

FunctionDescription
addScalarAdds a scalar value to each element of inTensor and puts the result into outTensor.
addTensorsAdds inTensorA to inTensorB and puts the result into outTensor. See the broadcasting rules for elementwise operations.
amplitudeToDbConverts inTensor from the amplitude scale to the decibel scale.
applyBoxFilterBlurs an image tensor using the box filter.
applyNMS-
applyThresholdApplies a fixed-level threshold to each array element.
approximatePolygonalCurveApproximates a polygonal curve with the specified precision.
argMaxReturns the indices of the maximum values along an the channels of inTensor, with the specified inShape. The result is put into outTensor.
argSortSimilar to numpy.argsort(), but in TensorMath, we have no kind and order parameters. Also, tensor is always 3D, and axis can be equal to 0(x), 1(y) or 2(z).
argSortMaskedApplies a list of indices of a tensor in sorted order of their corresponding values in the tensor to the given outTensor. Only indices whose corresponding values are not equal to 0 in the provided mask will be returned. Indices from the original tensor, before applying the mask, will be returned in the provided array. Returns the size of the list of indices applied to the outTensor (note: this will be equal to the number of non-zero values provided in the mask). eg: var inTensor = [5.0, 2.0, 1.0, 3.0, 7.0, -1.0, -5.0]; var mask = [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]; var outTensor = new Uint32Array(7); var order = SortOrder.Ascending; var size = TensorMath.argSortMasked(inTensor, mask, outTensor, order); print(outTensor.subarray(size)); // Expected: [2, 1, 3, 0, 4]
clampClamps all values of inTensor between minVal and maxVal, and puts the result into outTensor.
concatConcatenates inTensorA and inTensorB along the specified axis and puts the result to the outTensor. Input arrays have to have same dimension along other 2 axes.
dilateDilates an image by using a specific structuring element.
divTensorsDivides inTensorA by inTensorB and puts the result into outTensor. See broadcasting rules for elementwise operations.
drawLineDraws a line segment connecting two points.
erodeErodes an image by using a specific structuring element.
fillConvexPolyFills a convex polygon.
fillPolyFills a polygon. Note that you should pass an Array of Int32Array-s (polygonsTensors).
findContoursReturns contours. Note that: contours are sorted from the largest to the smallest; 2) We cannot return Array of TypedArray-s, so all contours data is stored in single outTensor and Array with sizes of each contour is returned.
findMinDistancesBetweenFinds minimum distances between each 2D point from one array, to 2D points in another array.
getContourLooks for contour points in a grayscale texture, or any one-channel tensor.
getRotatedRectPoints-
getVectorsLengthCalculates the length of all vectors in an array. Vectors can be of any dimension count.
isInRectangleChecks for each 2D point whether it is inside of a rectangle.
maxPlaces the maximum values of inTensor into outTensor.
maxDistanceBetweenPointsFrom the input points, finds the two points with the maximum distance between them and returns this distance. Works with points of any dimension count.
maxInSlideWindowIf we consider the tensor as a 3D array, this function finds the indexes of the maximum element in each subarray of size window.
minPlaces the minimum values of inTensor into outTensor.
minAreaRectReturn TensorMath's format of RotatedRect.
minInSlideWindowIf we consider the tensor as a 3D array, this function finds the indexes of the minimum element in each subarray of size window.
mulMatToPointsMultiply a matrix by a set of points through an efficient batch operation.
mulScalarMultiplies each element of inTensor by a scalar value and puts the result into outTensor.
mulTensorsMultiplies inTensorA and inTensorB and puts the result into outTensor. See broadcasting rules for elementwise operations.
opticalFlowStabilizes image objects between two consecutive frames caused by camera or object movement. Results are written to the points array.
permuteRearranges the inTensor of inShape according to the desired ordering and puts the result into outTensor.
polarSort2dSorts 2D points by polar angle relative to the center point.
powerRaises elements of inTensor to the power of val, and puts the results into outTensor.
powerToDbConverts inTensor from the power scale to the decibel scale.
projectPointsProject 3D points into 2D space using an efficient batch operation.
repeatDuplicates inTensor elements and store result in the outTensor.
rotatePoints3dApplies a rotation to each point in a set of 3D points, and places the results in outPoints.
smoothPointsSmooths a polygon formed by input points. Works with points of any dimension count, for example 2 for 2D points or 3 for 3D points. Results are written to outTensor.
softArgMaxApplies softArgMax function to the inTensor, with the specified inShape. The result is put into outTensor.
softMaxApplies softMax function to inTensor, with the specified inShape. The result is put into outTensor.
solvePnPEstimates the object pose given a set of object points (inObjectPoints), their corresponding image projections (inImagePoints), and the intrinsic camera matrix (transform).
solvePnPExtendedEstimates the object pose given a set of object points (inObjectPoints), their corresponding image projections (inImagePoints), and the intrinsic camera matrix (cameraIntrinsicsMatrix). Similar to solvePnP but has additional camera distortion coefficient settings.
solvePnPRansacsolvePnPRansac is very similar to solvePnPExtended except that it uses Random Sample Consensus ( RANSAC ) for robustly estimating the pose.
subpixelArgMaxApplies subpixelArgMax function to the inTensor, with the specified inShape and kernel size. The result is put into outTensor.
subTensorsSubtracts inTensorB from inTensorA and puts the result into outTensor. See the broadcasting rules for elementwise operations.
sumCalculate the sum of the inTensor elements and store result in the outTensor.
textureToGrayscaleConverts the texture to a set of 0-255 grayscale values, and outputs the result into outTensor.