Skip to content

Instantly share code, notes, and snippets.

@waldemarnt
Last active August 29, 2015 14:11
Show Gist options
  • Save waldemarnt/ceb3d1adb8e6cc1c7f9e to your computer and use it in GitHub Desktop.
Save waldemarnt/ceb3d1adb8e6cc1c7f9e to your computer and use it in GitHub Desktop.
Javacv(OpenCv) ColoredObjectTrack with the last javacv build
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;
public class ColoredObjectTrack implements Runnable {
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 {
grabber.start();
IplImage img;
int posX = 0;
int posY = 0;
while (true) {
img = grabber.grab();
if (img != null) {
// show image on window
cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
canvas.showImage(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) {
}
}
private void paint(IplImage img, int posX, int posY) {
Graphics g = jp.getGraphics();
path.setSize(img.width(), img.height());
// g.clearRect(0, 0, img.width(), img.height());
g.setColor(Color.RED);
// g.fillOval(posX, posY, 20, 20);
g.drawOval(posX, posY, 20, 20);
System.out.println(posX + " , " + posY);
}
private IplImage getThresholdImage(IplImage orgImg) {
IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
//
cvInRangeS(orgImg, rgba_min, rgba_max, imgThreshold);// red
cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 15,0,0,0);
cvSaveImage(++ii + "dsmthreshold.jpg", imgThreshold);
return imgThreshold;
}
public IplImage Equalize(BufferedImage bufferedimg) {
IplImage iploriginal = IplImage.createFrom(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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment