嗨,大家好,如何使用MatOfKeyPoint繪製矩形進行文本檢測| Java
我一直在用OpenCV4Android進行實時文本檢測和識別。識別部分已全部完成。但是,我不得不問一些關於文本檢測的問題。我正在使用MSER FeatureDetector進行檢測文本。
這纔是真正的時間和調用該方法的一部分:
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
carrierMat = inputFrame.gray();
carrierMat = General.MSER(carrierMat);
return carrierMat;
}
這是基本的MSER實現:
private static FeatureDetector fd = FeatureDetector.create(FeatureDetector.MSER);
private static MatOfKeyPoint mokp = new MatOfKeyPoint();
private static Mat edges = new Mat();
public static Mat MSER(Mat mat) {
//for mask
Imgproc.Canny(mat, edges, 400, 450);
fd.detect(mat, mokp, edges);
//for drawing keypoints
Features2d.drawKeypoints(mat, mokp, mat);
return mat;
}
它工作正常用於查找與邊緣的面具文本。
我想畫一個矩形爲這樣的集羣:
或本
你可以假設我有合適的點。如你所見,fd.detect()方法返回一個MatOfKeyPoint。因此,我已經試過這種方法繪製矩形:
public static Mat MSER_(Mat mat) {
fd.detect(mat, mokp);
KeyPoint[] refKp = mokp.toArray();
Point[] refPts = new Point[refKp.length];
for (int i = 0; i < refKp.length; i++) {
refPts[i] = refKp[i].pt;
}
MatOfPoint2f refMatPt = new MatOfPoint2f(refPts);
MatOfPoint2f approxCurve = new MatOfPoint2f();
//Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(refMatPt, true) * 0.02;
Imgproc.approxPolyDP(refMatPt, approxCurve, approxDistance, true);
//Convert back to MatOfPoint
MatOfPoint points = new MatOfPoint(approxCurve.toArray());
// Get bounding rect
Rect rect = Imgproc.boundingRect(points);
// draw enclosing rectangle (all same color, but you could use variable i to make them unique)
Imgproc.rectangle(mat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), Detect_Color_, 5);
//Features2d.drawKeypoints(mat, mokp, mat);
return mat;
}
但是,當我試圖Imgproc.arcLength()方法時,突然停了下來。我爲Imgproc.approxPolyDP()方法給出了一個隨機的近距離值,如0.1,但它並沒有真正有效地工作。
那麼如何才能爲檢測到的文字繪製矩形?
乾杯!
嗨,你有沒有找到這個解決方案? – Zy0n
不是,我改變了整個算法:) –