使用OpenCV Java获取子图像 [英] Get a sub image using opencv Java

查看:71
本文介绍了使用OpenCV Java获取子图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过如何通过在Java api中使用OpenCV获取子图像,但这无济于事

I have already looked at how to get sub image by using OpenCV in java api, but this did not help

我很好奇如何创建从文件中加载的Mat图像的子图像.当我跑步时:

I am curious how to create a sub image of a Mat image that I have loaded in from a file. When I run:

crop = img.submat(405, 450, 280, 335);

我得到了:

  OpenCV Error: Assertion failed (m.dims >= 2) in cv::Mat::Mat, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 269
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matrix.cpp:269: error: (-215) m.dims >= 2 in function cv::Mat::Mat
]
    at org.opencv.core.Mat.n_submat_rr(Native Method)
    at org.opencv.core.Mat.submat(Mat.java:2270)
    at Parking.WebCommunications.processImage(WebCommunications.java:54)
    at Parking.WebCommunications.<init>(WebCommunications.java:27)
    at Parking.App.main(App.java:19)

我似乎无法弄清楚为什么会这样.当我在图像上运行似乎是python中类似代码的一段代码时,它可以正常工作...但是我需要Java才能工作...

I cannot seem to figure out why this is happening. When I run what seems to be a similar piece of code in python on the image it works properly...but I need java to work...

    Range xRange = new Range(405, 450);
    Range yRange = new Range(280, 355);
    Mat crop;
    Mat blur = null;

    System.loadLibrary("opencv_java2411");

    //Load image from file
    Mat img = Highgui.imread("/Users/\"User name\"/git/SE300/JavaWorkspace/ParkingLotApp/src/main/resources/bottomOpen.JPG");

    //LOOP:
        //Crop to the Nth spot: cropN = img[y:y+h, x:x+w]
    System.out.println(img.rows());
    System.out.println(img.cols());

        crop = img.submat(405, 450, 280, 335);

推荐答案

我希望使用Java版本的Opencv中的一些基本类来做到这一点.

I manged to do it with some basic classes in Java version of Opencv.

这是代码.

// select the region fist        
rectCrop = new Rect(start_x, start_y, width, height);

// generate matrix of the interested region, from original_image
        Mat image_roi = new Mat(original_image, rectCrop);

// code to write the interested image to disk
        Highgui.imwrite("/Users/kapil/Research/test_imgs/out/area_of_intereset.jpg", image_roi);

这篇关于使用OpenCV Java获取子图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆