gpt4 book ai didi

java - 使用opencv Java获取子图像

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:09 26 4
gpt4 key购买 nike

我已经看了how to get sub image by using OpenCV in java api , 但这没有帮助

我很好奇如何创建我从文件加载的 Mat 图像的子图像。当我运行时:

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 才能工作……

编辑:

    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 中的一些基本类做到了这一点。

这是itp的

// 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);

关于java - 使用opencv Java获取子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com