public static Image convertMatToImage(Mat m) {
   int type = BufferedImage.TYPE_BYTE_GRAY;
   if (m.channels() > 1) {
     type = BufferedImage.TYPE_3BYTE_BGR;
   }
   int bufferSize = m.channels() * m.cols() * m.rows();
   byte[] b = new byte[bufferSize];
   m.get(0, 0, b);
   BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
   final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
   System.arraycopy(b, 0, targetPixels, 0, b.length);
   return image;
 }
Exemplo n.º 2
0
  /**
   * Helper function to convert a Mat into a BufferedImage. Taken from
   * http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui Author: 'Lucky Luke'
   *
   * @param matrix Mat of type CV_8UC3 or CV_8UC1
   * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
   */
  private static BufferedImage matToBufferedImage(Mat matrix) {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int) matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;

    matrix.get(0, 0, data);

    switch (matrix.channels()) {
      case 1:
        type = BufferedImage.TYPE_BYTE_GRAY;
        break;

      case 3:
        type = BufferedImage.TYPE_3BYTE_BGR;

        // bgr to rgb
        byte b;
        for (int i = 0; i < data.length; i = i + 3) {
          b = data[i];
          data[i] = data[i + 2];
          data[i + 2] = b;
        }
        break;

      default:
        return null;
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);

    return image;
  }
Exemplo n.º 3
0
 private static void printMatI(Mat mat) {
   int[] data = new int[mat.channels()];
   for (int r = 0; r < mat.rows(); r++) {
     for (int c = 0; c < mat.cols(); c++) {
       mat.get(r, c, data);
       log(lvl, "(%d, %d) %s", r, c, Arrays.toString(data));
     }
   }
 }
Exemplo n.º 4
0
 public static BufferedImage bufferedImage(Mat m) {
   int type = BufferedImage.TYPE_BYTE_GRAY;
   if (m.channels() > 1) {
     type = BufferedImage.TYPE_3BYTE_BGR;
   }
   BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
   m.get(
       0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData()); // get all the pixels
   return image;
 }
Exemplo n.º 5
0
  /** Capture images and run color processing through here */
  public void capture() {
    VideoCapture camera = new VideoCapture();

    camera.set(12, -20); // change contrast, might not be necessary

    // CaptureImage image = new CaptureImage();

    camera.open(0); // Useless
    if (!camera.isOpened()) {
      System.out.println("Camera Error");

      // Determine whether to use System.exit(0) or return

    } else {
      System.out.println("Camera OK");
    }

    boolean success = camera.read(capturedFrame);
    if (success) {
      try {
        processWithContours(capturedFrame, processedFrame);
      } catch (Exception e) {
        System.out.println(e);
      }
      // image.processFrame(capturedFrame, processedFrame);
      // processedFrame should be CV_8UC3

      // image.findCaptured(processedFrame);

      // image.determineKings(capturedFrame);

      int bufferSize = processedFrame.channels() * processedFrame.cols() * processedFrame.rows();
      byte[] b = new byte[bufferSize];

      processedFrame.get(0, 0, b); // get all the pixels
      // This might need to be BufferedImage.TYPE_INT_ARGB
      img =
          new BufferedImage(
              processedFrame.cols(), processedFrame.rows(), BufferedImage.TYPE_INT_RGB);
      int width = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_WIDTH);
      int height = (int) camera.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT);
      // img.getRaster().setDataElements(0, 0, width, height, b);
      byte[] a = new byte[bufferSize];
      System.arraycopy(b, 0, a, 0, bufferSize);

      Highgui.imwrite("camera.jpg", processedFrame);
      System.out.println("Success");
    } else System.out.println("Unable to capture image");

    camera.release();
  }
Exemplo n.º 6
0
 Mat imageLike(Mat image) {
   if (image.channels() == 3) return new Mat(image.rows(), image.cols(), CvType.CV_8UC4);
   return new Mat(image.rows(), image.cols(), CvType.CV_8UC3);
 }