use of org.bytedeco.javacv.CanvasFrame in project javacv by bytedeco.
the class PrincipalComponentAnalysis method execute.
private void execute(String[] args) throws Exception {
// If no params provided, compute the default image
BufferedImage bufferedImage = args.length >= 1 ? ImageIO.read(new File(args[0])) : ImageIO.read(this.getClass().getResourceAsStream("shapes2.jpg"));
System.out.println("Image type: " + bufferedImage.getType());
// Convert BufferedImage to Mat and create AutoCloseable objects
try (Mat matrix = new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(bufferedImage));
Mat mask = new Mat();
Mat gray = new Mat();
Mat denoised = new Mat();
Mat bin = new Mat();
Mat hierarchy = new Mat();
MatVector contours = new MatVector()) {
printMat(matrix);
cvtColor(matrix, gray, COLOR_BGR2GRAY);
// Normalize
GaussianBlur(gray, denoised, new Size(5, 5), 0);
threshold(denoised, mask, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
normalize(gray, gray, 0, 255, NORM_MINMAX, -1, mask);
// Convert image to binary
threshold(gray, bin, 150, 255, THRESH_BINARY);
// Find contours
findContours(bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
long contourCount = contours.size();
System.out.println("Countour count " + contourCount);
for (int i = 0; i < contourCount; ++i) {
// Calculate the area of each contour
Mat contour = contours.get(i);
double area = contourArea(contour);
// Ignore contours that are too small or too large
if (area > 128 && area < 8192) {
principalComponentAnalysis(contour, i, matrix);
}
}
CanvasFrame canvas = new CanvasFrame("PrincipalComponentAnalysis", 1);
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
canvas.setCanvasSize(320, 240);
OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
canvas.showImage(converter.convert(matrix));
}
}
use of org.bytedeco.javacv.CanvasFrame in project javacv by bytedeco.
the class RealSense2DepthMeasuring method main.
public static void main(String[] args) throws FrameGrabber.Exception {
final RealSense2FrameGrabber rs2 = new RealSense2FrameGrabber();
// list all cameras
for (RealSense2FrameGrabber.RealSense2DeviceInfo info : rs2.getDeviceInfos()) {
System.out.printf("Device: %s %s %s Locked: %b\n", info.getName(), info.getFirmware(), info.getSerialNumber(), info.isLocked());
}
// enable the color & depth stream of the realsense camera
rs2.enableColorStream(640, 480, 30);
rs2.enableDepthStream(640, 480, 30);
// here are more examples of streams:
/*
rs2.enableColorStream(640, 480, 30); // color stream
rs2.enableIRStream(640, 480, 90); // ir stream
rs2.enableStream(new RealSense2FrameGrabber.RealSenseStream(
RS2_STREAM_INFRARED,
2,
new Size(640, 480),
30,
RS2_FORMAT_Y8
)); // second ir stream
*/
// start realsense camera
rs2.start();
// start frame to view the stream
CanvasFrame canvasFrame = new CanvasFrame("RealSense");
canvasFrame.setCanvasSize(rs2.getImageWidth(), rs2.getImageHeight());
// add mouse listener to see the depth at the clicked point
canvasFrame.getCanvas().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
System.out.println("Depth: " + rs2.getDistance(e.getX(), e.getY()));
} catch (FrameGrabber.Exception ex) {
ex.printStackTrace();
}
}
});
// run canvas
while (canvasFrame.isVisible()) {
// trigger camera to capture images
rs2.trigger();
// display images -> grab will return the first stream added
// use rs2.grabDepth(), rs2.grabColor() and rs2.grabIR() for the other streams
Frame frame = rs2.grab();
if (frame == null) {
System.err.println("Frame is null!");
break;
}
// display frame
canvasFrame.showImage(frame);
}
// close realsense camera
rs2.stop();
rs2.release();
canvasFrame.dispose();
}
use of org.bytedeco.javacv.CanvasFrame in project javacv by bytedeco.
the class ImageSegmentation method imshow.
// I wrote a custom imshow method for problems using the OpenCV original one
private static void imshow(String txt, Mat img) {
CanvasFrame canvasFrame = new CanvasFrame(txt);
canvasFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
canvasFrame.setCanvasSize(img.cols(), img.rows());
canvasFrame.showImage(new OpenCVFrameConverter.ToMat().convert(img));
}
use of org.bytedeco.javacv.CanvasFrame in project javacv by bytedeco.
the class BioInspiredRetina method showImage.
private void showImage(Mat matrix) {
CanvasFrame canvasFrame = new CanvasFrame("Retina demonstration", 1);
canvasFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
canvasFrame.setCanvasSize(640, 480);
Canvas canvas = canvasFrame.getCanvas();
canvasFrame.getContentPane().removeAll();
ScrollPane scrollPane = new ScrollPane();
scrollPane.add(canvas);
canvasFrame.add(scrollPane);
canvasFrame.showImage(new OpenCVFrameConverter.ToMat().convert(matrix));
}
use of org.bytedeco.javacv.CanvasFrame in project javacv by bytedeco.
the class DeepLearningFaceDetection method main.
public static void main(String[] args) {
VideoCapture capture = new VideoCapture();
capture.set(CAP_PROP_FRAME_WIDTH, 1280);
capture.set(CAP_PROP_FRAME_HEIGHT, 720);
if (!capture.open(0)) {
System.out.println("Can not open the cam !!!");
}
Mat colorimg = new Mat();
CanvasFrame mainframe = new CanvasFrame("Face Detection", CanvasFrame.getDefaultGamma() / 2.2);
mainframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
mainframe.setCanvasSize(600, 600);
mainframe.setLocationRelativeTo(null);
mainframe.setVisible(true);
while (true) {
while (capture.read(colorimg) && mainframe.isVisible()) {
detectAndDraw(colorimg);
mainframe.showImage(converter.convert(colorimg));
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Aggregations