用java对连接字符的图像进行分割
我试图在下图中的不同角色周围画一个边界框。但是,由于某些字符已连接,因此无法绘制方框。我试过一些东西,比如;扩张,侵蚀和尝试不同的模糊,但我似乎无法将它们分开。侵蚀似乎是接近的最佳方式,但如果我继续侵蚀,字符就无法识别
我使用Java中的OpenCV库来实现这一点
Mat img = Imgcodecs.imread("test.jpg");
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(img, img, 220, 255, 0);
Imgproc.erode(img, img, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)));
Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
MatOfPoint2f approxCurve = new MatOfPoint2f();
//For each contour found
for (int i = 0; i < contours.size(); i++) {
MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
MatOfPoint points = new MatOfPoint(approxCurve.toArray());
Rect rect = Imgproc.boundingRect(points);
Imgproc.rectangle(img, rect.tl(), rect.br(), new Scalar(255, 255, 255), 1, 8, 0);
}
System.out.println(img);
Imgcodecs.imwrite("o.jpg", img);
之前:
之后:
# 1 楼答案
一种可能的方法(你可以尝试)是,如果你知道字符的数量,例如:5,那么你可以先在分割的轮廓周围绑定一个矩形,然后将宽度除以5,然后在图像上绘制黑色的矩形,然后图像将被成功分割
按照以下步骤操作:
结果如下:
现在图像已经清晰到可以识别了