java - 为什么 OpenCV 的 MSER 的 Python 实现和 Java 实现会产生不同的输出?

标签 java python opencv

我一直在尝试同时使用 OpenCV 的 MSER 算法的 Python 实现(opencv 2.4.11)和 Java 实现(opencv 2.4.10)。有趣的是,我注意到 MSER 的检测在 Python 和 Java 中返回不同类型的输出。在 Python 中,detect 返回一个点列表列表,其中每个点列表代表一个检测到的 blob。在 Java 中,返回一个 Mat,其中每一行都是一个点,其相关直径表示检测到的 Blob 。我想重现 Java 中的 Python 行为,其中 blob 由一组点定义,而不是一个点。有谁知道怎么回事?

Python:

frame = cv2.imread('test.jpg')
mser = cv2.MSER(**dict((k, kw[k]) for k in MSER_KEYS))  
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  
regions = mser.detect(gray, None)
print("REGIONS ARE: " + str(regions))

where the dict given to cv2.MSER is
{'_delta':7, '_min_area': 2000, '_max_area': 20000, '_max_variation': .25, '_min_diversity': .2, '_max_evolution': 200, '_area_threshold': 1.01, '_min_margin': .003, '_edge_blur_size': 5}

Python 输出:

REGIONS ARE: [array([[197,  58],
   [197,  59],
   [197,  60],
   ..., 
   [143,  75],
   [167,  86],
   [172,  98]], dtype=int32), array([[114,   2],
   [114,   1],
   [114,   0],
   ..., 
   [144,  56],
   [ 84,  55],
   [ 83,  55]], dtype=int32)]

Java:

Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_16S, new Scalar(4));
Mat gray = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_16S, new Scalar(4));
Imgproc.cvtColor(mat, gray, Imgproc.COLOR_RGB2GRAY, 4);

FeatureDetector fd = FeatureDetector.create(FeatureDetector.MSER);
MatOfKeyPoint regions = new MatOfKeyPoint();
fd.detect(gray, regions);
System.out.println("REGIONS ARE: " + regions);

Java 输出:

REGIONS ARE: Mat [ 10*1*CV_32FC(7), isCont=true, isSubmat=false, nativeObj=0x6702c688, dataAddr=0x59add760 ]

where each row of the Mat looks like
KeyPoint [pt={365.3387451171875, 363.75640869140625}, size=10.680443, angle=-1.0, response=0.0, octave=0, class_id=-1]

编辑:

answers.opencv.org 论坛上的一个模组提供了更多信息 (http://answers.opencv.org/question/63733/why-does-python-implementation-and-java-implementation-of-mser-create-different-output/):

unfortunately, it looks, like the java version is limited to the features2d.FeatureDetector interface, which lets you only access KeyPoints (not the actual Regions)

berak (Jun 10 '15)

@berak: So if I understand correctly from the docs, both the java version and the python/C++ version have the features2d.FeatureDetector interface, but the python/C++ version has the additional MSER class to find regions, not just key points? In that case, what do people do? Is it possible to add the C++ MSER class to the OpenCV manager, edit something like the javaFeatureDetector here here, and create a java wrapper for it? Thanks for any advice.

sloreti (Jun 11 '15)

so yes, you can get the rectangles in c++ or python, but not from java. that's a flaw in the design. the javaFeatureDetector is still in use, but to get the rectangles, you'd have to write your own jni interface, i guess. (and distribute your own .so along with your apk)

berak (Jun 12 '15)

最佳答案

您正在对 MSER 实现使用两个不同的接口(interface)。

Python cv2.MSER 给你一个包装好的 cv::MSER,它把它的 operator() 暴露给 Python 作为 detect :

//! the operator that extracts the MSERs from the image or the specific part of it
CV_WRAP_AS(detect) void operator()( const Mat& image, CV_OUT vector<vector<Point> >& msers,
                                    const Mat& mask=Mat() ) const;

这为您提供了您正在寻找的轮廓界面的漂亮列表。

相比之下,Java 使用 javaFeatureDetector 包装器,它调用由 MSER::detectImpl 支持的 FeatureDetector::detect 并使用标准的 FeatureDetector interface:一个KeyPoints列表。

如果您想在 Java(OpenCV 2.4 中)中访问 operator(),您必须将其包装在 JNI 中。

关于java - 为什么 OpenCV 的 MSER 的 Python 实现和 Java 实现会产生不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489149/

相关文章:

java - 如何使用 Nimbus LookAndFeel 更改 JToolTip 的背景颜色?

java - UrlBasedViewResolver 中的 viewClass 属性

python - pytest 具有相似包和模块结构的多个应用程序

python - 编写一个与 wpa_supplicant 交互的外部程序

c++ - 为 Visual Studio 2010 设置 OpenCV-2.3

java - Android OpenCV - CameraBridgeViewBase 拍照?

java - HashMap共享对象问题

java - 另一个线程的监听器

python - 从子目录导入模块

c++ - OpenCV彩色图像转灰度麻烦