Exemple #1
0
  public static void main(String args[]) {
    // Load the OpenCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Setup the camera
    VideoCapture camera = new VideoCapture();
    camera.open(0);

    // Create GUI windows to display camera output and OpenCV output
    int width = 320;
    int height = 240;
    camera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, width);
    camera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, height);
    JLabel cameraPane = createWindow("Camera output", width, height);
    JLabel opencvPane = createWindow("OpenCV output", width, height);

    // Main loop
    Mat rawImage = new Mat();
    while (true) {
      // Wait until the camera has a new frame
      while (!camera.read(rawImage)) {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      // Process the image however you like
      Mat binary = Detection.detectHueRange(rawImage);
      org.opencv.core.Point center = Detection.nextCenter(binary, width / 2, height / 2, 5);
      Mat processedImage = Detection.convertC(binary);
      System.out.println(center.x);
      System.out.println(center.y);

      //            Mat lines = Detection.detectEdges(rawImage, 80, 3);
      //            List<org.opencv.core.Point> edges = Detection.findWallEdges(lines, rawImage,
      // 25);
      //            Detection.drawLines(binary, edges);
      //            Mat processedImage = Detection.convertC(binary);
      Mat edges = Detection.contourImage(rawImage, 150, 3);
      Detection.hueEdges(rawImage, edges);
      Mat edgesC = Detection.convertC(edges);
      //            List<org.opencv.core.Point> lines = Detection.hueLines(edges);
      //            Detection.drawLines(binary, lines);
      //            Mat processedImage = Detection.convertC(binary);

      // Update the GUI windows
      updateWindow(cameraPane, rawImage);
      //            updateWindow(opencvPane, processedImage);
      updateWindow(opencvPane, edgesC);
    }
  }
  /** 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();
  }
  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  @Override
  public void run() {
    if (MODE.equals("VIDEO")) {
      Mat capturedImage = new Mat();
      VideoCapture vc = new VideoCapture(DEVICE);
      if (!vc.isOpened()) {
        System.out.println("Capture Failed!");
        return;
      }
      System.out.println("Device " + DEVICE + " opened");
      // set captured resolution
      vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 640);
      vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 480);

      // Manually set exposure
      vc.set(15, -11);
      while (true) {
        vc.read(capturedImage);
        if (capturedImage != null) {
          // flip the image to compensate for camera orientation
          Core.flip(capturedImage, capturedImage, -1);
          capturedImage.copyTo(finalDisplayImg);
          parseImage(capturedImage);
        }
      }
    } else { // STILL IMAGE
      Mat capturedImage = Highgui.imread(IMAGE_FILEPATH);
      while (true) {
        if (needUpdate) {
          capturedImage.copyTo(finalDisplayImg);
          parseImage(capturedImage);
          needUpdate = false;
        }
      }
    }
  }