我一直在尝试同时使用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论坛上的一个mod提供了更多信息(http://answers.opencv.org/question/63733/why-does-python-implementation-and-java-implementation-of-mser-create-different-output/):

最佳答案

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

Python cv2.MSER为您提供了一个包装好的cv::MSER,它将operator()作为detect公开给Python:

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

这为您提供了所需的轮廓接口(interface)列表。

相反,Java使用javaFeatureDetector包装器,该包装器调用FeatureDetector::detect支持的MSER::detectImpl,并使用标准FeatureDetector接口(interface):KeyPoints列表。

如果要使用Java(在OpenCV 2.4中)访问operator(),则必须将其包装在JNI中。

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

10-13 08:11
查看更多