org.bytedeco.javacpp.indexer.FloatIndexer Java Examples

The following examples show how to use org.bytedeco.javacpp.indexer.FloatIndexer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PerspectiveTransformRunner.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
private Mat getPerspectiveTransform(Mat sourceMat, Mat targetMat, int refWidth, int refHeight) {
    Mat initialTransform = opencv_imgproc.getPerspectiveTransform(sourceMat, targetMat);
    if(refWidth == -1 || refHeight == -1) { return initialTransform; }

    // Calculate where edges will end up in this case
    double[] extremes = calculateExtremes(initialTransform, refWidth, refHeight);

    FloatIndexer tIdx = targetMat.createIndexer();
    long rows = tIdx.size(0);
    for (long i = 0; i < rows; i++) {
        tIdx.put(i, 0, (float)(tIdx.get(i, 0) - extremes[0]));
        tIdx.put(i, 1, (float)(tIdx.get(i, 1) - extremes[1]));
    }

    return opencv_imgproc.getPerspectiveTransform(sourceMat, targetMat);
}
 
Example #2
Source File: ProjectiveDeviceP.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void initNativeIntrinsic() {
    if (intrinsicsMat == null) {
        intrinsicsMat = new Mat(3, 3, CV_32FC1);
        FloatIndexer intrinsicIdx = intrinsicsMat.createIndexer(true);

        // init to 0
        int k = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                intrinsicIdx.put(k++, 0);
            }
        }

        // set the values
        intrinsicIdx.put(0, 0, intrinsics.m00);
        intrinsicIdx.put(1, 1, intrinsics.m11);
        intrinsicIdx.put(0, 2, intrinsics.m02);
        intrinsicIdx.put(1, 2, intrinsics.m12);
        intrinsicIdx.put(2, 2, 1);
    }
}
 
Example #3
Source File: ProjectiveDeviceP.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void fillNative(PVector[] objectPoints,
        PVector[] imagePoints,
        Mat op, Mat ip) {

    FloatIndexer opIdx = op.createIndexer();
    FloatIndexer ipIdx = ip.createIndexer();

    // Fill the object and image matrices.
    for (int i = 0; i < objectPoints.length; i++) {
        opIdx.put(i, 0, objectPoints[i].x);
        opIdx.put(i, 1, objectPoints[i].y);
        opIdx.put(i, 2, objectPoints[i].z);

        ipIdx.put(i, 0, imagePoints[i].x);
        ipIdx.put(i, 1, imagePoints[i].y);
    }
}
 
Example #4
Source File: PerspectiveTransformRunner.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private Mat pointsToMat(List<Point> points){
    int rows = points.size();
    int cols = points.get(1).dimensions();
    Mat mat = new Mat(rows, cols, CvType.CV_32F);
    FloatIndexer idx = mat.createIndexer();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            idx.put(i, j, (float)points.get(i).get(j));
        }
    }
    return mat;
}
 
Example #5
Source File: OnnxTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test
public void runSqueezenet(TestContext testContext) throws Exception {

    long inputTensorSize = 224 * 224 * 3;

    FloatPointer inputTensorValues = new FloatPointer(inputTensorSize);
    FloatIndexer idx = FloatIndexer.create(inputTensorValues);
    for (long i = 0; i < inputTensorSize; i++)
        idx.put(i, (float) i / (inputTensorSize + 1));

    DataBuffer buffer = Nd4j.createBuffer(inputTensorValues, DataType.FLOAT, inputTensorSize, idx);

    INDArray contents = Nd4j.create(buffer);

    byte[] npyContents = Nd4j.toNpyByteArray(contents);

    File inputFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(inputFile, npyContents);

    for (int i = 0; i < 5; i++) {
        Response response = given().port(port)
                .multiPart("data_0", inputFile)
                .post("nd4j/numpy")
                .andReturn();
        //TODO: report memory leak in DNNL execution provider to ORT
        assertEquals("Response failed", 200, response.getStatusCode());

        INDArray bodyResult = Nd4j.createNpyFromByteArray(response.getBody().asByteArray());

        assertEquals(1.99018, bodyResult.getFloat(0), 1e-4);

        assertArrayEquals(new long[]{1, 1000}, bodyResult.shape());
    }
}
 
Example #6
Source File: LaneManagerTest.java    From android-robocar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testGetMaxPosition() {
  opencv_core.Mat src = LaneManager.readImage(getResourcePath(TEST_IMAGE));
  opencv_core.Mat saturationChannel = LaneManager.getSaturationChannel(src);
  opencv_core.Mat histogram = LaneManager.histogram(saturationChannel);

  // Compute manual
  float max = -1.0f;
  int maxIndex = -1;
  FloatIndexer idx = histogram.createIndexer();
  for (int row = 0; row < histogram.rows(); row++) {
    for (int col = 0; col < histogram.cols(); col++) {
      float value = idx.get(row, col);
      //System.out.println(String.format(Locale.US, "row: %d, col: %d, value: %f", row, col, value));
      if (max == -1 || value > max) {
        maxIndex = row;
        max = value;
      }
    }
  }

  // Compute with OpenCV
  HistogramPosition position = LaneManager.getMaxPosition(histogram);

  // Both results should match
  assertEquals(position.getBinIndex(), 21);
  assertEquals(position.getBinValue(), 36377.0, DELTA);
  assertEquals(position.getBinIndex(), maxIndex);
  assertEquals(position.getBinValue(), max, DELTA);
}
 
Example #7
Source File: CoreFunc.java    From vlpr4j with Apache License 2.0 5 votes vote down vote up
/**
 * Assign values to feature
 * <p>
 * 样本特征为水平、垂直直方图和低分辨率图像所组成的矢量
 * 
 * @param in
 * @param sizeData - 低分辨率图像size = sizeData*sizeData, 可以为0
 * @return
 */
public static Mat features(final Mat in, final int sizeData) {
    float[] vhist = projectedHistogram(in, Direction.VERTICAL);
    float[] hhist = projectedHistogram(in, Direction.HORIZONTAL);
    Mat lowData = new Mat();
    if (sizeData > 0) {
        resize(in, lowData, new Size(sizeData, sizeData));
    }

    int numCols = vhist.length + hhist.length + lowData.cols() * lowData.rows();
    Mat out = Mat.zeros(1, numCols, CV_32F).asMat();
    FloatIndexer idx = out.createIndexer();

    int j = 0;
    for (int i = 0; i < vhist.length; ++i, ++j) {
        idx.put(0, j, vhist[i]);
    }
    for (int i = 0; i < hhist.length; ++i, ++j) {
        idx.put(0, j, hhist[i]);
    }
    for (int x = 0; x < lowData.cols(); x++) {
        for (int y = 0; y < lowData.rows(); y++, ++j) {
            float val = lowData.ptr(x, y).get() & 0xFF;
            idx.put(0, j, val);
        }
    }

    return out;
}
 
Example #8
Source File: CoreFunc.java    From EasyPR-Java with Apache License 2.0 5 votes vote down vote up
/**
 * Assign values to feature
 * <p>
 * 样本特征为水平、垂直直方图和低分辨率图像所组成的矢量
 * 
 * @param in
 * @param sizeData
 *            低分辨率图像size = sizeData*sizeData, 可以为0
 * @return
 */
public static Mat features(final Mat in, final int sizeData) {

    float[] vhist = projectedHistogram(in, Direction.VERTICAL);
    float[] hhist = projectedHistogram(in, Direction.HORIZONTAL);

    Mat lowData = new Mat();
    if (sizeData > 0) {
        resize(in, lowData, new Size(sizeData, sizeData));
    }

    int numCols = vhist.length + hhist.length + lowData.cols() * lowData.rows();
    Mat out = Mat.zeros(1, numCols, CV_32F).asMat();
    FloatIndexer idx = out.createIndexer();

    int j = 0;
    for (int i = 0; i < vhist.length; ++i, ++j) {
        idx.put(0, j, vhist[i]);
    }
    for (int i = 0; i < hhist.length; ++i, ++j) {
        idx.put(0, j, hhist[i]);
    }
    for (int x = 0; x < lowData.cols(); x++) {
        for (int y = 0; y < lowData.rows(); y++, ++j) {
            float val = lowData.ptr(x, y).get() & 0xFF;
            idx.put(0, j, val);
        }
    }

    return out;
}
 
Example #9
Source File: FloatDataBufferTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPointerCreation() {
    FloatPointer floatPointer = new FloatPointer(1, 2, 3, 4);
    Indexer indexer = FloatIndexer.create(floatPointer);
    DataBuffer buffer = Nd4j.createBuffer(floatPointer, DataBuffer.Type.FLOAT, 4, indexer);
    DataBuffer other = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertArrayEquals(other.asFloat(), buffer.asFloat(), 0.001f);
}
 
Example #10
Source File: DefaultDataBufferFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * @param floatPointer
 * @param length
 * @return
 */
@Override
public DataBuffer create(FloatPointer floatPointer, long length) {
    floatPointer.capacity(length);
    floatPointer.limit(length);
    floatPointer.position(0);
    return new FloatBuffer(floatPointer, FloatIndexer.create(floatPointer), length);
}
 
Example #11
Source File: DefaultDataBufferFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @param floatPointer
 * @param length
 * @return
 */
@Override
public DataBuffer create(FloatPointer floatPointer, long length) {
    floatPointer.capacity(length);
    floatPointer.limit(length);
    floatPointer.position(0);
    return new FloatBuffer(floatPointer, FloatIndexer.create(floatPointer), length);
}
 
Example #12
Source File: FloatDataBufferTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPointerCreation() {
    FloatPointer floatPointer = new FloatPointer(1, 2, 3, 4);
    Indexer indexer = FloatIndexer.create(floatPointer);
    DataBuffer buffer = Nd4j.createBuffer(floatPointer, DataType.FLOAT, 4, indexer);
    DataBuffer other = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertArrayEquals(other.asFloat(), buffer.asFloat(), 0.001f);
}
 
Example #13
Source File: OnnxMultipleInputsTest.java    From konduit-serving with Apache License 2.0 3 votes vote down vote up
@Test
public void runAdd(TestContext testContext) throws Exception {

    long inputTensorSize = 3 * 4 * 5;

    FloatPointer inputTensorValues = new FloatPointer(inputTensorSize);
    FloatIndexer idx = FloatIndexer.create(inputTensorValues);
    for (long i = 0; i < inputTensorSize; i++)
        idx.put(i, (float) i / (inputTensorSize + 1));

    DataBuffer buffer = Nd4j.createBuffer(inputTensorValues, DataType.FLOAT, inputTensorSize, idx);

    INDArray contents = Nd4j.create(buffer);

    byte[] npyContents = Nd4j.toNpyByteArray(contents);

    File inputFile = temporary.newFile();
    FileUtils.writeByteArrayToFile(inputFile, npyContents);

    Response response = given().port(port)
            .multiPart("x", inputFile)
            .multiPart("y", inputFile)
            .post("nd4j/numpy")
            .andReturn();

    assertEquals("Response failed", 200, response.getStatusCode());

    INDArray bodyResult = Nd4j.createNpyFromByteArray(response.getBody().asByteArray());

    assertEquals(0.032786883, bodyResult.getFloat(1), 1e-6);

    assertArrayEquals(new long[]{1, 60}, bodyResult.shape());
}