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

slo*_*eti 35 python java opencv

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

蟒蛇:

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}
Run Code Online (Sandbox Code Playgroud)

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)]
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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]
Run Code Online (Sandbox Code Playgroud)

编辑:

在answers.opencv.org论坛上的mod提供了更多信息(http://answers.opencv.org/question/63733/why-does-python-implementation-and-java-implementation-of-mser-create-不同的输出/):

不幸的是,它看起来像java版本仅限于features2d.FeatureDetector接口,它允许您只访问KeyPoints(而不是实际的Region)

berak(2015年6月10日)

@berak:所以如果我从文档中正确理解,java版本和python/C++版本都有features2d.FeatureDetector接口,但是python/C++版本有额外的MSER类来查找区域,而不仅仅是关键点?在那种情况下,人们做什么?是否可以将C++ MSER类添加到OpenCV管理器,在这里编辑类似javaFeatureDetector的东西,并为它创建一个java包装器?谢谢你的建议.

sloreti(2015年6月11日)

所以,是的,您可以使用c ++或python获取矩形,但不能使用java.这是设计中的一个缺陷.javaFeatureDetector仍然在使用,但是为了得到矩形,我猜你必须编写自己的jni接口.(并与你的apk一起分发你自己的.so)

berak(2015年6月12日)

szy*_*zym 1

您正在使用两个不同的接口来实现 MSER。

Pythoncv2.MSER为您提供了一个wrapped 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;
Run Code Online (Sandbox Code Playgroud)

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

相比之下,Java 使用javaFeatureDetector包装器,该包装器FeatureDetector::detect调用MSER::detectImpl并使用标准 FeatureDetector 接口:关键点列表。

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