【发布时间】:2018-05-10 21:21:05
【问题描述】:
我正在尝试将 Android 相机预览图像输入 OpenCV 以检测 Aruco 代码。根据前人的工作,目前的流程如下:
- 我有一个 YUV
Image.Plane。 - 我把它喂给 ZXing
PlanarYUVLuminanceSource。 - 对此我调用
getMatrix(),它为我提供了byte[]的亮度值。 - 检查https://github.com/jsmith613/Aruco-Marker-Tracking-Android/,我注意到它正在调用
(CvCameraViewFrame).rgba(),并且返回的Mat是CV_8UC4类型。我注意到调用(Mat).get(0, 0)会产生double[4],例如{1, 4, 2, 255},我推断这些对应于RGBA。然后将此Mat传递给(MarkerDetector).detect()。 - 因此,我构造了一个相同形式和类型的
Mat,并用数据加载它。 Mat mat = new Mat(width, height, CvType.CV_8UC4, new Scalar(0.0, 0.0, 0.0, 255.0));- (对于 x 和 y:)
int lum = matrix[y * w + x] & 0xFF; mat.put(x, y, new double[]{lum, lum, lum, 255});- 然后我把这个
Mat给检测器。
它工作,但 for 循环很慢 - 复制所有像素大约需要一秒钟。我强烈怀疑有一种更快的方法 - 肯定有一种方法可以传递一个纯字节数组进入Mat?仍然可以与(MarkerDetector).detect() 一起使用?
我的代码,一旦我有PlanarYUVLuminanceSource (source),如下:
Mat mat = new Mat(width, height, CvType.CV_8UC4, new Scalar(0.0, 0.0, 0.0, 255.0));
byte[] matrix = source.getMatrix();
double[] pixel = new double[]{0,0,0,255};
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int luminance = matrix[y * width + x] & 0xFF;
pixel[0] = luminance;
pixel[1] = luminance;
pixel[2] = luminance;
mat.put(x, y, pixel);
}
}
Vector<Marker> markers = new Vector<>();
mMarkerDetector.detect(mat, result, mCameraParameters, Constants.ARUCO_MARKER_SIZE, null);
【问题讨论】:
标签: android opencv optimization yuv