public static void main(String[] args) { try { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat source = Imgcodecs.imread("test_image.jpg", 0); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Imgproc.GaussianBlur(source, source, new Size(45, 45), 0); Imgproc.adaptiveThreshold( source, source, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 10); Core.bitwise_not(source, source); // Line detection Mat img2 = null; Imgproc.cvtColor(source, img2, Imgproc.COLOR_GRAY2RGB); Mat img3 = null; Imgproc.cvtColor(source, img3, Imgproc.COLOR_GRAY2RGB); MatOfInt4 lines = new MatOfInt4(); // Imgproc.HoughLines(img, lines, rho, theta, threshold); // Write to File Imgcodecs.imwrite("gaussian.jpg", source); System.out.println("Success!"); } catch (Exception e) { System.out.println("Error has occurred: " + e.getMessage()); } }
/** Constructor: creates 2 mat objects: 1 for the raw image; other for the processed image */ public CaptureImage() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); capturedFrame = new Mat(); processedFrame = new Mat(); board = new byte[32]; for (int i = 0; i < board.length; i++) { board[i] = 0; } captured = new byte[12]; }
/** 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(); }