`

5.车牌图像字符分割

阅读更多
要识别车牌字符,前提是先进行车牌字符的正确分割与提取。字符分割的任务是把多列或多行字符图像中的每个字符从整个图像中切割出来成为单个字符。车牌字符的正确分割对字符的识别是很关键的。传统的字符分割算法可以归纳为以下三类:直接分割法、基于识别基础上的分割法、自适应分割线类聚法。
// 读取车牌图像
        Mat image = Imgcodecs.imread("2023-plate2.jpg");
        // 转换为灰度图像
        Mat gray = new Mat();
        Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY);
        // 二值化处理
        Mat binary = new Mat();
        Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
        // 去噪处理
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
        Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_OPEN, kernel);
        Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_CLOSE, kernel);

        // 查找轮廓
        List<MatOfPoint> contours = new ArrayList();
        Imgproc.findContours(binary, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        System.out.println(contours.size());
        // 遍历每个轮廓,进行字符分割
        for (int i = 0; i < contours.size(); i++) {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            if (rect.width <20  && rect.height > 20) { // 根据实际情况调整阈值&& rect.height > 30
                System.out.println(i+",width: "+rect.width+", height: "+rect.height);
                Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255));
            }
        }
        // 显示结果图像
        //Imgcodecs.imwrite("2023-plate2-4.jpg", image);
        Rect rect1 = Imgproc.boundingRect(contours.get(8));
        Mat croppedImage1 = new Mat(image, rect1);
        Imgcodecs.imwrite("2023-plate2-N1.jpg", croppedImage1);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics