forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add
Java2DFrameConverter
and OpenCVFrameConverter
, and use the…
…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
Showing
31 changed files
with
1,191 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
@@ -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()); | ||
|
@@ -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); | ||
// | ||
|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.