use of org.bytedeco.javacv.FrameGrabber in project javacv by bytedeco.
the class ColoredObjectTrack method run.
@Override
public void run() {
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 = converter.convert(grabber.grab());
if (img != null) {
// show image on window
// l-r = 90_degrees_steps_anti_clockwise
cvFlip(img, img, 1);
canvas.showImage(converter.convert(img));
IplImage detectThrs = getThresholdImage(img);
CvMoments moments = new CvMoments();
cvMoments(detectThrs, moments, 1);
double mom10 = cvGetSpatialMoment(moments, 1, 0);
double mom01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
posX = (int) (mom10 / area);
posY = (int) (mom01 / area);
// only if its a valid position
if (posX > 0 && posY > 0) {
paint(img, posX, posY);
}
}
// Thread.sleep(INTERVAL);
}
} catch (Exception e) {
}
}
Aggregations