Skip to content

Commit

Permalink
* Add Java2DFrameConverter and OpenCVFrameConverter, and use the…
Browse files Browse the repository at this point in the history
…m to refactor `Frame`, `CanvasFrame`, `FrameGrabber`, and `FrameRecorder` in a way to help users reduce coupling with Java 2D or OpenCV (issue bytedeco#84)
  • Loading branch information
saudet committed Mar 22, 2015
1 parent 858d6c4 commit 7ee75f8
Show file tree
Hide file tree
Showing 31 changed files with 1,191 additions and 235 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

* Add `Java2DFrameConverter` and `OpenCVFrameConverter`, and use them to refactor `Frame`, `CanvasFrame`, `FrameGrabber`, and `FrameRecorder` in a way to help users reduce coupling with Java 2D or OpenCV ([issue #84](https://github.com/bytedeco/javacv/issues/84))
* Fix `Demo` class in the `README.md` file ([issue #102](https://github.com/bytedeco/javacv/issues/102))
* Add new `ColoredObjectTrack` sample ([pull #99](https://github.com/bytedeco/javacv/pull/99))
* Add `option` property to `FFmpegFrameGrabber` to let users set such things as "analyzeduration", "probesize", or "list_devices"
* Fix "AVFrame.format is not set" and "AVFrame.width or height is not set" warning messages ([issue #76](https://github.com/bytedeco/javacv/issues/76))

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.10</version>
<version>0.11-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JavaCV</name>
Expand Down Expand Up @@ -73,12 +73,12 @@
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.10-${javacpp.version}</version>
<version>2.4.11-${javacpp.version}</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg</artifactId>
<version>2.5.1-${javacpp.version}</version>
<version>2.5.4-${javacpp.version}</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
Expand Down
4 changes: 3 additions & 1 deletion samples/BlobDemo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.bytedeco.javacv.Blobs;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.OpenCVFrameConverter;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
Expand Down Expand Up @@ -118,7 +119,8 @@ public static void ShowImage(IplImage image, String caption, int width, int heig
CanvasFrame canvas = new CanvasFrame(caption, 1); // gamma=1
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
canvas.setCanvasSize(width, height);
canvas.showImage(image);
OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
canvas.showImage(converter.convert(image));
}

public static void Highlight(IplImage image, int [] inVec)
Expand Down
72 changes: 29 additions & 43 deletions samples/ColoredObjectTrack.java
Original file line number Diff line number Diff line change
@@ -1,84 +1,68 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* Just an example using the opencv to make a colored object tracking,
* i adpted this code to bytedeco/javacv, i think this will help some people.
*
* Waldemar <[email protected]>
*/
package javacv;

import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
import static org.bytedeco.javacpp.opencv_core.cvCreateImage;
import static org.bytedeco.javacpp.opencv_core.cvFlip;
import static org.bytedeco.javacpp.opencv_core.cvGetSize;
import static org.bytedeco.javacpp.opencv_core.cvInRangeS;
import static org.bytedeco.javacpp.opencv_core.cvScalar;
import static org.bytedeco.javacpp.opencv_highgui.cvSaveImage;
import static org.bytedeco.javacpp.opencv_imgproc.CV_BGR2GRAY;
import static org.bytedeco.javacpp.opencv_imgproc.CV_MEDIAN;
import static org.bytedeco.javacpp.opencv_imgproc.cvCvtColor;
import static org.bytedeco.javacpp.opencv_imgproc.cvEqualizeHist;
import static org.bytedeco.javacpp.opencv_imgproc.cvGetCentralMoment;
import static org.bytedeco.javacpp.opencv_imgproc.cvGetSpatialMoment;
import static org.bytedeco.javacpp.opencv_imgproc.cvMoments;
import static org.bytedeco.javacpp.opencv_imgproc.cvSmooth;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacpp.opencv_imgproc.CvMoments;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.VideoInputFrameGrabber;

import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;

public class ColoredObjectTrack implements Runnable {

public static void main(String[] args) {
public static void main(String[] args) {
ColoredObjectTrack cot = new ColoredObjectTrack();
Thread th = new Thread(cot);
th.start();
}



final int INTERVAL = 10;// 1sec
final int CAMERA_NUM = 0; // Default camera for this time

/**
* Correct the color range- it depends upon the object, camera quality,
* environment.
*/
static CvScalar rgba_min = cvScalar(0, 0, 130, 0);// RED wide dabur birko
static CvScalar rgba_max = cvScalar(80, 80, 255, 0);

IplImage image;
CanvasFrame canvas = new CanvasFrame("Web Cam Live");
CanvasFrame path = new CanvasFrame("Detection");
int ii = 0;
JPanel jp = new JPanel();

public ColoredObjectTrack() {
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
path.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
path.setContentPane(jp);
}

@Override
public void run() {
FrameGrabber grabber = new VideoInputFrameGrabber(CAMERA_NUM);
try {
FrameGrabber grabber = FrameGrabber.createDefault(CAMERA_NUM);
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
grabber.start();
IplImage img;
int posX = 0;
int posY = 0;
while (true) {
img = grabber.grab();
img = converter.convert(grabber.grab());
if (img != null) {
// show image on window
cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
canvas.showImage(img);
canvas.showImage(converter.convert(img));
IplImage detectThrs = getThresholdImage(img);

CvMoments moments = new CvMoments();
Expand All @@ -98,7 +82,7 @@ public void run() {
} catch (Exception e) {
}
}

private void paint(IplImage img, int posX, int posY) {
Graphics g = jp.getGraphics();
path.setSize(img.width(), img.height());
Expand All @@ -109,7 +93,7 @@ private void paint(IplImage img, int posX, int posY) {
System.out.println(posX + " , " + posY);

}

private IplImage getThresholdImage(IplImage orgImg) {
IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
//
Expand All @@ -119,14 +103,16 @@ private IplImage getThresholdImage(IplImage orgImg) {
cvSaveImage(++ii + "dsmthreshold.jpg", imgThreshold);
return imgThreshold;
}


public IplImage Equalize(BufferedImage bufferedimg) {
IplImage iploriginal = IplImage.createFrom(bufferedimg);
Java2DFrameConverter converter1 = new Java2DFrameConverter();
OpenCVFrameConverter.ToIplImage converter2 = new OpenCVFrameConverter.ToIplImage();
IplImage iploriginal = converter2.convert(converter1.convert(bufferedimg));
IplImage srcimg = IplImage.create(iploriginal.width(), iploriginal.height(), IPL_DEPTH_8U, 1);
IplImage destimg = IplImage.create(iploriginal.width(), iploriginal.height(), IPL_DEPTH_8U, 1);
cvCvtColor(iploriginal, srcimg, CV_BGR2GRAY);
cvEqualizeHist(srcimg, destimg);
return destimg;
}
}
}
17 changes: 11 additions & 6 deletions samples/FaceApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import java.io.File;
import java.io.IOException;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.opencv_objdetect;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.bytedeco.javacpp.opencv_objdetect;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_objdetect.*;
import static org.bytedeco.javacpp.opencv_highgui.*;

/**
*
Expand All @@ -29,6 +31,8 @@ public class FaceApplet extends Applet implements Runnable {
private CvSeq faces = null;
private boolean stop = false;
private Exception exception = null;
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
Java2DFrameConverter paintConverter = new Java2DFrameConverter();

@Override public void init() {
try {
Expand Down Expand Up @@ -74,19 +78,19 @@ public void run() {
grabber.setImageWidth(getWidth());
grabber.setImageHeight(getHeight());
grabber.start();
grabbedImage = grabber.grab();
grabbedImage = grabberConverter.convert(grabber.grab());
} catch (Exception e) {
if (grabber != null) grabber.release();
grabber = new OpenCVFrameGrabber(0);
grabber.setImageWidth(getWidth());
grabber.setImageHeight(getHeight());
grabber.start();
grabbedImage = grabber.grab();
grabbedImage = grabberConverter.convert(grabber.grab());
}
grayImage = IplImage.create(grabbedImage.width(), grabbedImage.height(), IPL_DEPTH_8U, 1);
smallImage = IplImage.create(grabbedImage.width()/4, grabbedImage.height()/4, IPL_DEPTH_8U, 1);
stop = false;
while (!stop && (grabbedImage = grabber.grab()) != null) {
while (!stop && (grabbedImage = grabberConverter.convert(grabber.grab())) != null) {
if (faces == null) {
cvClearMemStorage(storage);
cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
Expand All @@ -113,7 +117,8 @@ public void run() {

@Override public void paint(Graphics g) {
if (grabbedImage != null) {
BufferedImage image = grabbedImage.getBufferedImage(2.2/grabber.getGamma());
Frame frame = grabberConverter.convert(grabbedImage);
BufferedImage image = paintConverter.getBufferedImage(frame, 2.2/grabber.getGamma());
Graphics2D g2 = image.createGraphics();
if (faces != null) {
g2.setColor(Color.RED);
Expand Down
8 changes: 5 additions & 3 deletions samples/HoughLines.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import javax.swing.JFrame;
import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
Expand Down Expand Up @@ -28,6 +28,8 @@ public static void main(String[] args) {

CanvasFrame source = new CanvasFrame("Source");
CanvasFrame hough = new CanvasFrame("Hough");
OpenCVFrameConverter.ToIplImage sourceConverter = new OpenCVFrameConverter.ToIplImage();
OpenCVFrameConverter.ToIplImage houghConverter = new OpenCVFrameConverter.ToIplImage();
if (src == null) {
System.out.println("Couldn't load source image.");
return;
Expand Down Expand Up @@ -106,8 +108,8 @@ else if(args.length==2 && args[1].contentEquals("multiscale")){
cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
}
}
source.showImage(src);
hough.showImage(colorDst);
source.showImage(sourceConverter.convert(src));
hough.showImage(houghConverter.convert(colorDst));

source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down
11 changes: 5 additions & 6 deletions samples/MotionDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
* Angelo Marchesin <[email protected]>
*/

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacv.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_calib3d.*;
import static org.bytedeco.javacpp.opencv_objdetect.*;

public class MotionDetector {
public static void main(String[] args) throws Exception {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
grabber.start();

IplImage frame = grabber.grab();
IplImage frame = converter.convert(grabber.grab());
IplImage image = null;
IplImage prevImage = null;
IplImage diff = null;
Expand All @@ -32,7 +31,7 @@ public static void main(String[] args) throws Exception {

CvMemStorage storage = CvMemStorage.create();

while (canvasFrame.isVisible() && (frame = grabber.grab()) != null) {
while (canvasFrame.isVisible() && (frame = converter.convert(grabber.grab())) != null) {
cvClearMemStorage(storage);

cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2);
Expand All @@ -56,7 +55,7 @@ public static void main(String[] args) throws Exception {
// do some threshold for wipe away useless details
cvThreshold(diff, diff, 64, 255, CV_THRESH_BINARY);

canvasFrame.showImage(diff);
canvasFrame.showImage(converter.convert(diff));

// recognize contours
CvSeq contour = new CvSeq(null);
Expand Down
1 change: 0 additions & 1 deletion samples/OpenCVFaceRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
* Source: http://pcbje.com/2012/12/doing-face-recognition-with-javacv/
*
* @author Petter Christian Bjelland
* @author Samuel Audet
*/
public class OpenCVFaceRecognizer {
public static void main(String[] args) {
Expand Down
1 change: 0 additions & 1 deletion samples/OpticalFlowDense.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* (Frame-1 & Frame-2) and put the velocity of every pixel to another image (OF) in their coordinate.
*
* @author Dawit Gebreyohannes
* @author Samuel Audet
*/
public class OpticalFlowDense {
public static void main(String[] args) {
Expand Down
7 changes: 5 additions & 2 deletions samples/RLSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import java.io.File;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
Expand All @@ -11,7 +13,6 @@
* http://stackoverflow.com/questions/21554431/implementation-run-length-smoothing-algorithm-in-c
*
* @author Nicholas Woodward
* @author Samuel Audet
*/
public class RLSA {

Expand All @@ -20,8 +21,10 @@ public static void main(String[] args) {

IplImage image = null;
try {
Java2DFrameConverter converter1 = new Java2DFrameConverter();
OpenCVFrameConverter.ToIplImage converter2 = new OpenCVFrameConverter.ToIplImage();
BufferedImage img = ImageIO.read(new File(imagePath));
image = IplImage.createFrom(img);
image = converter2.convert(converter1.convert(img));
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
Loading

0 comments on commit 7ee75f8

Please sign in to comment.