Helps in detecting text characters on metal plates

Hi everyone, I’m new to OpenCV.
I’m trying to identify text on metal plates and vehicle chassis.

This would be the desired effect:

Tried this implementation below:

package com.autoscore;

import nu.pattern.OpenCV;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

public class CharDetect {

    public static void main(String[] args) {
        // Carregue a biblioteca OpenCV
        OpenCV.loadLocally();

        // Load image, grayscale, Otsu's threshold
        String imagePath = "./src/main/resources/chassi3.jpeg";

        // Carrega a imagem
        Mat image = Imgcodecs.imread(imagePath, Imgcodecs.IMREAD_GRAYSCALE);

        // Aplica binarização adaptativa para melhorar a detecção de texto branco em fundo escuro
        Mat adaptiveThreshold = new Mat();
        Imgproc.adaptiveThreshold(image, adaptiveThreshold, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 11, 3);

        // Aplica um filtro de média para remover ruídos
        Mat filteredImage = new Mat();
        Imgproc.medianBlur(adaptiveThreshold, filteredImage, 5);

        // Encontra os contornos na imagem filtrada
        Mat hierarchy = new Mat();
        List<MatOfPoint> contours = new ArrayList<>();
        Imgproc.findContours(filteredImage, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

        // Desenha os contornos na imagem original
        Mat contourImage = new Mat();
        Imgproc.cvtColor(image, contourImage, Imgproc.COLOR_GRAY2BGR);
        Scalar color = new Scalar(0, 0, 255); // Cor vermelha para os contornos
        int thickness = 2; // Espessura da linha do contorno

        for (MatOfPoint contour : contours) {
            Rect rect = Imgproc.boundingRect(contour);
            double aspectRatio = (double) rect.width / rect.height;

            // Verifica se a proporção da largura pela altura está próxima à proporção de um caractere de texto comum
            if (aspectRatio > 0.1 && aspectRatio < 1.0) {
                double area = Imgproc.contourArea(contour);
                if (area > 100) { // Ajuste o valor do limiar conforme necessário para evitar contornos indesejados
                    Imgproc.rectangle(contourImage, rect.tl(), rect.br(), color, thickness);
                }
            }
        }


        HighGui.imshow("thresh", contourImage);
       // HighGui.imshow("croppedImage", croppedImage);

        HighGui.waitKey();
    }
}

But this was the only result I got:

you could try to exploit, that this is a monospaced font, and you mainly need to detect ‘a grid of letters’
(they’re all same width / height !)

Would you have any examples of how to do this, I’m really lost.

these situations are usually solved by training a neural network.

no, I highly doubt that tesseract ocr can solve any non-trivial tasks. don’t even think about that. use something else.

Could you provide a sample original image

IMG_20220611_115619557_HDR