Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using the new OpenCV Java bindings to read an image and do some processing on the image. I'm trying to convert C code to Java using the Java bindings but can't seem to find the relevant methods:

C code:

cv::Mat img = cv::imread(argv[1]);
cv::Mat gray;
cv::cvtColor(img, gray, CV_BGR2GRAY);
int erosion_size = 5;
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS,
                                       cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
                                       cv::Point(erosion_size, erosion_size) );
cv::erode(gray, gray, element);

I can't find:

  • imread
  • cvtcolor
  • getStructuringElement
  • erode
  • I looked around the api here: http://docs.opencv.org/java/

    Unfortunately the sample java code provided doesn't even show how to read an image!

  • CV_LOAD_IMAGE_ANYDEPTH: returns 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
  • CV_LOAD_IMAGE_COLOR: always convert image to a color one.
  • CV_LOAD_IMAGE_GRAYSCALE: always convert image to a grayscale one.
  • Example:

    // OpenCV 2.x   
    Mat img = Highgui.imread("path/to/img", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    // OpenCV 3.x
    Mat img = Imgcodecs.imread("path/to/img", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    

    If you have correctly installed Opencv with support for Java desktop and included opencv-2.4.4.jar , your should import:

    import org.opencv.imgproc.Imgproc;
    import org.opencv.core.Point;
    import org.opencv.core.Size;
    import org.opencv.highgui.Highgui;
    

    And your code will look like this:

    Mat img = Highgui.imread(argv[1], Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    int erosion_size = 5;
    Mat element  = Imgproc.getStructuringElement(
        Imgproc.MORPH_CROSS, new Size(2 * erosion_size + 1, 2 * erosion_size + 1), 
        new Point(erosion_size, erosion_size)
    Imgproc.erode(img, img, element);
                    I know this is an old post, but i configured open-cv and all, i try running your code andi get: "Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J 	at org.opencv.highgui.Highgui.imread_0(Native Method) 	at org.opencv.highgui.Highgui.imread(Highgui.java:306) 	at pack.ColoredObjectTrack.main(ColoredObjectTrack.java:41)"
    – susparsy
                    Jan 30, 2014 at 8:30
                    You have not correctly referenced native library. Follow the tutorial with more attention.
    – andriy
                    Jan 30, 2014 at 8:55
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.