Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

here is my code for license plate recogniaton private void Form1Load(object sender,…

  1. User

    here is my code for license plate recogniaton

        private void Form1_Load(object sender, EventArgs e)
        {
            string imagePath = @"D:\lp3.jpg";
    
    
            string licensePlate = ReadLicensePlate(imagePath);
    
    
            MessageBox.Show("The license plate is: " + licensePlate);
        }
        static bool CheckFarsiDataExists(string tessdataPath)
        {
            string farsiDataFile = Path.Combine(tessdataPath, "fas.traineddata");
            bool exists = File.Exists(farsiDataFile);
            return exists;
        }
        static string ReadLicensePlate(string imagePath)
        {
            string extractedText = "";
    
            // Check if Farsi data file exists
            string tessdataPath = @"./tessdata";
            if (!CheckFarsiDataExists(tessdataPath))
            {
                Console.WriteLine("Farsi data file not found in tessdata folder.");
                return "";
            }
    
            using (var engine = new TesseractEngine(@"./tessdata", "fas", EngineMode.Default))
            {
    
                engine.SetVariable("tessedit_char_whitelist", "ابپتثجچحخدذرزسشصضطظعغفقکگلمنوهی0123456789");
                engine.SetVariable("classify_bln_numeric_mode", "1");
                using (var img = Pix.LoadFromMemory(PreprocessImage(imagePath)))
                {
                    using (var page = engine.Process(img))
                    {
                        extractedText = page.GetText();
                        float confidence = page.GetMeanConfidence();
                        Console.WriteLine("Confidence: " + (confidence * 100) + "%");
                    }
                }
            }
    
            return extractedText;
        }
    
    
    
        static byte[] PreprocessImage(string imagePath)
        {
            Mat src = Cv2.ImRead(imagePath, ImreadModes.Color);
            if (src.Empty())
            {
                Console.WriteLine("Image not found…");
                return Array.Empty<byte>();
            }
    
            // Resize image
            int newWidth = src.Width * 2;
            int newHeight = src.Height * 2;
            Mat resized = new Mat();
            Cv2.Resize(src, resized, new OpenCvSharp.Size(newWidth, newHeight), 0, 0, InterpolationFlags.Linear);
    
            // Convert to grayscale
            Mat gray = new Mat();
            Cv2.CvtColor(resized, gray, ColorConversionCodes.BGR2GRAY);
    
            // Apply median blur
            Mat medianBlurred = new Mat();
            Cv2.MedianBlur(gray, medianBlurred, 3);
    
            // Apply adaptive thresholding
            Mat thresholded = new Mat();
            Cv2.AdaptiveThreshold(medianBlurred, thresholded, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 11, 2);
    
            byte[] tmp;
            Cv2.ImEncode(".jpg", thresholded, out tmp);
    
            return tmp;
        }
    

    can you use bilateralFilter and Canny for bette result?

  2. Assistant

    Yes, you can use bilateralFilter and Canny edge detection to improve the license plate recognition results. Here is the modified code:

    Replace the PreprocessImage function with this:

    static byte[] PreprocessImage(string imagePath)
    {
        Mat src = Cv2.ImRead(imagePath, ImreadModes.Color);
        if (src.Empty())
        {
            Console.WriteLine("Image not found…");
            return Array.Empty<byte>();
        }
    
        // Resize image
        int newWidth = src.Width * 2;
        int newHeight = src.Height * 2;
        Mat resized = new Mat();
        Cv2.Resize(src, resized, new OpenCvSharp.Size(newWidth, newHeight), 0, 0, InterpolationFlags.Linear);
    
        // Convert to grayscale
        Mat gray = new Mat();
        Cv2.CvtColor(resized, gray, ColorConversionCodes.BGR2GRAY);
    
        // Apply bilateral filter
        Mat bilateralFiltered = new Mat();
        Cv2.BilateralFilter(gray, bilateralFiltered, 9, 75, 75);
    
        // Apply Canny edge detection
        Mat cannyEdges = new Mat();
        Cv2.Canny(bilateralFiltered, cannyEdges, 100, 200);
    
        // Apply median blur
        Mat medianBlurred = new Mat();
        Cv2.MedianBlur(cannyEdges, medianBlurred, 3);
    
        // Apply adaptive thresholding
        Mat thresholded = new Mat();
        Cv2.AdaptiveThreshold(medianBlurred, thresholded, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 11, 2);
    
        byte[] tmp;
        Cv2.ImEncode(".jpg", thresholded, out tmp);
    
        return tmp;
    }
    

    This version of the function applies a bilateral filter to the grayscale image before performing Canny edge detection. It could reduce noise in the image while preserving edge information. Then the Canny edge detection is applied, which is a popular edge detection technique in image processing. The rest of the pipeline, including the median blur and adaptive thresholding, remains the same.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.