2012-03-23 3 views
2
import com.googlecode.javacpp.Loader; 
import com.googlecode.javacv.*; 
import com.googlecode.javacv.cpp.*; 
import static com.googlecode.javacv.cpp.opencv_core.*; 
import static com.googlecode.javacv.cpp.opencv_imgproc.*; 
import static com.googlecode.javacv.cpp.opencv_calib3d.*; 
import static com.googlecode.javacv.cpp.opencv_objdetect.*; 

public class Demo { 
    public static void main(String[] args) throws Exception { 
     String classifierName = null; 
     if (args.length > 0) { 
      classifierName = args[0]; 
     } else { 
      System.err.println("Please provide the path to \"haarcascade_frontalface_alt.xml\"."); 
      System.exit(1); 
    } 

    // Preload the opencv_objdetect module to work around a known bug. 
    Loader.load(opencv_objdetect.class); 

    // We can "cast" Pointer objects by instantiating a new object of the desired class. 
    CvHaarClassifierCascade classifier = new CvHaarClassifierCascade(cvLoad(classifierName)); 
    if (classifier.isNull()) { 
     System.err.println("Error loading classifier file \"" + classifierName + "\"."); 
     System.exit(1); 
    } 

    // CanvasFrame is a JFrame containing a Canvas component, which is hardware accelerated. 
    // It can also switch into full-screen mode when called with a screenNumber. 
    CanvasFrame frame = new CanvasFrame("Some Title"); 

    // OpenCVFrameGrabber uses opencv_highgui, but other more versatile FrameGrabbers 
    // include DC1394FrameGrabber, FlyCaptureFrameGrabber, OpenKinectFrameGrabber, 
    // PS3EyeFrameGrabber, VideoInputFrameGrabber, and FFmpegFrameGrabber. 
    FrameGrabber grabber = new OpenCVFrameGrabber(0); 
    grabber.start(); 

    // FAQ about IplImage: 
    // - For custom raw processing of data, getByteBuffer() returns an NIO direct 
    // buffer wrapped around the memory pointed by imageData. 
    // - To get a BufferedImage from an IplImage, you may call getBufferedImage(). 
    // - The createFrom() factory method can construct an IplImage from a BufferedImage. 
    // - There are also a few copy*() methods for BufferedImage<->IplImage data transfers. 
    IplImage grabbedImage = grabber.grab(); 
    int width = grabbedImage.width(); 
    int height = grabbedImage.height(); 
    IplImage grayImage = IplImage.create(width, height, IPL_DEPTH_8U, 1); 
    IplImage rotatedImage = grabbedImage.clone(); 

    // Let's create some random 3D rotation... 
    CvMat randomR = CvMat.create(3, 3), randomAxis = CvMat.create(3, 1); 
    // We can easily and efficiently access the elements of CvMat objects 
    // with the set of get() and put() methods. 
    randomAxis.put((Math.random()-0.5)/4, (Math.random()-0.5)/4, (Math.random()-0.5)/4); 
    cvRodrigues2(randomAxis, randomR, null); 
    double f = (width + height)/2.0;  randomR.put(0, 2, randomR.get(0, 2)*f); 
              randomR.put(1, 2, randomR.get(1, 2)*f); 
    randomR.put(2, 0, randomR.get(2, 0)/f); randomR.put(2, 1, randomR.get(2, 1)/f); 
    System.out.println(randomR); 

    // Objects allocated with a create*() or clone() factory method are automatically released 
    // by the garbage collector, but may still be explicitly released by calling release(). 
    // You shall NOT call cvReleaseImage(), cvReleaseMemStorage(), etc. on objects allocated this way. 
    CvMemStorage storage = CvMemStorage.create(); 

    // We can allocate native arrays using constructors taking an integer as argument. 
    CvPoint hatPoints = new CvPoint(3); 

    // Again, FFmpegFrameRecorder also exists as a more versatile alternative. 
    FrameRecorder recorder = new OpenCVFrameRecorder("output.avi", width, height); 
    recorder.start(); 

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

     // Let's try to detect some faces! but we need a grayscale image... 
     cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY); 
     CvSeq faces = cvHaarDetectObjects(grayImage, classifier, storage, 
       1.1, 3, CV_HAAR_DO_CANNY_PRUNING); 
     int total = faces.total(); 
     for (int i = 0; i < total; i++) { 
      CvRect r = new CvRect(cvGetSeqElem(faces, i)); 
      int x = r.x(), y = r.y(), w = r.width(), h = r.height(); 
      cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0); 

      // To access the elements of a native array, use the position() method. 
      hatPoints.position(0).x(x-w/10) .y(y-h/10); 
      hatPoints.position(1).x(x+w*11/10).y(y-h/10); 
      hatPoints.position(2).x(x+w/2) .y(y-h/2); 
      cvFillConvexPoly(grabbedImage, hatPoints.position(0), 3, CvScalar.GREEN, CV_AA, 0); 
     } 

     // Let's find some contours! but first some thresholding... 
     cvThreshold(grayImage, grayImage, 64, 255, CV_THRESH_BINARY); 

     // To check if an output argument is null we may call either isNull() or equals(null). 
     CvSeq contour = new CvSeq(null); 
     cvFindContours(grayImage, storage, contour, Loader.sizeof(CvContour.class), 
       CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 
     while (contour != null && !contour.isNull()) { 
      if (contour.elem_size() > 0) { 
       CvSeq points = cvApproxPoly(contour, Loader.sizeof(CvContour.class), 
         storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0); 
       cvDrawContours(grabbedImage, points, CvScalar.BLUE, CvScalar.BLUE, -1, 1, CV_AA); 
      } 
      contour = contour.h_next(); 
     } 

     cvWarpPerspective(grabbedImage, rotatedImage, randomR); 

     frame.showImage(rotatedImage); 
     recorder.record(rotatedImage); 
    } 
    recorder.stop(); 
    grabber.stop(); 
    frame.dispose(); 
} 
} 

위의 코드를 샘플 코드에서 가져 와서 실행하려고했습니다. 언급 한 jar 파일을 추가했습니다. 모든 오류와 경고가 사라졌습니다. 하지만 코드를 실행할 때. javacv 샘플 프로그램이 Eclipse에서 실행되지 않습니까?

나는 다음과 같은 오류가있어 ...
java.lang.UnsatisfiedLinkError: C:\Users\Balachander P\AppData\Local\Temp\jniopencv_core7504532540941704965.dll: Can't find dependent libraries 
    at java.lang.ClassLoader$NativeLibrary.load(Native Method) 
    at java.lang.ClassLoader.loadLibrary0(Unknown Source) 
    at java.lang.ClassLoader.loadLibrary(Unknown Source) 
    at java.lang.Runtime.load0(Unknown Source) 
    at java.lang.System.load(Unknown Source) 
    at com.googlecode.javacpp.Loader.load(Loader.java:352) 
    at com.googlecode.javacpp.Loader.load(Loader.java:285) 
    at com.googlecode.javacv.cpp.opencv_core.<clinit>(opencv_core.java:129) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Unknown Source) 
    at com.googlecode.javacpp.Loader.load(Loader.java:305) 
    at com.googlecode.javacpp.Loader.load(Loader.java:285) 
    at com.googlecode.javacv.cpp.opencv_core$CvArr.<clinit>(opencv_core.java:156) 
    at com.googlecode.javacv.JavaCV.<clinit>(JavaCV.java:88) 

나는 javacv에 새로 온 사람과 처음으로 시도. 누군가가 나를 도와 줄 수 있습니까?

+1

일부 코드를 표시하십시오! 네이티브 라이브러리를 사용하고 있습니까 (그렇게 보입니다). 건배! – SimonSez

+2

여기의 지시 사항을 따르셨습니까? http://code.google.com/p/javacv/wiki/Windows7AndOpenCV –

+0

안내가 도움을 많이주었습니다. – Bala

답변

1

OpenCV (* .dll)의 라이브러리 파일은 기본 설치 디렉토리 또는 Windows의 현재 작업 디렉토리가있는 시스템 PATH에서 찾을 수 있습니다. opnecv가 제대로 연결되지처럼

http://code.google.com/p/javacv/

확인 시스템 PATH는

관련 문제