Exemplo n.º 1
0
  @Override
  @SuppressWarnings("unchecked")
  public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) {
    final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get();
    final Mat tmp = ((Optional<Mat>) data).get();
    final boolean externalOnly = ((InputSocket<Boolean>) inputs[1]).getValue().get();

    if (input.empty()) {
      return;
    }

    // findContours modifies its input, so we pass it a temporary copy of the input image
    input.copyTo(tmp);

    // OpenCV has a few different things it can return from findContours, but for now we only use
    // EXTERNAL and LIST.
    // The other ones involve hierarchies of contours, which might be useful in some situations, but
    // probably only
    // when processing the contours manually in code (so, not in a graphical pipeline).
    MatVector contours = new MatVector();
    findContours(
        tmp, contours, externalOnly ? CV_RETR_EXTERNAL : CV_RETR_LIST, CV_CHAIN_APPROX_TC89_KCOS);

    final OutputSocket<ContoursReport> contoursSocket = (OutputSocket<ContoursReport>) outputs[0];
    contoursSocket.setValue(new ContoursReport(contours, input.rows(), input.cols()));
  }
Exemplo n.º 2
0
  @Override
  public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data) {
    final Mat[] dataArray =
        (Mat[]) data.orElseThrow(() -> new IllegalStateException("Data was not provided"));

    final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get();
    final List<Number> channel1 = ((InputSocket<List<Number>>) inputs[1]).getValue().get();
    final List<Number> channel2 = ((InputSocket<List<Number>>) inputs[2]).getValue().get();
    final List<Number> channel3 = ((InputSocket<List<Number>>) inputs[3]).getValue().get();

    final OutputSocket<Mat> outputSocket = (OutputSocket<Mat>) outputs[0];
    final Mat output = outputSocket.getValue().get();

    // Intentionally 1, 3, 2. This maps to the HLS open cv expects
    final Scalar lowScalar =
        new Scalar(
            channel1.get(0).doubleValue(),
            channel3.get(0).doubleValue(),
            channel2.get(0).doubleValue(),
            0);

    final Scalar highScalar =
        new Scalar(
            channel1.get(1).doubleValue(),
            channel3.get(1).doubleValue(),
            channel2.get(1).doubleValue(),
            0);

    final Mat low = reallocateMatIfInputSizeOrWidthChanged(dataArray, 0, lowScalar, input);
    final Mat high = reallocateMatIfInputSizeOrWidthChanged(dataArray, 1, highScalar, input);
    final Mat hls = dataArray[2];

    try {
      cvtColor(input, hls, COLOR_BGR2HLS);
      inRange(hls, low, high, output);
      outputSocket.setValue(output);
    } catch (RuntimeException e) {
      logger.log(Level.WARNING, e.getMessage(), e);
    }
  }